ImageFormat_JPG.cpp
Go to the documentation of this file.00001 //*** ImageFormat_JPG.cpp *** 00002 00003 #include "ImageFormat_JPG.h" 00004 #include "Debug.h" 00005 #include "Asset.h" 00006 #include "StandardLibrary.h" 00007 00008 #define WIN32_LEAN_AND_MEAN 00009 #define VC_EXTRALEAN 00010 #include <stdio.h> 00011 #include "jpeglib/jpeglib.h" 00012 00013 //*** Register *** 00014 00015 void ImageFormat_JPG::Register() 00016 { 00017 ImageFormat::RegisterImageFormat(TestAsset,Create); 00018 } 00019 00020 00021 //*** Create *** 00022 00023 ImageFormat* ImageFormat_JPG::Create(const Asset& asset) 00024 { 00025 return new ImageFormat_JPG(asset); 00026 } 00027 00028 00029 //*** TestAsset *** 00030 00031 bool ImageFormat_JPG::TestAsset(const Asset& asset) 00032 { 00033 // Check if the header matches JPG (JFIF) files 00034 if (asset.Open()) 00035 { 00036 unsigned char jpgheader[]={0xff,0xd8,0xff,0xe0}; 00037 unsigned char buffer[4]; 00038 asset.Read(buffer,4); 00039 asset.Close(); 00040 if (MemCmp(buffer,jpgheader,4)==0) 00041 { 00042 return true; 00043 } 00044 } 00045 00046 return false; 00047 } 00048 00049 00050 00051 00052 //*** Constructor *** 00053 00054 ImageFormat_JPG::ImageFormat_JPG(const Asset& asset): 00055 width_(0), 00056 height_(0), 00057 image_(0) 00058 { 00059 int size=asset.GetSize(); 00060 char* data=new char[size]; 00061 asset.Open(); 00062 asset.Read(data,size); 00063 asset.Close(); 00064 00065 /* This struct contains the JPEG decompression parameters and pointers to 00066 * working space (which is allocated as needed by the JPEG library). 00067 */ 00068 struct jpeg_decompress_struct cinfo; 00069 00070 /* More stuff */ 00071 JSAMPARRAY buffer; /* Output row buffer */ 00072 int row_stride; /* physical row width in output buffer */ 00073 00074 00075 /* Step 1: allocate and initialize JPEG decompression object */ 00076 00077 /* We set up the normal JPEG error routines, then override error_exit. */ 00078 struct jpeg_error_mgr jerr; 00079 cinfo.err = jpeg_std_error(&jerr); 00080 00081 /* Now we can initialize the JPEG decompression object. */ 00082 jpeg_create_decompress(&cinfo); 00083 00084 /* Step 2: specify data source (eg, a file) */ 00085 00086 jpeg_memory_src(&cinfo, reinterpret_cast<JOCTET*>(data), size); 00087 00088 /* Step 3: read file parameters with jpeg_read_header() */ 00089 00090 (void) jpeg_read_header(&cinfo, TRUE); 00091 /* We can ignore the return value from jpeg_read_header since 00092 * (a) suspension is not possible with the stdio data source, and 00093 * (b) we passed TRUE to reject a tables-only JPEG file as an error. 00094 * See libjpeg.txt for more info. 00095 */ 00096 00097 /* Step 4: set parameters for decompression */ 00098 00099 /* In this example, we don't need to change any of the defaults set by 00100 * jpeg_read_header(), so we do nothing here. 00101 */ 00102 00103 /* Step 5: Start decompressor */ 00104 00105 (void) jpeg_start_decompress(&cinfo); 00106 /* We can ignore the return value since suspension is not possible 00107 * with the stdio data source. 00108 */ 00109 00110 width_= cinfo.output_width; 00111 height_=cinfo.output_height; 00112 image_=new unsigned int[width_*height_]; 00113 00114 /* We may need to do some setup of our own at this point before reading 00115 * the data. After jpeg_start_decompress() we have the correct scaled 00116 * output image dimensions available, as well as the output colormap 00117 * if we asked for color quantization. 00118 * In this example, we need to make an output work buffer of the right size. 00119 */ 00120 /* JSAMPLEs per row in output buffer */ 00121 row_stride = cinfo.output_width * cinfo.output_components; 00122 /* Make a one-row-high sample array that will go away when done with image */ 00123 buffer = (*cinfo.mem->alloc_sarray) 00124 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); 00125 00126 /* Step 6: while (scan lines remain to be read) */ 00127 /* jpeg_read_scanlines(...); */ 00128 00129 /* Here we use the library's state variable cinfo.output_scanline as the 00130 * loop counter, so that we don't have to keep track ourselves. 00131 */ 00132 int y=0; 00133 while (cinfo.output_scanline < cinfo.output_height) { 00134 /* jpeg_read_scanlines expects an array of pointers to scanlines. 00135 * Here the array is only one element long, but you could ask for 00136 * more than one scanline at a time if that's more convenient. 00137 */ 00138 (void) jpeg_read_scanlines(&cinfo, buffer, 1); 00139 /* Assume put_scanline_someplace wants a pointer and sample count. */ 00140 //put_scanline_someplace(buffer[0], row_stride); 00141 for (int x=0; x<row_stride/3; x++) 00142 { 00143 unsigned char r=(unsigned char)(*buffer)[x*3+0]; 00144 unsigned char g=(unsigned char)(*buffer)[x*3+1]; 00145 unsigned char b=(unsigned char)(*buffer)[x*3+2]; 00146 image_[x+y*width_]=0xff000000 | r<<16 | g<<8 | b; 00147 } 00148 y++; 00149 } 00150 00151 /* Step 7: Finish decompression */ 00152 00153 (void) jpeg_finish_decompress(&cinfo); 00154 /* We can ignore the return value since suspension is not possible 00155 * with the stdio data source. 00156 */ 00157 00158 /* Step 8: Release JPEG decompression object */ 00159 00160 /* This is an important step since it will release a good deal of memory. */ 00161 jpeg_destroy_decompress(&cinfo); 00162 00163 delete[] data; 00164 } 00165 00166 00167 //*** Destructor *** 00168 00169 ImageFormat_JPG::~ImageFormat_JPG() 00170 { 00171 if (image_) 00172 { 00173 delete[] image_; 00174 } 00175 } 00176 00177 00178 //*** GetWidth *** 00179 00180 int ImageFormat_JPG::GetWidth() 00181 { 00182 return width_; 00183 } 00184 00185 00186 //*** GetHeight *** 00187 00188 int ImageFormat_JPG::GetHeight() 00189 { 00190 return height_; 00191 } 00192 00193 00194 //*** GetCelCount *** 00195 00196 int ImageFormat_JPG::GetCelCount() 00197 { 00198 return 1; 00199 } 00200 00201 00202 //*** GetCelDelay *** 00203 00204 float ImageFormat_JPG::GetCelDelay(int celIndex) 00205 { 00206 return 0; 00207 } 00208 00209 00210 //*** CopyPixels *** 00211 00212 void ImageFormat_JPG::CopyPixels(unsigned int* destination) 00213 { 00214 if (!image_) 00215 { 00216 return; 00217 } 00218 00219 MemCpy(destination,image_,width_*height_*sizeof(unsigned int)); 00220 } 00221 00222 00223 //*** Save *** 00224 00225 void ImageFormat_JPG::Save(const Filename& filename, int width, int height, void* data) 00226 { 00227 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
