JunkBox_Lib  1.10.2
jpeg_tool.c File Reference

JPEG TOOL. More...

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

Go to the source code of this file.

Functions

JPEGImage read_jpeg_file (const char *fname)
 
int write_jpeg_file (const char *fname, JPEGImage *jp, int qulty)
 
int write_jpeg_mem (unsigned char **buf, unsigned long *len, JPEGImage *jp, int qulty)
 
WSGraph JPEGImage2WSGraph (JPEGImage jp)
 
BSGraph JPEGImage2BSGraph (JPEGImage jp)
 
JPEGImage WSGraph2JPEGImage (WSGraph vp)
 
JPEGImage BSGraph2JPEGImage (BSGraph vp)
 
JPEGImage make_JPEGImage (int xs, int ys, int col)
 
void free_JPEGImage (JPEGImage *jp)
 
 METHODDEF (void)
 
 METHODDEF (boolean)
 
 GLOBAL (void)
 

Detailed Description

Version
0.9
Date
2009 3/8
Author
Fumi.Iseki (C)
Attention
this software is based in part on the work of the Independent JPEG Group. http://www.ijg.org/

Definition in file jpeg_tool.c.

Function Documentation

◆ BSGraph2JPEGImage()

JPEGImage BSGraph2JPEGImage ( BSGraph  vp)

JPEGImage BSGraph2JPEGImage(BSGraph vp)

Definition at line 325 of file jpeg_tool.c.

326 {
327  JPEGImage jp;
328  int i, j, k, yp, zp;
329 
330  memset(&jp, 0, sizeof(JPEGImage));
331  if (vp.gp==NULL) {
333  return jp;
334  }
335 
336  jp = make_JPEGImage(vp.xs, vp.ys, vp.zs);
337  if (jp.gp==NULL || jp.img==NULL) return jp;
338 
339  for (k=0; k<vp.zs; k++) {
340  zp = k*vp.xs*vp.ys;
341  for (j=0; j<vp.ys; j++) {
342  yp = zp + j*vp.xs;
343  for (i=0; i<vp.xs; i++) {
344  jp.img[j][i*vp.zs + k] = vp.gp[yp + i];
345  }
346  }
347  }
348 
349  return jp;
350 }
#define JBXL_GRAPH_NODATA_ERROR
データが無い
Definition: jbxl_state.h:171
JPEGImage make_JPEGImage(int xs, int ys, int col)
Definition: jpeg_tool.c:356
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
JSAMPLE * gp
Definition: jpeg_tool.h:53
int state
Definition: jpeg_tool.h:52
JSAMPARRAY img
Definition: jpeg_tool.h:54

References BSGraph::gp, JPEGImage::gp, JPEGImage::img, JBXL_GRAPH_NODATA_ERROR, make_JPEGImage(), JPEGImage::state, BSGraph::xs, BSGraph::ys, and BSGraph::zs.

Here is the call graph for this function:

◆ free_JPEGImage()

void free_JPEGImage ( JPEGImage jp)

void free_JPEGImage(JPEGImage* jp)

Definition at line 396 of file jpeg_tool.c.

397 {
398  if (jp==NULL) return;
399 
400  freeNull(jp->gp);
401  freeNull(jp->img);
402 
403  jp->xs = jp->ys = jp->col = 0;
404  return;
405 }
int xs
Definition: jpeg_tool.h:49
int ys
Definition: jpeg_tool.h:50
int col
Definition: jpeg_tool.h:51
#define freeNull(p)
Definition: tools.h:201

References JPEGImage::col, freeNull, JPEGImage::gp, JPEGImage::img, JPEGImage::xs, and JPEGImage::ys.

◆ GLOBAL()

GLOBAL ( void  )

Definition at line 455 of file jpeg_tool.c.

456 {
457  //memory_dest_ptr dest;
458  my_mem_dest_ptr dest;
459 
460  if (cinfo->dest == NULL) { // first time for this JPEG object?
461  cinfo->dest = (struct jpeg_destination_mgr*)
462  (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT, sizeof(my_mem_destination_mgr));
463  }
464 
465  dest = (my_mem_dest_ptr)cinfo->dest;
466  dest->pub.init_destination = mem_init_destination;
467  dest->pub.empty_output_buffer = mem_empty_output_buffer;
468  dest->pub.term_destination = mem_term_destination;
469  dest->pub.next_output_byte = *buf;
470  dest->pub.free_in_buffer = *len;
471  dest->pub.next_output_byte = dest->buffer = *buf;
472  dest->pub.free_in_buffer = dest->bufsize = *len;
473  dest->outbuffer = buf;
474  dest->outsize = len;
475  dest->newbuffer = NULL;
476 }
unsigned char ** buf
Definition: jpeg_tool.h:96
unsigned char unsigned long * len
Definition: jpeg_tool.h:96
my_mem_destination_mgr * my_mem_dest_ptr
Definition: jpeg_tool.h:71
unsigned char ** outbuffer
Definition: jpeg_tool.h:64
struct jpeg_destination_mgr pub
Definition: jpeg_tool.h:62
unsigned long * outsize
Definition: jpeg_tool.h:65
unsigned char * newbuffer
Definition: jpeg_tool.h:66

