JunkBox_Lib  1.10.2
png_tool.c File Reference

PNG TOOL. More...

#include "png_tool.h"
#include "jbxl_state.h"
Include dependency graph for png_tool.c:

Go to the source code of this file.

Functions

PNGImage read_png_file (const char *fname)
 
int write_png_file (const char *fname, PNGImage *png)
 
WSGraph PNGImage2WSGraph (PNGImage png)
 
BSGraph PNGImage2BSGraph (PNGImage png)
 
PNGImage WSGraph2PNGImage (WSGraph vp)
 
PNGImage BSGraph2PNGImage (BSGraph vp)
 
void free_PNGImage (PNGImage *png)
 

Detailed Description

Version
0.9
Date
2024 7/19
Author
Fumi.Iseki (C)
Attention

Definition in file png_tool.c.

Function Documentation

◆ BSGraph2PNGImage()

PNGImage BSGraph2PNGImage ( BSGraph  vp)

PNGImage BSGraph2PNGImage(BSGraph vp)

Definition at line 278 of file png_tool.c.

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 }
unsigned char uByte
1Byte
Definition: common.h:332
#define JBXL_GRAPH_IVDCOLOR_ERROR
無効なカラー指定
Definition: jbxl_state.h:183
#define JBXL_NORMAL
正常
Definition: jbxl_state.h:32
#define JBXL_GRAPH_MEMORY_ERROR
メモリエラー
Definition: jbxl_state.h:170
int zs
zサイズ. 4Byte. 2Dの場合は 1.
Definition: gdata.h:30
int xs
xサイズ. 4Byte.
Definition: gdata.h:28
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

References PNGImage::col, BSGraph::gp, PNGImage::gp, JBXL_GRAPH_IVDCOLOR_ERROR, JBXL_GRAPH_MEMORY_ERROR, JBXL_NORMAL, PNGImage::state, PNGImage::type, BSGraph::xs, PNGImage::xs, BSGraph::ys, PNGImage::ys, and BSGraph::zs.

◆ free_PNGImage()

void free_PNGImage ( PNGImage png)

void free_PNGImage(PNGImage* png)

Definition at line 325 of file png_tool.c.

326 {
327  if (png==NULL) return;
328  if (png->gp!=NULL) free(png->gp);
329  memset(png, 0, sizeof(PNGImage));
330 
331  return;
332 }

References PNGImage::gp.

◆ PNGImage2BSGraph()

BSGraph PNGImage2BSGraph ( PNGImage  png)

BSGraph PNGImage2BSGraph(PNGImage png)

PNGデータをチャンネル分解して,BSGraphに格納する.

Definition at line 208 of file png_tool.c.

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 }
BSGraph make_BSGraph(int xs, int ys, int zs)
Definition: gdata.c:66
Definition: gdata.h:27
int state
状態
Definition: gdata.h:31

References PNGImage::col, BSGraph::gp, PNGImage::gp, JBXL_NORMAL, make_BSGraph(), BSGraph::state, PNGImage::xs, and PNGImage::ys.

Here is the call graph for this function:

◆ PNGImage2WSGraph()

WSGraph PNGImage2WSGraph ( PNGImage  png)

WSGraph PNGImage2WSGraph(PNGImage png)

PNGデータをチャンネル分解して,WSGraphに格納する.

Definition at line 181 of file png_tool.c.

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 }
short sWord
2Byte
Definition: common.h:335
WSGraph make_WSGraph(int xs, int ys, int zs)
Definition: gdata.c:108
Definition: gdata.h:42
int state
状態
Definition: gdata.h:46
sWord * gp
グラフィックデータへのポインタ. xs*ys*zs*2Byte.
Definition: gdata.h:47

References PNGImage::col, WSGraph::gp, PNGImage::gp, JBXL_NORMAL, make_WSGraph(), WSGraph::state, PNGImage::xs, and PNGImage::ys.

Here is the call graph for this function:

◆ read_png_file()

PNGImage read_png_file ( const char *  fname)

PNGImage read_png_file(const char* fname)

