Encoding/PNG.cpp

Sat, 08 Mar 2014 13:55:46 +0200

author
larukedi
date
Sat, 08 Mar 2014 13:55:46 +0200
changeset 401
85c7261e590a
parent 369
47edbbd0ead8
permissions
-rw-r--r--

added LICENSE and README.md files

215
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
1 #include "PNG.h"
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
2
217
2b8f41054f60 * Linux update for PNG encoder
cemkalyoncu
parents: 215
diff changeset
3 #include "../External/PNG/png.h"
2b8f41054f60 * Linux update for PNG encoder
cemkalyoncu
parents: 215
diff changeset
4 #include "../External/PNG/pngstruct.h"
2b8f41054f60 * Linux update for PNG encoder
cemkalyoncu
parents: 215
diff changeset
5 #include <stdexcept>
2b8f41054f60 * Linux update for PNG encoder
cemkalyoncu
parents: 215
diff changeset
6 #include <cstring>
215
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
7
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
8 namespace gge { namespace encoding {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
9 namespace png {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
10 void ReadFile(png_struct_def *p, unsigned char *buf, size_t size) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
11 FileReader *reader = (FileReader*)(p->io_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
12 reader->Buf.read((char*)buf, size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
13 size=(size_t)reader->Buf.gcount();
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
14 if(size>0 && reader->Buf.fail())
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
15 reader->Buf.clear();
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
16 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
17 void WriteFile(png_struct *p, unsigned char *buf, size_t size) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
18 FileWriter *writer = (FileWriter*)(p->io_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
19 writer->Buf.write((char*)buf, size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
20
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
21 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
22 void ReadArray(png_struct *p, unsigned char *buf, size_t size) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
23 ArrayReader *reader = (ArrayReader*)(p->io_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
24 std::memcpy(buf, &reader->Buf[reader->BufPos], size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
25 reader->BufPos += size;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
26 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
27 void WriteArray(png_struct *p, unsigned char *buf, size_t size) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
28 ArrayWriter *writer = (ArrayWriter*)(p->io_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
29 if (size)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
30 {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
31 std::memcpy(&writer->Buf[writer->BufPos], buf, size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
32 writer->BufPos+=size;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
33 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
34 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
35 void ReadVector(png_struct *p, unsigned char *buf, size_t size) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
36 VectorReader *reader = (VectorReader*)(p->io_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
37 size = std::min(size, reader->Buf.size() - reader->BufPos);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
38 if (size)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
39 std::memcpy(buf, &reader->Buf[reader->BufPos], size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
40 reader->BufPos += size;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
41 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
42 void WriteVector(png_struct *p, unsigned char *buf, size_t size) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
43 VectorWriter *writer = (VectorWriter*)(p->io_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
44 if (size)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
45 {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
46 unsigned oldSize = writer->Buf.size();
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
47 writer->Buf.resize(oldSize + size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
48 std::memcpy(&writer->Buf[oldSize], buf, size);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
49 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
50 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
51 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
52
318
53ce968fa91f * JPEG decoding
cemkalyoncu
parents: 317
diff changeset
53 PNG::Info PNG::decode(png::Reader *reader,png::Buffer *buffer) {
215
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
54 Info inf;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
55
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
56 png_structp png_ptr;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
57 png_infop info_ptr;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
58
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
59 unsigned long width,height;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
60 int bit_depth,color_type;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
61
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
62 unsigned char sig[8];
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
63
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
64 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,NULL);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
65 if (!png_ptr)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
66 throw std::runtime_error("Cannot create PNG read struct");
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
67
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
68 info_ptr = png_create_info_struct(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
69 if (!info_ptr) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
70 png_destroy_read_struct(&png_ptr, NULL, NULL);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
71 throw std::runtime_error("Cannot create PNG info struct");
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
72 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
73
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
74 png_set_read_fn(png_ptr, (void*)reader, reader->Read);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
75
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
76 reader->Read(png_ptr, sig, 8);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
77 if (!png_check_sig(sig, 8))
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
78 throw std::runtime_error("PNG signature mismatch");
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
79
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
80 png_set_sig_bytes(png_ptr, 8);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
81 png_read_info(png_ptr, info_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
82
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
83 png_get_IHDR(png_ptr, info_ptr, (png_uint_32*)&width, (png_uint_32*)&height, &bit_depth,
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
84 &color_type, NULL, NULL, NULL);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
85 inf.Width=(int)width;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
86 inf.Height=(int)height;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
87
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
88 if (color_type == PNG_COLOR_TYPE_PALETTE)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
89 png_set_expand(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
90 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
91 png_set_expand(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
92 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
93 png_set_expand(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
94 if (color_type == PNG_COLOR_TYPE_PALETTE)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
95 png_set_palette_to_rgb(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
96 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
97 png_set_expand_gray_1_2_4_to_8(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
98 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
99 png_set_tRNS_to_alpha(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
100 if (bit_depth == 16)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
101 png_set_strip_16(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
102 if (color_type == PNG_COLOR_TYPE_GRAY ||
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
103 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
104 png_set_gray_to_rgb(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
105
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
106 unsigned int i, rowbytes;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
107 unsigned char ** row_pointers=new unsigned char*[height];
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
108
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
109 png_read_update_info(png_ptr, info_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
110
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
111 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
112 int pChannels = (int)png_get_channels(png_ptr, info_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
113
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
114 buffer->Resize(height*rowbytes);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
115 for (i = 0; i < height; ++i)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
116 row_pointers[i] = buffer->Offset(i*rowbytes);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
117
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
118
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
119
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
120 png_read_image(png_ptr, row_pointers);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
121
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
122 png_read_end(png_ptr, NULL);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
123
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
124 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
125
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
126
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
127
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
128 //currently only RGB and RGBA is supported
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
129 if(pChannels==4) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
130 inf.Alpha=true;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
131 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
132 else {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
133 inf.Alpha=false;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
134 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
135
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
136 inf.RowBytes=rowbytes;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
137
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
138
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
139 delete[] row_pointers;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
140
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
141 delete reader;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
142 delete buffer; //this does not delete underlying buffer
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
143
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
144 return inf;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
145 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
146
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
147 void PNG::encode(png::Buffer *buffer,png::Writer *writer, int width, int height) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
148 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
149 if (!png_ptr)
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
150 throw std::runtime_error("Cannot create PNG compressor");
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
151
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
152 png_infop info_ptr = png_create_info_struct(png_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
153 if (!info_ptr) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
154 png_destroy_write_struct(&png_ptr,
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
155 (png_infopp)NULL);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
156 throw std::runtime_error("Cannot create PNG info");
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
157 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
158
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
159 setjmp(png_jmpbuf(png_ptr));
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
160
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
161 png_set_write_fn(png_ptr, (void*)writer, writer->Write, NULL);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
162
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
163 png_set_IHDR(png_ptr, info_ptr, width, height,
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
164 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
165 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
166
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
167
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
168 png_write_info(png_ptr, info_ptr);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
169 Byte **rows=new Byte*[height];
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
170 for(int i=0;i<height;i++) {
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
171 rows[i]=buffer->Offset(i*width*4);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
172 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
173 png_write_image(png_ptr, rows);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
174 png_write_end(png_ptr, NULL);
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
175
317
4c019dacae27 * Memory leak related with ExportPNG is fixed
cemkalyoncu
parents: 217
diff changeset
176 png_destroy_write_struct(&png_ptr, &info_ptr);
4c019dacae27 * Memory leak related with ExportPNG is fixed
cemkalyoncu
parents: 217
diff changeset
177
215
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
178
369
47edbbd0ead8 * Additional precautions in case of errors
cemkalyoncu
parents: 318
diff changeset
179 delete[] rows;
215
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
180 delete writer;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
181 delete buffer;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
182 }
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
183
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
184 PNG Png;
2f248555f7a0 * PNG en/decoder, Image resource is modified to use it
cemkalyoncu
parents:
diff changeset
185 } }

mercurial