References buf, my_mem_destination_mgr::buffer, my_mem_destination_mgr::bufsize, len, my_mem_destination_mgr::newbuffer, my_mem_destination_mgr::outbuffer, my_mem_destination_mgr::outsize, and my_mem_destination_mgr::pub.

◆ JPEGImage2BSGraph()

BSGraph JPEGImage2BSGraph ( JPEGImage  jp)

BSGraph JPEGImage2BSGraph(JPEGImage jp)

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

Definition at line 262 of file jpeg_tool.c.

263 {
264  BSGraph vp;
265  int i, j, k, yp, zp;
266 
267  memset(&vp, 0, sizeof(BSGraph));
268  if (jp.gp==NULL || jp.img==NULL) {
270  return vp;
271  }
272 
273  vp = make_BSGraph(jp.xs, jp.ys, jp.col);
274  if (vp.gp==NULL) return vp;
275 
276  for (k=0; k<jp.col; k++) {
277  zp = k*jp.xs*jp.ys;
278  for (j=0; j<jp.ys; j++) {
279  yp = zp + j*jp.xs;
280  for (i=0; i<jp.xs; i++) {
281  vp.gp[yp + i] = (uByte)(jp.img[j][i*jp.col + k]); // チャンネルに分解
282  }
283  }
284  }
285 
286  vp.state = JBXL_NORMAL;
287  return vp;
288 }
unsigned char uByte
1Byte
Definition: common.h:332
BSGraph make_BSGraph(int xs, int ys, int zs)
Definition: gdata.c:66
#define JBXL_NORMAL
正常
Definition: jbxl_state.h:32
Definition: gdata.h:27
int state
状態
Definition: gdata.h:31

References JPEGImage::col, BSGraph::gp, JPEGImage::gp, JPEGImage::img, JBXL_GRAPH_NODATA_ERROR, JBXL_NORMAL, make_BSGraph(), BSGraph::state, JPEGImage::xs, and JPEGImage::ys.

Here is the call graph for this function:

◆ JPEGImage2WSGraph()

WSGraph JPEGImage2WSGraph ( JPEGImage  jp)

WSGraph JPEGImage2WSGraph(JPEGImage jp)

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

Definition at line 228 of file jpeg_tool.c.

229 {
230  WSGraph vp;
231  int i, j, k, yp, zp;
232 
233  memset(&vp, 0, sizeof(WSGraph));
234  if (jp.gp==NULL || jp.img==NULL) {
236  return vp;
237  }
238 
239  vp = make_WSGraph(jp.xs, jp.ys, jp.col);
240  if (vp.gp==NULL) return vp;
241 
242  for (k=0; k<jp.col; k++) {
243  zp = k*jp.xs*jp.ys;
244  for (j=0; j<jp.ys; j++) {
245  yp = zp + j*jp.xs;
246  for (i=0; i<jp.xs; i++) {
247  vp.gp[yp + i] = (sWord)(jp.img[j][i*jp.col + k]); // チャンネルに分解
248  }
249  }
250  }
251 
252  vp.state = JBXL_NORMAL;
253  return vp;
254 }
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 JPEGImage::col, WSGraph::gp, JPEGImage::gp, JPEGImage::img, JBXL_GRAPH_NODATA_ERROR, JBXL_NORMAL, make_WSGraph(), WSGraph::state, JPEGImage::xs, and JPEGImage::ys.

Here is the call graph for this function:

◆ make_JPEGImage()

JPEGImage make_JPEGImage ( int  xs,
int  ys,
int  col 
)

JPEGImage make_JPEGImage(int xs, int ys, int col)

Definition at line 356 of file jpeg_tool.c.

