コード:
#include <iostream>
#include <zlib.h>
/*
000000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 >.PNG........IHDR<
000010 00 00 00 10 00 00 00 0c 08 02 00 00 00 e4 85 aa >................<
000020 d6 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 >.....sRGB.......<
000030 00 04 67 41 4d 41 00 00 b1 8f 0b fc 61 05 00 00 >..gAMA......a...<
000040 00 09 70 48 59 73 00 00 12 74 00 00 12 74 01 de >..pHYs...t...t..<
000050 66 1f 78 00 00 00 53 49 44 41 54 28 53 63 78 2b >f.x...SIDAT(Scx+<
000060 a3 42 24 fa ff 89 01 88 40 1a 20 2c fc 88 1a 1a >.B$.....@. ,....<
000070 70 01 88 2c 76 0d ff 31 00 01 0d 58 01 44 16 8b >p..,v..1...X.D..<
000080 06 fc 08 45 03 31 48 69 a3 0f 04 a1 18 83 07 01 >...E.1Hi........<
000090 95 42 48 06 86 45 2f 88 41 28 1a e0 c6 e0 42 e8 >.BH..E/.A(....B.<
0000a0 1a 88 41 40 a5 20 b4 d1 07 00 00 e4 25 2e 43 9b >..A@. ......%.C.<
0000b0 5c 61 00 00 00 00 49 45 4e 44 ae 42 60 82 >\a....IEND.B`.<
0000be
*/
unsigned char png[] = {
0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x0c,0x08,0x02,0x00,0x00,0x00,0xe4,0x85,0xaa,
0xd6,0x00,0x00,0x00,0x01,0x73,0x52,0x47,0x42,0x00,0xae,0xce,0x1c,0xe9,0x00,0x00,
0x00,0x04,0x67,0x41,0x4d,0x41,0x00,0x00,0xb1,0x8f,0x0b,0xfc,0x61,0x05,0x00,0x00,
0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x12,0x74,0x00,0x00,0x12,0x74,0x01,0xde,
0x66,0x1f,0x78,0x00,0x00,0x00,0x53,0x49,0x44,0x41,0x54,0x28,0x53,0x63,0x78,0x2b,
0xa3,0x42,0x24,0xfa,0xff,0x89,0x01,0x88,0x40,0x1a,0x20,0x2c,0xfc,0x88,0x1a,0x1a,
0x70,0x01,0x88,0x2c,0x76,0x0d,0xff,0x31,0x00,0x01,0x0d,0x58,0x01,0x44,0x16,0x8b,
0x06,0xfc,0x08,0x45,0x03,0x31,0x48,0x69,0xa3,0x0f,0x04,0xa1,0x18,0x83,0x07,0x01,
0x95,0x42,0x48,0x06,0x86,0x45,0x2f,0x88,0x41,0x28,0x1a,0xe0,0xc6,0xe0,0x42,0xe8,
0x1a,0x88,0x41,0x40,0xa5,0x20,0xb4,0xd1,0x07,0x00,0x00,0xe4,0x25,0x2e,0x43,0x9b,
0x5c,0x61,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82,
};
struct Img { int real_w, h; };
Img data = { 16, 12 }; // IHDR の次の 0x00000010 と 0x0000000c
Img *img = &data;
int main()
{
//comp_size:圧縮データのサイズ
unsigned long comp_size = 0x53; // IDAT の直前の 0x00000053
//comp_img:圧縮データ配列
unsigned char *comp_img = png + 0x5b; // IDAT の直後のデータ
// IHDR のビット深度が 8、カラータイプが 2 だから、RGB 3バイト
unsigned long output_size = (img->h*img->real_w + img->h) * 3;
unsigned char *output = new unsigned char[output_size];
switch (uncompress(output, &output_size, comp_img, comp_size)) {
case Z_OK:
std::cout << "復号に成功" << std::endl;
break;
case Z_MEM_ERROR:
std::cout << "メモリが不足" << std::endl;
break;
case Z_BUF_ERROR:
std::cout << "出力バッファの不足" << std::endl;
break;
case Z_DATA_ERROR:
std::cout << "入力バッファがおかしい" << std::endl;
break;
default:
std::cout << "その他" << std::endl;
break;
}
}