JunkBox_Lib  1.10.2
png_tool.c
Go to the documentation of this file.
1 
11 #include "png_tool.h"
12 #include "jbxl_state.h"
13 
14 
15 #ifdef ENABLE_PNG
16 
17 
33 PNGImage read_png_file(const char* fname)
34 {
35  PNGImage png;
36  memset(&png, 0, sizeof(PNGImage));
37 
38  FILE* fp = fopen(fname, "rb");
39  if (fp==NULL) {
41  return png;
42  }
43 
44  // ヘッダチェック
45  unsigned char png_sig[PNG_SIGNATURE_SIZE];
46  int sz = fread(png_sig, 1, PNG_SIGNATURE_SIZE, fp);
47  if (sz!=PNG_SIGNATURE_SIZE || png_sig_cmp(png_sig, 0, PNG_SIGNATURE_SIZE)) {
49  return png;
50  }
51 
52  png_structp strct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
53  if (strct==NULL) {
54  png.state = JBXL_ERROR;
55  return png;
56  }
57  png_infop info = png_create_info_struct(strct);
58  if (info==NULL) {
59  png_destroy_read_struct(&strct, NULL, NULL);
60  strct = NULL;
61  png.state = JBXL_ERROR;
62  return png;
63  }
64 
65  png_init_io(strct, fp);
66  png_set_sig_bytes(strct, PNG_SIGNATURE_SIZE);
67  png_read_png(strct, info, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_16, NULL);
68 
69  png.xs = png_get_image_width(strct, info);
70  png.ys = png_get_image_height(strct, info);
71  png.type = png_get_color_type(strct, info);
72 
73  if (png.type == PNG_COLOR_TYPE_GRAY) png.col = 1;
74  else if (png.type == PNG_COLOR_TYPE_GA) png.col = 2;
75  else if (png.type == PNG_COLOR_TYPE_RGB) png.col = 3;
76  else if (png.type == PNG_COLOR_TYPE_RGBA) png.col = 4;
77  else {
78  png_destroy_read_struct(&strct, &info, NULL);
80  return png;
81  }
82  int length = png.xs*png.ys*png.col;
83 
84  png.gp = (uByte*)malloc(length);
85  if (png.gp==NULL) {
86  png_destroy_read_struct(&strct, &info, NULL);
88  return png;
89  }
90 
91  uByte** datap = png_get_rows(strct, info);
92  int len = png.xs*png.col;
93  for (int j=0; j<png.ys; j++) {
94  memcpy(png.gp + j*len, datap[j], len);
95  }
96 
97  png_destroy_read_struct(&strct, &info, NULL);
98  png.state = JBXL_NORMAL;
99 
100  return png;
101 }
102 
103 
120 int write_png_file(const char* fname, PNGImage* png)
121 {
122  if (fname==NULL) return JBXL_GRAPH_IVDARG_ERROR;
123 
124  FILE* fp = fopen(fname, "wb");
125  if (fp==NULL) return JBXL_GRAPH_OPFILE_ERROR;
126  if (png->state!=JBXL_NORMAL || png->gp==NULL) return JBXL_GRAPH_NODATA_ERROR;
127  //
128  png_structp strct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
129  if (strct==NULL) return JBXL_ERROR;
130 
131  png_infop info = png_create_info_struct(strct);
132  if (info==NULL) {
133  png_destroy_write_struct(&strct, NULL);
134  return JBXL_ERROR;
135  }
136 
137  if (png->col==1) png->type = PNG_COLOR_TYPE_GRAY;
138  else if (png->col==2) png->type = PNG_COLOR_TYPE_GA;
139  else if (png->col==3) png->type = PNG_COLOR_TYPE_RGB;
140  else if (png->col==4) png->type = PNG_COLOR_TYPE_RGBA;
141  else {
142  png_destroy_write_struct(&strct, &info);
144  }
145 
146  png_init_io(strct, fp);
147  png_set_IHDR(strct, info, png->xs, png->ys, 8, png->type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
148  uByte** datap = (uByte**)png_malloc(strct, sizeof(uByte*) * png->ys);
149  if (datap==NULL) {
150  png_destroy_write_struct(&strct, &info);
152  }
153  png_set_rows(strct, info, datap);
154 
155  int len = png->xs*png->col;
156  for (int j=0; j<png->ys; j++) {
157  datap[j] = (uByte*)malloc(len);
158  if (datap[j]==NULL) {
159  for (int i=0; i<j; i++) png_free(strct, datap[i]);
160  png_free(strct, datap);
161  png_destroy_write_struct(&strct, &info);
163  }
164  memcpy(datap[j], png->gp + j*len, len);
165  }
166  //
167  png_write_png(strct, info, PNG_TRANSFORM_IDENTITY, NULL);
168 
169  for (int j=0; j<png->ys; j++) png_free(strct, datap[j]);
170  png_free(strct, datap);
171  png_destroy_write_struct(&strct, &info);
172  return 0;
173 }
174 
175 
182 {
183  WSGraph vp = make_WSGraph(png.xs, png.ys, png.col);
184  if (vp.state!=JBXL_NORMAL || vp.gp==NULL) return vp;
185 
186  int i, j, k;
187  for (k=0; k<png.col; k++) {
188  int kp = k*png.xs*png.ys;
189  for (j=0; j<png.ys; j++) {
190  int jp = j*png.xs;
191  int ip = jp + kp;
192  int cp = jp*png.col + k;
193  for (i=0; i<png.xs; i++) {
194  //vp.gp[k*png.xs*png.ys + j*png.xs + i] = png.gp[png.col(j*png.xs + i) + k];
195  vp.gp[ip + i] = (sWord)png.gp[cp + png.col*i];
196  }
197  }
198  }
199  return vp;
200 }
201 
202 
209 {
210  BSGraph vp = make_BSGraph(png.xs, png.ys, png.col);
211  if (vp.state!=JBXL_NORMAL || vp.gp==NULL) return vp;
212 
213  int i, j, k;
214  for (k=0; k<png.col; k++) {
215  int kp = k*png.xs*png.ys;
216  for (j=0; j<png.ys; j++) {
217  int jp = j*png.xs;
218  int ip = jp + kp;
219  int cp = jp*png.col + k;
220  for (i=0; i<png.xs; i++) {
221  vp.gp[ip + i] = (uByte)png.gp[cp + png.col*i];
222  }
223  }
224  }
225  return vp;
226 }
227 
228 
233 {
234  PNGImage png;
235  memset(&png, 0, sizeof(PNGImage));
236 
237  png.xs = vp.xs;
238  png.ys = vp.ys;
239  png.col = vp.zs;
240  int length = png.xs*png.ys*png.col;
241 
242  if (vp.zs==1) png.type = PNG_COLOR_TYPE_GRAY;
243  else if (vp.zs==2) png.type = PNG_COLOR_TYPE_GA;
244  else if (vp.zs==3) png.type = PNG_COLOR_TYPE_RGB;
245  else if (vp.zs==4) png.type = PNG_COLOR_TYPE_RGBA;
246  else {
247  memset(&png, 0, sizeof(PNGImage));
249  return png;
250  }
251  png.gp = (uByte*)malloc(sizeof(uByte)*length);
252  if (png.gp==NULL) {
253  memset(&png, 0, sizeof(PNGImage));
255  return png;
256  }
257 
258  int i, j, k;
259  for (k=0; k<png.col; k++) {
260  int kp = k*png.xs*png.ys;
261  for (j=0; j<png.ys; j++) {
262  int jp = j*png.xs;
263  int ip = jp + kp;
264  int cp = jp*png.col + k;
265  for (i=0; i<png.xs; i++) {
266  png.gp[cp + png.col*i] = (uByte)vp.gp[ip + i];
267  }
268  }
269  }
270  png.state = JBXL_NORMAL;
271  return png;
272 }
273 
274 
279 {
280  PNGImage png;
281  memset(&png, 0, sizeof(PNGImage));
282 
283  png.xs = vp.xs;
284  png.ys = vp.ys;
285  png.col = vp.zs;
286  int length = png.xs*png.ys*png.col;
287 
288  if (vp.zs==1) png.type = PNG_COLOR_TYPE_GRAY;
289  else if (vp.zs==2) png.type = PNG_COLOR_TYPE_GA;
290  else if (vp.zs==3) png.type = PNG_COLOR_TYPE_RGB;
291  else if (vp.zs==4) png.type = PNG_COLOR_TYPE_RGBA;
292  else {
293  memset(&png, 0, sizeof(PNGImage));
295  return png;
296  }
297 
298  png.gp = (uByte*)malloc(sizeof(uByte)*length);
299  if (png.gp==NULL) {
300  memset(&png, 0, sizeof(PNGImage));
302  return png;
303  }
304 
305  int i, j, k;
306  for (k=0; k<png.col; k++) {
307  int kp = k*png.xs*png.ys;
308  for (j=0; j<png.ys; j++) {
309  int jp = j*png.xs;
310  int ip = jp + kp;
311  int cp = jp*png.col + k;
312  for (i=0; i<png.xs; i++) {
313  png.gp[cp + png.col*i] = vp.gp[ip + i];
314  }
315  }
316  }
317  png.state = JBXL_NORMAL;
318  return png;
319 }
320 
321 
326 {
327  if (png==NULL) return;
328  if (png->gp!=NULL) free(png->gp);
329  memset(png, 0, sizeof(PNGImage));
330 
331  return;
332 }
333 
334 
335 
336 #endif // DISABLE_PNG
337 
short sWord
2Byte
Definition: common.h:335
unsigned char uByte
1Byte
Definition: common.h:332
BSGraph make_BSGraph(int xs, int ys, int zs)
Definition: gdata.c:66
WSGraph make_WSGraph(int xs, int ys, int zs)
Definition: gdata.c:108
JunkBox_Lib 状態ヘッダ
#define JBXL_GRAPH_IVDARG_ERROR
無効な引数
Definition: jbxl_state.h:178
#define JBXL_GRAPH_NODATA_ERROR
データが無い
Definition: jbxl_state.h:171
#define JBXL_ERROR
エラー
Definition: jbxl_state.h:34
#define JBXL_GRAPH_OPFILE_ERROR
ファイルのオープンエラー
Definition: jbxl_state.h:173
#define JBXL_GRAPH_IVDCOLOR_ERROR
無効なカラー指定
Definition: jbxl_state.h:183
#define JBXL_NORMAL
正常
Definition: jbxl_state.h:32
#define JBXL_GRAPH_HEADER_ERROR
画像ヘッダーのエラー
Definition: jbxl_state.h:169
#define JBXL_GRAPH_MEMORY_ERROR
メモリエラー
Definition: jbxl_state.h:170
unsigned char unsigned long * len
Definition: jpeg_tool.h:96
PNGImage WSGraph2PNGImage(WSGraph vp)
Definition: png_tool.c:232
BSGraph PNGImage2BSGraph(PNGImage png)
Definition: png_tool.c:208
PNGImage read_png_file(const char *fname)
Definition: png_tool.c:33
PNGImage BSGraph2PNGImage(BSGraph vp)
Definition: png_tool.c:278
void free_PNGImage(PNGImage *png)
Definition: png_tool.c:325
int write_png_file(const char *fname, PNGImage *png)
Definition: png_tool.c:120
WSGraph PNGImage2WSGraph(PNGImage png)
Definition: png_tool.c:181
PNG TOOL HEADER.
#define PNG_SIGNATURE_SIZE
Definition: png_tool.h:34
Definition: gdata.h:27
int zs
zサイズ. 4Byte. 2Dの場合は 1.
Definition: gdata.h:30
int xs
xサイズ. 4Byte.
Definition: gdata.h:28
int state
状態
Definition: gdata.h:31
uByte * gp
グラフィックデータへのポインタ. xs*ys*zs*1Byte.
Definition: gdata.h:32
int ys
yサイズ. 4Byte.
Definition: gdata.h:29
uByte type
Definition: png_tool.h:44
int xs
Definition: png_tool.h:40
int state
Definition: png_tool.h:43
uByte * gp
Definition: png_tool.h:45
int ys
Definition: png_tool.h:41
int col
Definition: png_tool.h:42
Definition: gdata.h:42
int zs
zサイズ. 4Byte. 2Dの場合は 1.
Definition: gdata.h:45
int xs
xサイズ. 4Byte.
Definition: gdata.h:43
int state
状態
Definition: gdata.h:46
sWord * gp
グラフィックデータへのポインタ. xs*ys*zs*2Byte.
Definition: gdata.h:47
int ys
yサイズ. 4Byte.
Definition: gdata.h:44