libpng使用

自己的实现

  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     
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值