PNGファイルを読み込んで,PNGImage構造体へデータを格納する.

Parameters
fname読み込むファイル名
Returns
PNGImage データ.state に情報が入る.
Return values
JBXL_NORMALstate: 正常終了
JBXL_ERRORstate: 初期化エラー
JBXL_GRAPH_OPFILE_ERRORstate: ファイルオープンエラー
JBXL_GRAPH_HEADER_ERRORstate: 不正ファイル(PNGファイルでない?)
JBXL_GRAPH_IVDCOLOR_ERRORstate: サポート外のチャンネル(カラー)数
JBXL_GRAPH_MEMORY_ERRORstate: メモリエラー

Definition at line 33 of file png_tool.c.

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 }
#define JBXL_ERROR
エラー
Definition: jbxl_state.h:34
#define JBXL_GRAPH_OPFILE_ERROR
ファイルのオープンエラー
Definition: jbxl_state.h:173
#define JBXL_GRAPH_HEADER_ERROR
画像ヘッダーのエラー
Definition: jbxl_state.h:169
unsigned char unsigned long * len
Definition: jpeg_tool.h:96
#define PNG_SIGNATURE_SIZE
Definition: png_tool.h:34

References PNGImage::col, PNGImage::gp, JBXL_ERROR, JBXL_GRAPH_HEADER_ERROR, JBXL_GRAPH_IVDCOLOR_ERROR, JBXL_GRAPH_MEMORY_ERROR, JBXL_GRAPH_OPFILE_ERROR, JBXL_NORMAL, len, PNG_SIGNATURE_SIZE, PNGImage::state, PNGImage::type, PNGImage::xs, and PNGImage::ys.

◆ write_png_file()

int write_png_file ( const char *  fname,
PNGImage png 
)

int write_png_file(const char* fname, PNGImage* png)

png の画像データを fnameに書き出す.

Parameters
fnameファイル名
png保存する PNGデータへのポインタ
Return values
0正常終了
JBXL_GRAPH_OPFILE_ERRORファイルオープンエラー
JBXL_GRAPH_HEADER_ERROR不正ファイル(PNGファイルでない?)
JBXL_GRAPH_MEMORY_ERRORメモリエラー
JBXL_GRAPH_NODATA_ERRORpng にデータが無い
JBXL_GRAPH_IVDARG_ERRORファイル名が NULL
JBXL_GRAPH_IVDCOLOR_ERRORサポート外のチャンネル(カラー)数

Definition at line 120 of file png_tool.c.

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 }
#define JBXL_GRAPH_IVDARG_ERROR
無効な引数
Definition: jbxl_state.h:178
#define JBXL_GRAPH_NODATA_ERROR
データが無い
Definition: jbxl_state.h:171

References PNGImage::col, PNGImage::gp, JBXL_ERROR, JBXL_GRAPH_IVDARG_ERROR, JBXL_GRAPH_IVDCOLOR_ERROR, JBXL_GRAPH_MEMORY_ERROR, JBXL_GRAPH_NODATA_ERROR, JBXL_GRAPH_OPFILE_ERROR, JBXL_NORMAL, len, PNGImage::state, PNGImage::type, PNGImage::xs, and PNGImage::ys.

◆ WSGraph2PNGImage()

PNGImage WSGraph2PNGImage ( WSGraph  vp)

PNGImage WSGraph2PNGImage(WSGraph vp)

Definition at line 232 of file png_tool.c.

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 }
int zs
zサイズ. 4Byte. 2Dの場合は 1.
Definition: gdata.h:45
int xs
xサイズ. 4Byte.
Definition: gdata.h:43
int ys
yサイズ. 4Byte.
Definition: gdata.h:44

References PNGImage::col, WSGraph::gp, PNGImage::gp, JBXL_GRAPH_IVDCOLOR_ERROR, JBXL_GRAPH_MEMORY_ERROR, JBXL_NORMAL, PNGImage::state, PNGImage::type, WSGraph::xs, PNGImage::xs, WSGraph::ys, PNGImage::ys, and WSGraph::zs.