1 #include <stdio.h>
2
3 #include "jpeglib.h"
4
5 void
6 JpegInitDestination(j_compress_ptr cinfo)
7 {
8 }
9
10 static boolean
11 JpegEmptyOutputBuffer(j_compress_ptr cinfo)
12 {
13 return TRUE;
14 }
15
16 static void
17 JpegTermDestination(j_compress_ptr cinfo)
18 {
19 // jpegDstDataLen = jpegDstBufferLen - jpegDstManager.free_in_buffer;
20 }
21
22 /**
23 Raw Rgb Data converted to Jpeg data
24 */
25 bool JpegCompress(int w,int h,const char * rgb_data,int rgb_size,
26 char * jpeg_data,int *jpeg_size){
27 struct jpeg_compress_struct cinfo;
28 struct jpeg_error_mgr jerr;
29 struct jpeg_destination_mgr jpegDstManager;
30 int ret;
31 unsigned char *srcBuf = new unsigned char[w * 3];
32 JSAMPROW rowPointer[1];
33 rowPointer[0] = (JSAMPROW)srcBuf;
34 int left_size;
35 left_size = *jpeg_size;
36 cinfo.err = jpeg_std_error(&jerr);
37 jpeg_create_compress(&cinfo);
38
39 cinfo.image_width = w;
40 cinfo.image_height = h;
41 cinfo.input_components = 3;
42 cinfo.in_color_space = JCS_RGB;
43 cinfo.raw_data_in = true;
44 jpeg_set_defaults(&cinfo);
45
46
47 cinfo.dest = &jpegDstManager;
48
49 jpegDstManager.next_output_byte = (unsigned char*)jpeg_data;
50 jpegDstManager.free_in_buffer = left_size;
51 jpegDstManager.init_destination = JpegInitDestination;
52 jpegDstManager.empty_output_buffer = JpegEmptyOutputBuffer;
53 jpegDstManager.term_destination = JpegTermDestination;
54
55 //jpeg_set_quality(&cinfo, 20, TRUE);
56
57 jpeg_start_compress(&cinfo, TRUE);
58 for(int y=0;y< h;y++){
59 rowPointer[0] = (unsigned char*)(rgb_data + y*w*3);
60 ret = jpeg_write_scanlines(&cinfo, rowPointer, 1);
61 }
62 jpeg_finish_compress(&cinfo);
63 jpeg_destroy_compress(&cinfo);
64 *jpeg_size = left_size - jpegDstManager.free_in_buffer;
65 return true;
66 }
67
68 void
69 JpegInitSource(j_decompress_ptr cinfo)
70 {
71
72 }
73
74 boolean
75 JpegFillInputBuffer(j_decompress_ptr cinfo)
76 {
77 /*
78 jpegError = true;
79 jpegSrcManager.bytes_in_buffer = jpegBufferLen;
80 jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr; */
81 return TRUE;
82 }
83
84 void
85 JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes)
86 {/*
87 if (num_bytes < 0 || (size_t)num_bytes > jpegSrcManager.bytes_in_buffer) {
88 jpegError = true;
89 jpegSrcManager.bytes_in_buffer = jpegBufferLen;
90 jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr;
91 } else {
92 jpegSrcManager.next_input_byte += (size_t) num_bytes;
93 jpegSrcManager.bytes_in_buffer -= (size_t) num_bytes;
94 }*/
95 }
96
97 void
98 JpegTermSource(j_decompress_ptr cinfo)
99 {
100 /* No work necessary here. */
101 }
102
103 bool JpegUnCompress(const char * jpeg_data,int jpeg_size,
104 char *rgb_data,int rgb_size,int w,int h){
105 struct jpeg_decompress_struct cinfo;
106 struct jpeg_error_mgr jerr;
107 struct jpeg_source_mgr jpegSrcManager;
108 int ret;
109 JSAMPROW rowPointer[1];
110 cinfo.err = jpeg_std_error(&jerr);
111 jpeg_create_decompress(&cinfo);
112
113 jpegSrcManager.init_source = JpegInitSource;
114 jpegSrcManager.fill_input_buffer = JpegFillInputBuffer;
115 jpegSrcManager.skip_input_data = JpegSkipInputData;
116 jpegSrcManager.resync_to_restart = jpeg_resync_to_restart;
117 jpegSrcManager.term_source = JpegTermSource;
118 jpegSrcManager.next_input_byte = (unsigned char*)jpeg_data;
119 jpegSrcManager.bytes_in_buffer = jpeg_size;
120 cinfo.src = &jpegSrcManager;
121
122 jpeg_read_header(&cinfo, TRUE);
123 cinfo.out_color_space = JCS_RGB;
124 jpeg_start_decompress(&cinfo);
125 if( cinfo.output_width != (unsigned int)w && cinfo.output_height != (unsigned int)h){
126 jpeg_destroy_decompress(&cinfo);
127 return false;
128 }
129 for (int dy = 0; cinfo.output_scanline < cinfo.output_height; dy++) {
130 rowPointer[0] = (unsigned char *)(rgb_data + w*dy*3);
131 ret = jpeg_read_scanlines(&cinfo, rowPointer, 1);
132 }
133 jpeg_finish_decompress(&cinfo);
134 jpeg_destroy_decompress(&cinfo);
135 return true;
136 }
137