357 {
358  int j;
359  JPEGImage jp;
360 
361  memset(&jp, 0, sizeof(JPEGImage));
362  if (xs==0 || ys==0) {
364  return jp;
365  }
366  if (col<1) col = 3;
367 
368  jp.img = (JSAMPARRAY)malloc(sizeof(JSAMPROW)*ys);
369  if (jp.img==NULL) {
371  return jp;
372  }
373 
374  jp.gp = (JSAMPLE*)malloc(sizeof(JSAMPLE)*col*xs*ys);
375  if (jp.gp==NULL) {
376  freeNull(jp.img);
378  return jp;
379  }
380 
381  for (j=0; j<ys; j++) {
382  jp.img[j] = (JSAMPROW)&jp.gp[j*col*xs];
383  }
384 
385  jp.xs = xs;
386  jp.ys = ys;
387  jp.col = col;
388 
389  return jp;
390 }
#define JBXL_GRAPH_IVDARG_ERROR
無効な引数
Definition: jbxl_state.h:178
#define JBXL_GRAPH_MEMORY_ERROR
メモリエラー
Definition: jbxl_state.h:170

References JPEGImage::col, freeNull, JPEGImage::gp, JPEGImage::img, JBXL_GRAPH_IVDARG_ERROR, JBXL_GRAPH_MEMORY_ERROR, JPEGImage::state, JPEGImage::xs, and JPEGImage::ys.

Referenced by BSGraph2JPEGImage(), read_jpeg_file(), and WSGraph2JPEGImage().

Here is the caller graph for this function:

◆ METHODDEF() [1/2]

METHODDEF ( boolean  )

Definition at line 421 of file jpeg_tool.c.

422 {
423  size_t nextsize;
424  JOCTET * nextbuffer;
425  my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
426 
427  nextsize = dest->bufsize * 2;
428  nextbuffer = (JOCTET*)malloc(nextsize);
429 
430  if (nextbuffer == NULL) return FALSE;
431 
432  memcpy(nextbuffer, dest->buffer, dest->bufsize);
433 
434  if (dest->newbuffer != NULL) free(dest->newbuffer);
435 
436  dest->newbuffer = nextbuffer;
437  dest->pub.next_output_byte = nextbuffer + dest->bufsize;
438  dest->pub.free_in_buffer = dest->bufsize;
439  dest->buffer = nextbuffer;
440  dest->bufsize = nextsize;
441 
442  return TRUE;
443 }
#define TRUE
Definition: common.h:226
#define FALSE
Definition: common.h:223

References my_mem_destination_mgr::buffer, my_mem_destination_mgr::bufsize, FALSE, my_mem_destination_mgr::newbuffer, my_mem_destination_mgr::pub, and TRUE.

◆ METHODDEF() [2/2]

METHODDEF ( void  )

Definition at line 415 of file jpeg_tool.c.

416 {
417  UNUSED(cinfo);
418 }
#define UNUSED(x)
Definition: common.h:264

References UNUSED.

◆ read_jpeg_file()

JPEGImage read_jpeg_file ( const char *  fname)

JPEGImage read_jpeg_file(const char* fname)

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

Parameters
fname読み込むファイル名
Returns
JPEGImage データ.gp==NULL の場合,state に情報が入る.
Return values
JBXL_GRAPH_OPFILE_ERRORstate: ファイルオープンエラー
JBXL_GRAPH_HEADER_ERRORstate: 不正ファイル(JPEGファイルでない?)
JBXL_GRAPH_MEMORY_ERRORstate: メモリエラー

Definition at line 31 of file jpeg_tool.c.

32 {
33  JPEGImage jp;
34  int xs, ys, col;
35  FILE* fp;
36 
37  struct jpeg_decompress_struct jdat;
38  struct jpeg_error_mgr jerr;
39 
40  memset(&jp, 0, sizeof(JPEGImage));
41 
42  jdat.err = jpeg_std_error(&jerr);
43  jpeg_create_decompress(&jdat);
44 
45  // エラーハンドラ
46 /* jdat.client_data = "Client Data";
47  jerr.error_exit = jpeg_error_exit;
48  //jerr.output_message = print_message;
49 */
50  fp = fopen(fname, "rb");
51  if (fp==NULL) {
53  return jp;
54  }
55  jpeg_stdio_src(&jdat, fp);
56  jpeg_read_header(&jdat, TRUE);
57  jpeg_start_decompress(&jdat);
58 
59  xs = jdat.output_width;
60  ys = jdat.output_height;
61  col = jdat.output_components;
62  if (xs<=0 || ys<=0 || col<=0) {
63  jpeg_destroy_decompress(&jdat);
64  fclose(fp);
66  return jp;
67  }
68 
69  jp = make_JPEGImage(xs, ys, col);
70  if (jp.gp==NULL) {
71  jpeg_destroy_decompress(&jdat);
72  fclose(fp);
74  return jp;
75  }
76 
77  int rmn = jdat.output_height;
78  while(rmn>0) {
79  jpeg_read_scanlines(&jdat, jp.img+jdat.output_scanline, rmn);
80  rmn = jdat.output_height - jdat.output_scanline;
81  }
82  jpeg_finish_decompress (&jdat);
83  jpeg_destroy_decompress(&jdat);
84 
85  fclose(fp);
86 
87  return jp;
88 }
#define JBXL_GRAPH_OPFILE_ERROR
ファイルのオープンエラー
Definition: jbxl_state.h:173
#define JBXL_GRAPH_HEADER_ERROR
画像ヘッダーのエラー
Definition: jbxl_state.h:169

