00056 {
00057
00058
struct jpeg_decompress_struct cinfo;
00059
00060
00061 FILE* inputFile=fopen( fileIn.ascii(),
"rb" );
00062
if(!inputFile)
return false;
00063
00064
00065
struct GVJPEGFatalError jerr;
00066 cinfo.err = jpeg_std_error(&jerr);
00067 cinfo.err->error_exit =
GVJPEGFatalError::handler;
00068
if (setjmp(jerr.
mJmpBuffer))
00069 {
00070 jpeg_destroy_decompress(&cinfo);
00071 fclose(inputFile);
00072
return false;
00073 }
00074
00075
00076 jpeg_create_decompress(&cinfo);
00077 jpeg_stdio_src(&cinfo, inputFile);
00078 jpeg_read_header(&cinfo, TRUE);
00079
00080
00081
int origWidth = (
int)cinfo.image_width;
00082
int origHeight = (
int)cinfo.image_height;
00083
int num = 1;
00084
int denom = 1;
00085
00086
00087
if( origWidth > targetWidth || origHeight > targetHeight )
00088 {
00089
while( denom < 8 &&
00090 ( origWidth / (denom*2) >= targetWidth || origHeight / (denom*2) >= targetHeight )
00091 )
00092 { denom = denom*2; }
00093 }
00094
00095
00096 cinfo.scale_num=num;
00097 cinfo.scale_denom=denom;
00098
00099
00100 jpeg_start_decompress(&cinfo);
00101
00102
switch(cinfo.output_components)
00103 {
00104
00105
case 1:
00106 {
00107 scaledImage.create( cinfo.output_width, cinfo.output_height, 8, 256 );
00108
for (
int i=0; i<256; i++)
00109 {
00110 scaledImage.setColor(i, qRgb(i,i,i));
00111 }
00112 }
00113
break;
00114
00115
00116
case 3:
00117
case 4:
00118 scaledImage.create( cinfo.output_width, cinfo.output_height, 32 );
00119
break;
00120
00121
00122
default:
00123 jpeg_destroy_decompress(&cinfo);
00124 fclose(inputFile);
00125
return false;
00126 }
00127
00128
00129 uchar** lines = scaledImage.jumpTable();
00130
while (cinfo.output_scanline < cinfo.output_height)
00131 {
00132 jpeg_read_scanlines(&cinfo, lines + cinfo.output_scanline, cinfo.output_height);
00133 }
00134 jpeg_finish_decompress(&cinfo);
00135
00136
00137
00138
if ( cinfo.output_components == 1 )
00139 {
00140 scaledImage = scaledImage.convertDepth( 32, Qt::AutoColor );
00141 }
00142
00143
00144
if ( cinfo.output_components == 3 )
00145 {
00146
for (uint j=0; j<cinfo.output_height; j++)
00147 {
00148 uchar *in = scaledImage.scanLine(j) + cinfo.output_width*3;
00149 QRgb *out = (QRgb*)( scaledImage.scanLine(j) );
00150
00151
for (uint i=cinfo.output_width; i--; )
00152 {
00153 in-=3;
00154 out[i] = qRgb(in[0], in[1], in[2]);
00155 }
00156 }
00157 }
00158
00159
00160
if( scaledImage.width() != targetWidth || scaledImage.height() != targetHeight )
00161 {
00162
int clampedTargetWidth = targetWidth;
00163
int clampedTargetHeight = targetHeight;
00164
00165
if(QMIN( ((
float)targetWidth)/origWidth, ((
float)targetHeight)/origHeight ) > 2)
00166 {
00167 clampedTargetWidth = 2*origWidth;
00168 clampedTargetHeight = 2*origHeight;
00169 }
00170
00171 scaledImage = scaledImage.smoothScale(clampedTargetWidth, clampedTargetHeight, QImage::ScaleMin);
00172 }
00173 jpeg_destroy_decompress(&cinfo);
00174 fclose(inputFile);
00175
return true;
00176 }