00001
00002
00003
00004
00005
00006
00007 #include "Platform/GTAssert.h"
00008 #include "JPEGImage2.h"
00009
00010 JPEGImage2::JPEGImage2(const Image& image)
00011 {
00012 *this = image;
00013 }
00014
00015 JPEGImage2& JPEGImage2::operator=(const Image& src)
00016 {
00017 cameraInfo = src.cameraInfo;
00018 frameNumber = src.frameNumber;
00019 cameraInfo = src.cameraInfo;
00020 colorTable = src.colorTable;
00021
00022 unsigned char srcimage[cameraResolutionHeight_ERS7][cameraResolutionWidth_ERS7][3];
00023 for( int i = 0; i < cameraInfo.resolutionHeight; ++i)
00024 {
00025 for( int j = 0; j < cameraInfo.resolutionWidth; ++j)
00026 {
00027 srcimage[i][j][0] = src.image[i][0][j];
00028 srcimage[i][j][2] = src.image[i][1][j];
00029 srcimage[i][j][1] = src.image[i][2][j];
00030 }
00031 }
00032 jpeg_compress_struct cInfo;
00033 jpeg_error_mgr jem;
00034 cInfo.err = jpeg_std_error(&jem);
00035 jpeg_create_compress(&cInfo);
00036
00037 if(!cInfo.dest)
00038 cInfo.dest = (jpeg_destination_mgr*)
00039 (*cInfo.mem->alloc_small) ((j_common_ptr) &cInfo,JPOOL_PERMANENT,
00040 sizeof(DestDescriptor));
00041 cInfo.dest->init_destination = onDestInit;
00042 cInfo.dest->empty_output_buffer = onDestEmpty;
00043 cInfo.dest->term_destination = onDestTerm;
00044 ((DestDescriptor*) cInfo.dest)->theObject = this;
00045
00046 cInfo.image_width = cameraInfo.resolutionWidth;
00047 cInfo.image_height = cameraInfo.resolutionHeight;
00048 cInfo.input_components = 3;
00049
00050 cInfo.in_color_space = JCS_YCbCr;
00051 cInfo.jpeg_color_space = JCS_RGB;
00052 jpeg_set_defaults(&cInfo);
00053 cInfo.dct_method = JDCT_FASTEST;
00054 jpeg_set_quality(&cInfo,75,true);
00055
00056 jpeg_start_compress(&cInfo,true);
00057
00058 while(cInfo.next_scanline < cInfo.image_height)
00059 {
00060 JSAMPROW rowPointer = const_cast<JSAMPROW>(&srcimage[cInfo.next_scanline][0][0]);
00061 jpeg_write_scanlines(&cInfo,&rowPointer, 1);
00062 }
00063
00064 jpeg_finish_compress(&cInfo);
00065 jpeg_destroy_compress(&cInfo);
00066 return *this;
00067 }
00068
00069 void JPEGImage2::toImage(Image& dest) const
00070 {
00071 dest.cameraInfo = cameraInfo;
00072 dest.frameNumber = frameNumber;
00073
00074 jpeg_decompress_struct cInfo;
00075 jpeg_error_mgr jem;
00076 cInfo.err = jpeg_std_error(&jem);
00077
00078 jpeg_create_decompress(&cInfo);
00079
00080 if(!cInfo.src)
00081 cInfo.src = (jpeg_source_mgr *)
00082 (*cInfo.mem->alloc_small)((j_common_ptr) &cInfo,JPOOL_PERMANENT,
00083 sizeof(jpeg_source_mgr));
00084 cInfo.src->init_source = onSrcIgnore;
00085 cInfo.src->fill_input_buffer = onSrcEmpty;
00086 cInfo.src->skip_input_data = onSrcSkip;
00087 cInfo.src->resync_to_restart = jpeg_resync_to_restart;
00088 cInfo.src->term_source = onSrcIgnore;
00089 cInfo.src->bytes_in_buffer = size;
00090 cInfo.src->next_input_byte = (const JOCTET*) image;
00091
00092 jpeg_read_header(&cInfo,true);
00093 jpeg_start_decompress(&cInfo);
00094
00095
00096 while (cInfo.output_scanline < cInfo.output_height)
00097 {
00098 JSAMPROW rowPointer = (unsigned char*) (cInfo.output_height == (unsigned) cameraResolutionHeight_ERS210
00099 ? &dest.image[cInfo.output_scanline][0][0]
00100 : &dest.image[cInfo.output_scanline]
00101 [0][0]);
00102 (void) jpeg_read_scanlines(&cInfo,&rowPointer,1);
00103 }
00104
00105
00106 jpeg_finish_decompress(&cInfo);
00107 jpeg_destroy_decompress(&cInfo);
00108
00109
00110 if(cInfo.output_height == (unsigned) cameraResolutionHeight_ERS210)
00111 for(unsigned i = 0; i < cInfo.output_height; ++i)
00112 for(unsigned c = 2; c > 0; --c)
00113 memmove(&dest.image[i][c][0],
00114 &dest.image[i][0][c * cameraResolutionWidth_ERS210],
00115 cameraResolutionWidth_ERS210);
00116
00117
00118 for(int y = 0; y < dest.cameraInfo.resolutionHeight; ++y)
00119 for(int c = 3; c < 6; ++c)
00120 memset(&dest.image[y][c][0], 128, dest.cameraInfo.resolutionWidth);
00121 }
00122
00123 void JPEGImage2::onDestInit(j_compress_ptr cInfo)
00124 {
00125 JPEGImage2& image = *((DestDescriptor*) cInfo->dest)->theObject;
00126 cInfo->dest->next_output_byte = (JOCTET*) image.image;
00127 cInfo->dest->free_in_buffer = image.cameraInfo.resolutionWidth * image.cameraInfo.resolutionHeight;
00128 }
00129
00130 int JPEGImage2::onDestEmpty(j_compress_ptr)
00131 {
00132 ASSERT(false);
00133 return false;
00134 }
00135
00136 void JPEGImage2::onDestTerm(j_compress_ptr cInfo)
00137 {
00138 JPEGImage2& image = *((DestDescriptor*) cInfo->dest)->theObject;
00139 image.size = (char*) cInfo->dest->next_output_byte - (char*) image.image;
00140 }
00141
00142 void JPEGImage2::onSrcSkip(j_decompress_ptr,long)
00143 {
00144 }
00145
00146 int JPEGImage2::onSrcEmpty(j_decompress_ptr)
00147 {
00148 ASSERT(false);
00149 return false;
00150 }
00151
00152 void JPEGImage2::onSrcIgnore(j_decompress_ptr)
00153 {
00154 }
00155
00156 Out& operator<<(Out& stream,const JPEGImage2& image)
00157 {
00158 ASSERT(image.size);
00159 stream << image.cameraInfo.resolutionWidth << image.cameraInfo.resolutionHeight << image.frameNumber << image.size;
00160 stream.write(&image.image,image.size);
00161 return stream;
00162 }
00163
00164 In& operator>>(In& stream,JPEGImage2& image)
00165 {
00166 stream >> image.cameraInfo.resolutionWidth >> image.cameraInfo.resolutionHeight >> image.frameNumber >> image.size;
00167 image.setCameraInfo();
00168 stream.read(&image.image,image.size);
00169 return stream;
00170 }