自己的实现
1 unsigned int component(png_const_bytep row, png_uint_32 x, unsigned int c, unsigned int bit_depth, unsigned int channels) { 2 png_uint_32 bit_offset_hi = bit_depth * ((x >> 6) * channels); 3 png_uint_32 bit_offset_lo = bit_depth * ((x & 0x3f) * channels + c); 4 5 row = (png_const_bytep)(((PNG_CONST png_byte(*)[8])row) + bit_offset_hi); 6 row += bit_offset_lo >> 3; 7 bit_offset_lo &= 0x07; 8 9 switch (bit_depth) { 10 case 1: 11 return (row[0] >> (7 - bit_offset_lo)) & 0x01; 12 case 2: 13 return (row[0] >> (6 - bit_offset_lo)) & 0x03; 14 case 4: 15 return (row[0] >> (4 - bit_offset_lo)) & 0x0f; 16 case 8: 17 return row[0]; 18 case 16: 19 return (row[0] << 8) + row[1]; 20 default: 21 fprintf(stderr, "pixel_component: invalid bit depth %u\n", bit_depth); 22 return row[0]; 23 } 24 } 25 26 void readPixel(png_const_bytep row, png_uint_32 x, png_uint_32 bit_depth, png_uint_32 color_type, BitmapPixel *pPixel) { 27 unsigned int channels = PNG_COLOR_TYPE_RGB_ALPHA ? 4 : 3; 28 pPixel->r = component(row, x, 0, bit_depth, channels); 29 pPixel->g = component(row, x, 1, bit_depth, channels); 30 pPixel->b = component(row, x, 2, bit_depth, channels); 31 pPixel->a = color_type == PNG_COLOR_TYPE_RGB_ALPHA ? component(row, x, 3, bit_depth, channels) : 0xFF; 32 } 33 34 BitmapData* PNG_ARGB8888(FILE *pngFile) { 35 BitmapData *pData; 36 png_structp png_ptr; 37 png_infop info_ptr; 38 png_bytepp pRow = NULL; 39 png_uint_32 width, height, ystart, xstart, ystep, xstep, px, ppx, py, count; 40 png_int_32 bit_depth, color_type, interlace_method, passes, pass; 41 42 if (!pngFile) { 43 return NULL; 44 } 45 if (!(png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) { 46 LETV_LOGE("ERROR PNG_ARGB8888: out of memory allocating png_struct!\n"); 47 return NULL; 48 } 49