References JPEGImage::gp, JPEGImage::img, JBXL_GRAPH_HEADER_ERROR, JBXL_GRAPH_MEMORY_ERROR, JBXL_GRAPH_OPFILE_ERROR, make_JPEGImage(), JPEGImage::state, and TRUE.

Here is the call graph for this function:

◆ write_jpeg_file()

int write_jpeg_file ( const char *  fname,
JPEGImage jp,
int  qulty 
)

int write_jpeg_file(const char* fname, JPEGImage* jp, int qulty)

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

Parameters
fnameファイル名
jp保存する JPEGデータ
qulty保存のクオリティ 0-100 100が最高画質
Return values
0正常終了
JBXL_GRAPH_OPFILE_ERRORファイルオープンエラー
JBXL_GRAPH_HEADER_ERROR不正ファイル(JPEGファイルでない?)
JBXL_GRAPH_MEMORY_ERRORメモリエラー
JBXL_GRAPH_NODATA_ERRORjp にデータが無い
JBXL_GRAPH_IVDARH_ERRORファイル名が NULL
JBXL_GRAPH_IVDCOLOR_ERRORサポート外のチャンネル数(現在の所チャンネル数は 1か 3のみをサポート)

Definition at line 108 of file jpeg_tool.c.

109 {
110  FILE* fp;
111  struct jpeg_compress_struct jdat;
112  struct jpeg_error_mgr jerr;
113 
114 
115  if (fname==NULL) return JBXL_GRAPH_IVDARG_ERROR;
116  if (jp->col!=1 && jp->col!=3) return JBXL_GRAPH_IVDCOLOR_ERROR;
117  if (jp->gp==NULL || jp->img==NULL) return JBXL_GRAPH_NODATA_ERROR;
118 
119  if (qulty>100) qulty = 100;
120  else if (qulty<0) qulty = 0;
121 
122 
123  fp = fopen(fname, "wb");
124  if (fp==NULL) {
126  }
127 
128  jdat.err = jpeg_std_error(&jerr);
129  jpeg_create_compress(&jdat);
130 
131  // エラーハンドラ
132 /* jdat.client_data = "Client Data";
133  jerr.error_exit = jpeg_error_exit;
134  //jerr.output_message = print_message;
135 */
136  jpeg_stdio_dest(&jdat, fp);
137 
138  jdat.image_width = jp->xs;
139  jdat.image_height = jp->ys;
140  jdat.input_components = jp->col;
141  if (jp->col==1) jdat.in_color_space = JCS_GRAYSCALE;
142  else jdat.in_color_space = JCS_RGB;
143 
144  jpeg_set_quality (&jdat, qulty, TRUE);
145  jpeg_set_defaults(&jdat);
146 
147  jpeg_start_compress (&jdat, TRUE);
148  jpeg_write_scanlines(&jdat, jp->img, jp->ys);
149  jpeg_finish_compress(&jdat);
150 
151  jpeg_destroy_compress(&jdat);
152  fclose(fp);
153 
154  return 0;
155 }
#define JBXL_GRAPH_IVDCOLOR_ERROR
無効なカラー指定
Definition: jbxl_state.h:183

References JPEGImage::col, JPEGImage::gp, JPEGImage::img, JBXL_GRAPH_IVDARG_ERROR, JBXL_GRAPH_IVDCOLOR_ERROR, JBXL_GRAPH_NODATA_ERROR, JBXL_GRAPH_OPFILE_ERROR, TRUE, JPEGImage::xs, and JPEGImage::ys.

◆ write_jpeg_mem()

int write_jpeg_mem ( unsigned char **  buf,
unsigned long *  len,
JPEGImage jp,
int  qulty 
)

int write_jpeg_mem(unsigned char** buf, unsigned long* len, JPEGImage* jp, int qulty)

