25void PNGImage::init(
void)
27 xs = ys = col = length = 0;
44bool PNGImage::isNull(
void)
46 if (gp==NULL)
return true;
54void PNGImage::clear(
void)
56 if (gp!=NULL) memset(gp, 0, length);
64void PNGImage::fill(
uByte v)
66 if (gp!=NULL) memset(gp, v, length);
76void PNGImage::free(
void)
78 if (gp!=NULL) ::free(gp);
87void PNGImage::getm(
int x,
int y,
int c)
94 gp = (
uByte*)malloc(length);
102 memset(gp, 0, length);
124void PNGImage::readData(FILE* fp)
133 unsigned char png_sig[PNG_SIGNATURE_SIZE];
134 unsigned int sz = (
unsigned int)fread(png_sig, 1, PNG_SIGNATURE_SIZE, fp);
135 if (sz!=PNG_SIGNATURE_SIZE || png_sig_cmp(png_sig, 0, PNG_SIGNATURE_SIZE)) {
140 strct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
145 info = png_create_info_struct(strct);
147 png_destroy_read_struct(&strct, NULL, NULL);
153 png_init_io(strct, fp);
154 png_set_sig_bytes(strct, PNG_SIGNATURE_SIZE);
155 png_read_png(strct, info, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_16, NULL);
157 xs = png_get_image_width(strct, info);
158 ys = png_get_image_height(strct, info);
159 type = png_get_color_type(strct, info);
161 if (type == PNG_COLOR_TYPE_GRAY) col = 1;
162 else if (type == PNG_COLOR_TYPE_GA) col = 2;
163 else if (type == PNG_COLOR_TYPE_RGB) col = 3;
164 else if (type == PNG_COLOR_TYPE_RGBA) col = 4;
166 png_destroy_read_struct(&strct, &info, NULL);
174 gp = (
uByte*)malloc(length);
176 png_destroy_read_struct(&strct, &info, NULL);
183 uByte** datap = png_get_rows(strct, info);
185 for (
int j=0; j<ys; j++) {
186 memcpy(gp + j*len, datap[j], len);
189 png_destroy_read_struct(&strct, &info, NULL);
212int PNGImage::writeData(FILE* fp)
223 strct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
228 info = png_create_info_struct(strct);
230 png_destroy_write_struct(&strct, NULL);
236 if (col==1) type = PNG_COLOR_TYPE_GRAY;
237 else if (col==2) type = PNG_COLOR_TYPE_GA;
238 else if (col==3) type = PNG_COLOR_TYPE_RGB;
239 else if (col==4) type = PNG_COLOR_TYPE_RGBA;
241 png_destroy_write_struct(&strct, &info);
248 png_init_io(strct, fp);
249 png_set_IHDR(strct, info, xs, ys, 8, type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
252 png_destroy_write_struct(&strct, &info);
258 png_set_rows(strct, info, datap);
261 for (
int j=0; j<ys; j++) {
262 datap[j] = (
uByte*)malloc(len);
263 if (datap[j]==NULL) {
264 for (
int i=0; i<j; i++) png_free(strct, datap[i]);
265 png_free(strct, datap);
266 png_destroy_write_struct(&strct, &info);
272 memcpy(datap[j], gp + j*len, len);
275 png_write_png(strct, info, PNG_TRANSFORM_IDENTITY, NULL);
277 for (
int j=0; j<ys; j++) png_free(strct, datap[j]);
278 png_free(strct, datap);
279 png_destroy_write_struct(&strct, &info);
307PNGImage jbxl::readPNGFile(
const char* fname)
316 FILE* fp = fopen(fname,
"rb");
322 png = readPNGData(fp);
345PNGImage jbxl::readPNGData(FILE* fp)
376int jbxl::writePNGFile(
const char* fname, PNGImage* png)
381 FILE* fp = fopen(fname,
"wb");
385 int ret = writePNGData(fp, png);
407int jbxl::writePNGData(FILE* fp, PNGImage* png)
412 int ret = png->writeData(fp);
#define JBXL_GRAPH_IVDARG_ERROR
無効な引数
#define JBXL_GRAPH_NODATA_ERROR
データが無い
#define JBXL_GRAPH_OPFILE_ERROR
ファイルのオープンエラー
#define JBXL_GRAPH_IVDCOLOR_ERROR
無効なカラー指定
#define JBXL_GRAPH_HEADER_ERROR
画像ヘッダーのエラー
#define JBXL_GRAPH_MEMORY_ERROR
メモリエラー