Data Structures | |
class | CAPTURE |
SUMMARY.....: Class for manipulates video capture. More... | |
struct | my_error_mgr |
Typedefs | |
typedef my_error_mgr * | my_error_ptr |
Functions | |
GLOBAL (imageRGB) | |
METHODDEF (void) | |
imageRGB | read_JPEG_file (char *filename) |
Load and decompress JPEG image from disk */. | |
bool | write_JPEG_file (char *filename, int quality, imageRGB img) |
Compress image into JPEG, and save it to disk quality must be in range 0-100. |
typedef struct my_error_mgr* my_error_ptr |
HAIR::GLOBAL | ( | imageRGB | ) |
Definition at line 146 of file jpeg.c.
References imageRGB::data, imageRGB::height, my_error_mgr::pub, my_error_mgr::setjmp_buffer, and imageRGB::width.
00147 { 00148 struct jpeg_decompress_struct cinfo; 00149 struct my_error_mgr jerr; 00150 FILE * infile; /* source file */ 00151 JSAMPARRAY buffer; /* Output row buffer */ 00152 int row_stride; /* physical row width in output buffer */ 00153 00154 if ((infile = fopen(filename, "rb")) == NULL) 00155 { 00156 fprintf(stderr, "can't open %s\n", filename); 00157 imageRGB zero = {0,0,0}; 00158 return zero; 00159 } 00160 00161 cinfo.err = jpeg_std_error(&jerr.pub); 00162 jerr.pub.error_exit = my_error_exit; 00163 if (setjmp(jerr.setjmp_buffer)) 00164 { 00165 jpeg_destroy_decompress(&cinfo); 00166 fclose(infile); 00167 imageRGB zero = {0,0,0}; 00168 return zero; 00169 } 00170 00171 jpeg_create_decompress(&cinfo); 00172 jpeg_stdio_src(&cinfo, infile); 00173 (void) jpeg_read_header(&cinfo, TRUE); 00174 (void) jpeg_start_decompress(&cinfo); 00175 row_stride = cinfo.output_width * cinfo.output_components; 00176 buffer = (*cinfo.mem->alloc_sarray) 00177 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); 00178 imageRGB img; 00179 img.width = cinfo.output_width; 00180 img.height = cinfo.output_height; 00181 00182 img.data = (unsigned char*)malloc(cinfo.output_components * img.width * img.height); 00183 unsigned char* ptr = img.data; 00184 while (cinfo.output_scanline < cinfo.output_height) 00185 { 00186 (void) jpeg_read_scanlines(&cinfo, buffer, 1); 00187 memcpy(ptr, buffer[0], row_stride); ptr += row_stride; 00188 } 00189 00190 (void) jpeg_finish_decompress(&cinfo); 00191 jpeg_destroy_decompress(&cinfo); 00192 00193 fclose(infile); 00194 00195 return img; 00196 }
HAIR::METHODDEF | ( | void | ) |
Definition at line 131 of file jpeg.c.
References my_error_mgr::setjmp_buffer.
00132 { 00133 my_error_ptr myerr = (my_error_ptr) cinfo->err; 00134 00135 (*cinfo->err->output_message) (cinfo); 00136 00137 longjmp(myerr->setjmp_buffer, 1); 00138 }
imageRGB HAIR::read_JPEG_file | ( | char * | filename | ) |
Load and decompress JPEG image from disk */.
filename | JPEG file name. |
bool write_JPEG_file | ( | char * | filename, | |
int | quality, | |||
imageRGB | img | |||
) |
Compress image into JPEG, and save it to disk quality must be in range 0-100.
filename | JPEG file name. | |
quality | Quality of JPEG. | |
img | Image in format RGB. |
Definition at line 59 of file jpeg.c.
References imageRGB::data, imageRGB::height, and imageRGB::width.
Referenced by CAPTURE::SaveJPG().
00060 { 00061 00062 int image_width = img.width; 00063 int image_height = img.height; 00064 unsigned char* image = img.data; 00065 int ltrysave = 0; 00066 bool booltrysave = true; 00067 bool boolreturn = false; 00068 00069 00070 JSAMPLE * image_buffer = (JSAMPLE *) image; 00071 struct jpeg_compress_struct cinfo; 00072 struct jpeg_error_mgr jerr; 00073 /* More stuff */ 00074 FILE * outfile; /* target file */ 00075 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ 00076 int row_stride; /* physical row width in image buffer */ 00077 00078 cinfo.err = jpeg_std_error(&jerr); 00079 jpeg_create_compress(&cinfo); 00080 00081 while(booltrysave) 00082 { 00083 ltrysave++; 00084 if ((outfile = fopen(filename, "wb")) == NULL) 00085 { 00086 fprintf(stderr, " > can't open %s %d/4\n", filename,ltrysave); 00087 } 00088 else 00089 booltrysave = false; 00090 00091 if(ltrysave == 4) 00092 return false; 00093 } 00094 00095 jpeg_stdio_dest(&cinfo, outfile); 00096 00097 cinfo.image_width = image_width; /* image width and height, in pixels */ 00098 cinfo.image_height = image_height; 00099 cinfo.input_components = 3; /* # of color components per pixel */ 00100 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ 00101 jpeg_set_defaults(&cinfo); 00102 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); 00103 00104 jpeg_start_compress(&cinfo, TRUE); 00105 00106 row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */ 00107 00108 while (cinfo.next_scanline < cinfo.image_height) 00109 { 00110 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride]; 00111 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); 00112 } 00113 00114 jpeg_finish_compress(&cinfo); 00115 fclose(outfile); 00116 jpeg_destroy_compress(&cinfo); 00117 00118 return true; 00119 00120 }