jp の画像データを *bufに書き出す.*bufは要 free

Parameters
[out]buf画像データが格納される.要 free
[out]lenbuf の長さ(Byte)が格納される.
jp保存する JPEGデータ
qulty保存のクオリティ 0〜100 100が最高画質
Return values
JBXL_GRAPH_OPFILE_ERRORファイルオープンエラー
JBXL_GRAPH_HEADER_ERROR不正ファイル(JPEGファイルでない?)
JBXL_GRAPH_MEMORY_ERRORメモリエラー
JBXL_GRAPH_NODATA_ERRORjp にデータが無い
JBXL_GRAPH_IVDARG_ERRORbuf が NULL
JBXL_GRAPH_IVDCOLOR_ERRORサポート外のチャンネル数(現在の所チャンネル数は 1か 3のみをサポート)

Definition at line 175 of file jpeg_tool.c.

176 {
177  struct jpeg_compress_struct jdat;
178  struct jpeg_error_mgr jerr;
179 
180  if (buf==NULL || len==NULL) return JBXL_GRAPH_IVDARG_ERROR;
181  if (jp->col!=1 && jp->col!=3) return JBXL_GRAPH_IVDCOLOR_ERROR;
182  if (jp->gp==NULL || jp->img==NULL) return JBXL_GRAPH_NODATA_ERROR;
183 
184  *len = jp->xs*jp->ys*jp->col;
185  if (*len<=0) return JBXL_GRAPH_IVDARG_ERROR;
186 
187  if (qulty>100) qulty = 100;
188  else if (qulty<0) qulty = 0;
189 
190  *buf = (unsigned char*)malloc(*len);
191  if (*buf==NULL) {
193  }
194 
195  jdat.err = jpeg_std_error(&jerr);
196  jpeg_create_compress(&jdat);
197 
198  jpeg_mem_dest(&jdat, buf, len);
199 
200  jdat.image_width = jp->xs;
201  jdat.image_height = jp->ys;
202  jdat.input_components = jp->col;
203  if (jp->col==1) jdat.in_color_space = JCS_GRAYSCALE;
204  else jdat.in_color_space = JCS_RGB;
205 
206  jpeg_set_quality (&jdat, qulty, TRUE);
207  jpeg_set_defaults(&jdat);
208 
209  jpeg_start_compress (&jdat, TRUE);
210  jpeg_write_scanlines(&jdat, jp->img, jp->ys);
211  jpeg_finish_compress(&jdat);
212  jpeg_destroy_compress(&jdat);
213 
214  if (*len<=0) {
215  freeNull(*buf);
216  return JBXL_GRAPH_ERROR;
217  }
218 
219  return 0;
220 }
#define JBXL_GRAPH_ERROR
GRAPH ライブラリーのエラー
Definition: jbxl_state.h:167

References buf, JPEGImage::col, freeNull, JPEGImage::gp, JPEGImage::img, JBXL_GRAPH_ERROR, JBXL_GRAPH_IVDARG_ERROR, JBXL_GRAPH_IVDCOLOR_ERROR, JBXL_GRAPH_MEMORY_ERROR, JBXL_GRAPH_NODATA_ERROR, len, TRUE, JPEGImage::xs, and JPEGImage::ys.

◆ WSGraph2JPEGImage()

JPEGImage WSGraph2JPEGImage ( WSGraph  vp)

JPEGImage WSGraph2JPEGImage(WSGraph vp)

Definition at line 294 of file jpeg_tool.c.

295 {
296  JPEGImage jp;
297  int i, j, k, yp, zp;
298 
299  memset(&jp, 0, sizeof(JPEGImage));
300  if (vp.gp==NULL) {
302  return jp;
303  }
304 
305  jp = make_JPEGImage(vp.xs, vp.ys, vp.zs);
306  if (jp.gp==NULL || jp.img==NULL) return jp;
307 
308  for (k=0; k<vp.zs; k++) {
309  zp = k*vp.xs*vp.ys;
310  for (j=0; j<vp.ys; j++) {
311  yp = zp + j*vp.xs;
312  for (i=0; i<vp.xs; i++) {
313  jp.img[j][i*vp.zs + k] = vp.gp[yp + i];
314  }
315  }
316  }
317 
318  return jp;
319 }
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 WSGraph::gp, JPEGImage::gp, JPEGImage::img, JBXL_GRAPH_NODATA_ERROR, make_JPEGImage(), JPEGImage::state, WSGraph::xs, WSGraph::ys, and WSGraph::zs.

Here is the call graph for this function: