56{
57
58 struct jpeg_decompress_struct cinfo;
59
60
61 FILE* inputFile=fopen( fileIn.ascii(), "rb" );
62 if(!inputFile) return false;
63
64
66 cinfo.err = jpeg_std_error(&jerr);
68 if (setjmp(jerr.mJmpBuffer))
69 {
70 jpeg_destroy_decompress(&cinfo);
71 fclose(inputFile);
72 return false;
73 }
74
75
76 jpeg_create_decompress(&cinfo);
77 jpeg_stdio_src(&cinfo, inputFile);
78 jpeg_read_header(&cinfo, TRUE);
79
80
81 int origWidth = (int)cinfo.image_width;
82 int origHeight = (int)cinfo.image_height;
83 int num = 1;
84 int denom = 1;
85
86
87 if( origWidth > targetWidth || origHeight > targetHeight )
88 {
89 while( denom < 8 &&
90 ( origWidth / (denom*2) >= targetWidth || origHeight / (denom*2) >= targetHeight )
91 )
92 { denom = denom*2; }
93 }
94
95
96 cinfo.scale_num=num;
97 cinfo.scale_denom=denom;
98
99
100 jpeg_start_decompress(&cinfo);
101
102 switch(cinfo.output_components)
103 {
104
105 case 1:
106 {
107 scaledImage.create( cinfo.output_width, cinfo.output_height, 8, 256 );
108 for (int i=0; i<256; i++)
109 {
110 scaledImage.setColor(i, qRgb(i,i,i));
111 }
112 }
113 break;
114
115
116 case 3:
117 case 4:
118 scaledImage.create( cinfo.output_width, cinfo.output_height, 32 );
119 break;
120
121
122 default:
123 jpeg_destroy_decompress(&cinfo);
124 fclose(inputFile);
125 return false;
126 }
127
128
129 uchar** lines = scaledImage.jumpTable();
130 while (cinfo.output_scanline < cinfo.output_height)
131 {
132 jpeg_read_scanlines(&cinfo, lines + cinfo.output_scanline, cinfo.output_height);
133 }
134 jpeg_finish_decompress(&cinfo);
135
136
137
138 if ( cinfo.output_components == 1 )
139 {
140 scaledImage = scaledImage.convertDepth( 32, Qt::AutoColor );
141 }
142
143
144 if ( cinfo.output_components == 3 )
145 {
146 for (uint j=0; j<cinfo.output_height; j++)
147 {
148 uchar *in = scaledImage.scanLine(j) + cinfo.output_width*3;
149 QRgb *out = (QRgb*)( scaledImage.scanLine(j) );
150
151 for (uint i=cinfo.output_width; i--; )
152 {
153 in-=3;
154 out[i] = qRgb(in[0], in[1], in[2]);
155 }
156 }
157 }
158
159
160 if( scaledImage.width() != targetWidth || scaledImage.height() != targetHeight )
161 {
162 int clampedTargetWidth = targetWidth;
163 int clampedTargetHeight = targetHeight;
164
165 if(QMIN( ((float)targetWidth)/origWidth, ((float)targetHeight)/origHeight ) > 2)
166 {
167 clampedTargetWidth = 2*origWidth;
168 clampedTargetHeight = 2*origHeight;
169 }
170
171 scaledImage = scaledImage.smoothScale(clampedTargetWidth, clampedTargetHeight, Qt::KeepAspectRatio);
172 }
173 jpeg_destroy_decompress(&cinfo);
174 fclose(inputFile);
175 return true;
176}
static void handler(j_common_ptr cinfo)