comparison jpg.c @ 10:078dc69945ad

working: libjpeg-turbo support, handle fb and monitor with different size
author Peter Meerwald <pmeerw@pmeerw.net>
date Sun, 15 May 2011 17:48:57 +0200
parents 66c77f9ba9b9
children
comparison
equal deleted inserted replaced
9:c7af696b6221 10:078dc69945ad
90 90
91 dest->pub.next_output_byte = dest->buffer = *outbuffer; 91 dest->pub.next_output_byte = dest->buffer = *outbuffer;
92 dest->pub.free_in_buffer = dest->bufsize = *outsize; 92 dest->pub.free_in_buffer = dest->bufsize = *outsize;
93 } 93 }
94 94
95 #ifdef HAVE_LIBJPEG_TURBO
96 jpg_buf_t build_jpg_from_fb(unsigned char *fb_mem, int fb_width, int fb_height, int bits_per_pixel, int mon_width, int mon_height) {
97 int scanline;
98 struct jpeg_compress_struct cinfo;
99 struct jpeg_error_mgr jerr;
100
101 cinfo.err = jpeg_std_error(&jerr);
102 jpeg_create_compress(&cinfo);
103
104 jpg_buf_t jpg_buf;
105 jpg_buf.size = 1 << 20;
106 jpg_buf.ptr = NULL;
107
108 my_jpeg_mem_dest(&cinfo, &jpg_buf.ptr, &jpg_buf.size);
109
110 cinfo.image_width = mon_width;
111 cinfo.image_height = mon_height;
112 cinfo.input_components = 4;
113 cinfo.in_color_space = JCS_EXT_BGRX;
114 jpeg_set_defaults(&cinfo);
115 cinfo.dct_method = JDCT_FASTEST;
116 jpeg_set_quality(&cinfo, 70, TRUE);
117
118 jpeg_start_compress(&cinfo, TRUE);
119
120 unsigned char buf[mon_width * 4];
121 JSAMPROW row_ptr[1] = {buf};
122 memset(buf, 0, mon_width*4);
123 if (fb_width >= mon_width)
124 for (scanline = 0; scanline < MIN(fb_height, mon_height); scanline++) {
125 row_ptr[0] = &fb_mem[scanline * fb_width * 4];
126 jpeg_write_scanlines(&cinfo, row_ptr, 1);
127 }
128 else {
129 for (scanline = 0; scanline < MIN(fb_height, mon_height); scanline++) {
130 memcpy(buf, &fb_mem[scanline * fb_width * 4], fb_width * 4);
131 jpeg_write_scanlines(&cinfo, row_ptr, 1);
132 }
133 }
134 memset(buf, 0, mon_width*4);
135 for (scanline = fb_height; scanline < mon_height; scanline++) {
136 jpeg_write_scanlines(&cinfo, row_ptr, 1);
137 }
138
139 jpeg_finish_compress(&cinfo);
140
141 jpeg_destroy_compress(&cinfo);
142
143 return jpg_buf;
144 }
145 #else
95 static void convert_rgba_to_rgb(unsigned char *buf, unsigned char *fb_mem, int width) { 146 static void convert_rgba_to_rgb(unsigned char *buf, unsigned char *fb_mem, int width) {
96 int i; 147 int i;
97 for (i = 0; i < width; i++) { 148 for (i = 0; i < width; i++) {
98 *buf++ = *fb_mem++; 149 // BGRX
99 *buf++ = *fb_mem++; 150 unsigned int x = *(unsigned int *)fb_mem;
100 *buf++ = *fb_mem++; 151 *buf++ = (x >> 16) & 0xff;
101 fb_mem++; 152 *buf++ = (x >> 8) & 0xff;
102 } 153 *buf++ = (x) & 0xff;
103 } 154 fb_mem += 4;
104 155 }
105 jpg_buf_t build_jpg_from_fb(unsigned char *fb_mem, int width, int height, int bits_per_pixel) { 156 }
157
158 jpg_buf_t build_jpg_from_fb(unsigned char *fb_mem, int fb_width, int fb_height, int bits_per_pixel, int mon_width, int mon_height) {
106 int scanline; 159 int scanline;
107 struct jpeg_compress_struct cinfo; 160 struct jpeg_compress_struct cinfo;
108 struct jpeg_error_mgr jerr; 161 struct jpeg_error_mgr jerr;
109 162
110 cinfo.err = jpeg_std_error(&jerr); 163 cinfo.err = jpeg_std_error(&jerr);
114 jpg_buf.size = 1 << 20; 167 jpg_buf.size = 1 << 20;
115 jpg_buf.ptr = NULL; 168 jpg_buf.ptr = NULL;
116 169
117 my_jpeg_mem_dest(&cinfo, &jpg_buf.ptr, &jpg_buf.size); 170 my_jpeg_mem_dest(&cinfo, &jpg_buf.ptr, &jpg_buf.size);
118 171
119 cinfo.image_width = width; 172 cinfo.image_width = mon_width;
120 cinfo.image_height = height; 173 cinfo.image_height = mon_height;
121 cinfo.input_components = 3; 174 cinfo.input_components = 3;
122 cinfo.in_color_space = JCS_RGB; 175 cinfo.in_color_space = JCS_RGB;
123 jpeg_set_defaults(&cinfo); 176 jpeg_set_defaults(&cinfo);
124 177 cinfo.dct_method = JDCT_FASTEST;
178 jpeg_set_quality(&cinfo, 70, TRUE);
179
125 jpeg_start_compress(&cinfo, TRUE); 180 jpeg_start_compress(&cinfo, TRUE);
126 181
127 JSAMPLE *buf = (JSAMPLE *) malloc(width * 3); 182 JSAMPLE buf[mon_width * 3];
128 JSAMPROW row_ptr[1] = {buf}; 183 JSAMPROW row_ptr[1] = {buf};
129 for (scanline = 0; scanline < height; scanline++) { 184 memset(buf, 0, mon_width*3);
130 convert_rgba_to_rgb(buf, &fb_mem[scanline * width * 4], width); 185 for (scanline = 0; scanline < MIN(fb_height, mon_height); scanline++) {
186 convert_rgba_to_rgb(buf, &fb_mem[scanline * fb_width * 4], mon_width);
131 jpeg_write_scanlines(&cinfo, row_ptr, 1); 187 jpeg_write_scanlines(&cinfo, row_ptr, 1);
132 } 188 }
133 free(buf); 189 memset(buf, 0, mon_width*3);
190 for (scanline = fb_height; scanline < mon_height; scanline++) {
191 jpeg_write_scanlines(&cinfo, row_ptr, 1);
192 }
134 193
135 jpeg_finish_compress(&cinfo); 194 jpeg_finish_compress(&cinfo);
136 195
137 jpeg_destroy_compress(&cinfo); 196 jpeg_destroy_compress(&cinfo);
138 197
139 return jpg_buf; 198 return jpg_buf;
140 } 199 }
200 #endif

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.