@@ -27,6 +27,7 @@ THE SOFTWARE.
27
27
// #include <ImageToolKit/IT_ImageLoader.h>
28
28
#include < TG3.h>
29
29
#include " png.h"
30
+ #include " jpeglib.h"
30
31
31
32
// using namespace ImageToolKit;
32
33
using namespace std ;
@@ -39,6 +40,7 @@ typedef struct
39
40
int offset;
40
41
}tImageSource;
41
42
43
+
42
44
// because we do not want to include "png.h" in CCXUIImage_uphone.h, so we implement
43
45
// the function as a static function
44
46
static void pngReadCallback (png_structp png_ptr, png_bytep data, png_size_t length)
@@ -78,10 +80,10 @@ UIImage::UIImage(TBitmap *bitmap)
78
80
m_imageInfo.data = m_pBitmap->GetDataPtr ();
79
81
m_imageInfo.height = m_pBitmap->GetHeight ();
80
82
m_imageInfo.width = m_pBitmap->GetWidth ();
81
- m_imageInfo.hasAlpha = m_pBitmap->HasAlphaData ();
83
+ m_imageInfo.hasAlpha = true ; // m_pBitmap->HasAlphaData();
82
84
// uphone only support predefined
83
85
m_imageInfo.isPremultipliedAlpha = true ;
84
- m_imageInfo.bitsPerComponent = m_pBitmap->GetDepth ();
86
+ m_imageInfo.bitsPerComponent = m_pBitmap->GetDepth () / 4 ;
85
87
}
86
88
}
87
89
@@ -107,10 +109,26 @@ UIImage::~UIImage(void)
107
109
}
108
110
}
109
111
110
- bool UIImage::initWithContentsOfFile (const string &strPath)
112
+ bool UIImage::initWithContentsOfFile (const string &strPath, tImageFormat imageType )
111
113
{
112
- // use libpng load image
113
- return loadPng (strPath.c_str ());
114
+ bool bRet = false ;
115
+
116
+ switch (imageType)
117
+ {
118
+ case kImageFormatPNG :
119
+ // use libpng load image
120
+ bRet = loadPng (strPath.c_str ());
121
+ break ;
122
+ case kImageFormatJPG :
123
+ bRet = loadJpg (strPath.c_str ());
124
+ break ;
125
+ default :
126
+ // unsupported image type
127
+ bRet = false ;
128
+ break ;
129
+ }
130
+
131
+ return bRet;
114
132
}
115
133
116
134
unsigned int UIImage::width (void )
@@ -141,21 +159,14 @@ int UIImage::CGImageGetBitsPerComponent(void)
141
159
return m_imageInfo.bitsPerComponent ;
142
160
}
143
161
144
- // 0 -> it is a mask
145
- // 1 -> other
162
+ // now we only support RGBA8888 or RGB888
163
+ // so it has color space
146
164
int UIImage::CGImageGetColorSpace (void )
147
165
{
148
- int nRet = 1 ;
149
-
150
- if (m_imageInfo.bitsPerComponent == 8 )
151
- {
152
- nRet = 0 ;
153
- }
154
-
155
- return nRet;
166
+ return 1 ;
156
167
}
157
168
158
- unsigned char * UIImage::getRGBA8888Data (void )
169
+ unsigned char * UIImage::getData (void )
159
170
{
160
171
return m_imageInfo.data ;
161
172
}
@@ -263,10 +274,7 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
263
274
m_imageInfo.height = info_ptr->height ;
264
275
m_imageInfo.width = info_ptr->width ;
265
276
m_imageInfo.isPremultipliedAlpha = false ;
266
- // we use CCX_RGA or CCX_RGB to save data
267
- // so the bitsPerComponet is 32, and it also
268
- // has the alpha data
269
- m_imageInfo.bitsPerComponent = 32 ;
277
+ m_imageInfo.bitsPerComponent = info_ptr->bit_depth ;
270
278
m_imageInfo.hasAlpha = true ;
271
279
272
280
// convert to appropriate format, we now only support RGBA8888
@@ -324,6 +332,81 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
324
332
return true ;
325
333
}
326
334
335
+ bool UIImage::loadJpg (const char *strFileName)
336
+ {
337
+ /* these are standard libjpeg structures for reading(decompression) */
338
+ struct jpeg_decompress_struct cinfo;
339
+ struct jpeg_error_mgr jerr;
340
+ /* libjpeg data structure for storing one row, that is, scanline of an image */
341
+ JSAMPROW row_pointer[1 ];
342
+
343
+ FILE *infile = fopen ( strFileName, " rb" );
344
+ unsigned long location = 0 ;
345
+ int i = 0 ;
346
+
347
+ if ( !infile )
348
+ {
349
+ return false ;
350
+ }
351
+ /* jpeg_stdio_src(&cinfo, infile);*/
352
+
353
+ /* here we set up the standard libjpeg error handler */
354
+ cinfo.err = jpeg_std_error ( &jerr );
355
+
356
+ /* setup decompression process and source, then read JPEG header */
357
+ jpeg_create_decompress ( &cinfo );
358
+
359
+ /* this makes the library read from infile */
360
+ jpeg_stdio_src ( &cinfo, infile );
361
+
362
+ /* reading the image header which contains image information */
363
+ jpeg_read_header ( &cinfo, true );
364
+
365
+ // we only support RGB or grayscale
366
+ if (cinfo.jpeg_color_space != JCS_RGB)
367
+ {
368
+ if (cinfo.jpeg_color_space == JCS_GRAYSCALE || cinfo.jpeg_color_space == JCS_YCbCr)
369
+ {
370
+ cinfo.out_color_space = JCS_RGB;
371
+ }
372
+ }
373
+ else
374
+ {
375
+ return false ;
376
+ }
377
+
378
+ /* Start decompression jpeg here */
379
+ jpeg_start_decompress ( &cinfo );
380
+
381
+ /* init image info */
382
+ m_imageInfo.width = cinfo.image_width ;
383
+ m_imageInfo.height = cinfo.image_height ;
384
+ m_imageInfo.hasAlpha = false ;
385
+ m_imageInfo.isPremultipliedAlpha = false ;
386
+ m_imageInfo.bitsPerComponent = 8 ;
387
+ m_imageInfo.data = new unsigned char [cinfo.output_width *cinfo.output_height *cinfo.output_components ];
388
+
389
+ /* now actually read the jpeg into the raw buffer */
390
+ /* row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );*/
391
+ row_pointer[0 ] = new unsigned char [cinfo.output_width *cinfo.output_components ];
392
+
393
+ /* read one scan line at a time */
394
+ while ( cinfo.output_scanline < cinfo.image_height )
395
+ {
396
+ jpeg_read_scanlines ( &cinfo, row_pointer, 1 );
397
+ for ( i=0 ; i<cinfo.image_width *cinfo.num_components ;i++)
398
+ m_imageInfo.data [location++] = row_pointer[0 ][i];
399
+ }
400
+
401
+ /* wrap up decompression, destroy objects, free pointers and close open files */
402
+ jpeg_finish_decompress ( &cinfo );
403
+ jpeg_destroy_decompress ( &cinfo );
404
+ delete row_pointer[0 ];
405
+ fclose ( infile );
406
+
407
+ return true ;
408
+ }
409
+
327
410
bool UIImage::save (const std::string &strFileName, int nFormat)
328
411
{
329
412
// / @todo uiimage::save
0 commit comments