JunkBox_Lib 1.10.1
Loading...
Searching...
No Matches
jp2k_tool2.c File Reference

JP2K TOOL2 with OpenJpeg-2.x. More...

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

Go to the source code of this file.

Functions

void init_jp2k (JP2KImage *jp)
 
void free_jp2k (JP2KImage *jp)
 
void setup_jp2k (JP2KImage *jp)
 
int get_jp2k_format (uByte *buf)
 
JP2KImage read_jp2k_file (const char *fname)
 
JP2KImage read_jp2k_data (const char *fname, int format)
 
BSGraph jp2k_toBSGraph (JP2KImage jp)
 

Detailed Description

Version
1.0
Date
2023 12/20
Author
Fumi.Iseki (C)
Attention
this software is based on OpenJPEG. http://www.openjpeg.org/

Definition in file jp2k_tool2.c.

Function Documentation

◆ free_jp2k()

void free_jp2k ( JP2KImage * jp)

Definition at line 36 of file jp2k_tool2.c.

37{
38 if (jp==NULL) return;
39 //
40 if (jp->image!=NULL) {
41 opj_image_destroy(jp->image);
42 }
43 init_jp2k(jp);
44}
void init_jp2k(JP2KImage *jp)
Definition jp2k_tool2.c:21

References init_jp2k().

Referenced by read_jp2k_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_jp2k_format()

int get_jp2k_format ( uByte * buf)

JPEG 2000のヘッダからファイルの種類を返す.
ただし,ヘッダから JP2K_FMT_JPTであることは判別できないので,注意する.

Parameters
bufヘッダ情報の入った変数.最低12Byte必要.
Returns
ファイル種別 (JP2K_FMT_NONE, JP2K_FMT_JP2, JP2K_FMT_J2K)

Definition at line 102 of file jp2k_tool2.c.

103{
104 int format = JP2K_FMT_NONE;
105
106 if (!memcmp(buf, JP2K_MAGIC_RFC3745_JP2, 12) || !memcmp(buf, JP2K_MAGIC_JP2, 4)) {
107 format = JP2K_FMT_JP2;
108 }
109 else if (!memcmp(buf, JP2K_MAGIC_J2K, 4)) {
110 format = JP2K_FMT_J2K;
111 }
112 return format;
113}
unsigned char ** buf
Definition jpeg_tool.h:96

References buf.

Referenced by read_jp2k_file().

Here is the caller graph for this function:

◆ init_jp2k()

void init_jp2k ( JP2KImage * jp)

Definition at line 21 of file jp2k_tool2.c.

22{
23 if (jp==NULL) return;
24 //
25 jp->xs = 0;
26 jp->ys = 0;
27 jp->ws = 0;
28 jp->hs = 0;
29 jp->col = 0;
30 jp->cmode = GRAPH_COLOR_RGBA;
31 jp->state = JBXL_NORMAL;
32 jp->image = NULL;
33}
#define GRAPH_COLOR_RGBA
Definition gheader.h:283
#define JBXL_NORMAL
正常
Definition jbxl_state.h:32

References GRAPH_COLOR_RGBA, and JBXL_NORMAL.

Referenced by free_jp2k(), read_jp2k_data(), and read_jp2k_file().

Here is the caller graph for this function:

◆ jp2k_toBSGraph()

BSGraph jp2k_toBSGraph ( JP2KImage jp)

Definition at line 249 of file jp2k_tool2.c.

250{
251 BSGraph vp;
252 int i, j, k, yp, yz, zp;
253
254 memset(&vp, 0, sizeof(BSGraph));
255 if (jp.image==NULL || jp.image->comps==NULL) {
257 return vp;
258 }
259
260 vp = make_BSGraph(jp.ws, jp.hs, jp.col);
261 if (vp.gp==NULL) return vp;
262
263 for (k=0; k<jp.col; k++) {
264 zp = k*jp.ws*jp.hs;
265 for (j=0; j<jp.hs; j++) {
266 yp = j*jp.xs;
267 yz = j*jp.ws + zp;
268 for (i=0; i<jp.ws; i++) {
269 vp.gp[yz + i] = (uByte)jp.image->comps[k].data[yp + i]; // as Byte境界
270 }
271 }
272 }
273 return vp;
274}
unsigned char uByte
1Byte
Definition common.h:332
BSGraph make_BSGraph(int xs, int ys, int zs)
Definition gdata.c:66
#define JBXL_GRAPH_NODATA_ERROR
データが無い
Definition jbxl_state.h:171
int xs
xサイズ. 4Byte.
Definition gdata.h:28
int state
状態
Definition gdata.h:31
uByte * gp
グラフィックデータへのポインタ. xs*ys*zs*1Byte.
Definition gdata.h:32

References BSGraph::gp, JBXL_GRAPH_NODATA_ERROR, make_BSGraph(), BSGraph::state, and BSGraph::xs.

Here is the call graph for this function:

◆ read_jp2k_data()

JP2KImage read_jp2k_data ( const char * fname,
int format )

Definition at line 156 of file jp2k_tool2.c.

158{
159 JP2KImage jp;
160 init_jp2k(&jp);
161
162 opj_stream_t* stream = NULL;
163 opj_codec_t* codec = NULL;
164
165 opj_dparameters_t parameters;
166 opj_set_default_decoder_parameters(&parameters);
167
168#if OPENJPEG_VER < JP2K_VER_21
169 stream = opj_stream_create_default_file_stream(fp, 1); // 2.0.0
170#else
171 stream = opj_stream_create_default_file_stream(fname, 1); // 2.1.0
172#endif
173
174 if (stream==NULL){
175 jp.state = JBXL_GRAPH_RDFILE_ERROR;
176 return jp;
177 }
178
179 if (format==JP2K_FMT_J2K) { // JPEG 2000 codestream
180 codec = opj_create_decompress(OPJ_CODEC_J2K);
181 }
182 else if (format==JP2K_FMT_JP2) { // JPEG 2000 compressed image data
183 codec = opj_create_decompress(OPJ_CODEC_JP2);
184 }
185 else if (format==JP2K_FMT_JPT) { // JPEG 2000 JPIP
186 codec = opj_create_decompress(OPJ_CODEC_JPT);
187 }
188 else {
189 PRINT_MESG("ERROR: JBXL::readJPEG2KData: unknown file format!\n");
190 opj_stream_destroy(stream);
191 return jp;
192 }
193
194 if (!opj_setup_decoder(codec, &parameters) ){
195 opj_stream_destroy(stream);
196 opj_destroy_codec(codec);
197 jp.state = JBXL_GRAPH_ERROR;
198 return jp;
199 }
200
201 // allow_partial
202 if (!opj_decoder_set_strict_mode(codec, OPJ_FALSE)) { // #define OPJ_FALSE 0
203 opj_stream_destroy(stream);
204 opj_destroy_codec(codec);
205 jp.state = JBXL_GRAPH_ERROR;
206 return jp;
207 }
208
209 /*
210 if (!opj_codec_set_threads(codec, 1)) {
211 opj_stream_destroy(stream);
212 opj_destroy_codec(codec);
213 jp.state = JBXL_GRAPH_ERROR;
214 return jp;
215 }*/
216
217 if (!opj_read_header(stream, codec, &jp.image)){
218 opj_stream_destroy(stream);
219 opj_destroy_codec(codec);
220 jp.state = JBXL_GRAPH_ERROR;
221 return jp;
222 }
223
224 if (!opj_set_decode_area(codec, jp.image, 0, 0, 0, 0)){
225 opj_stream_destroy(stream);
226 opj_destroy_codec(codec);
227 free_jp2k(&jp);
228 jp.state = JBXL_GRAPH_ERROR;
229 return jp;
230 }
231
232 if (!(opj_decode(codec, stream, jp.image) && opj_end_decompress(codec, stream))) {
233 opj_destroy_codec(codec);
234 opj_stream_destroy(stream);
235 free_jp2k(&jp);
236 jp.state = JBXL_GRAPH_ERROR;
237 return jp;
238 }
239
240 setup_jp2k(&jp);
241
242 opj_stream_destroy(stream);
243 opj_destroy_codec(codec);
244
245 return jp;
246}
#define JBXL_GRAPH_ERROR
GRAPH ライブラリーのエラー
Definition jbxl_state.h:167
#define JBXL_GRAPH_RDFILE_ERROR
ファイルの読み込みエラー
Definition jbxl_state.h:174
void free_jp2k(JP2KImage *jp)
Definition jp2k_tool2.c:36
void setup_jp2k(JP2KImage *jp)
Definition jp2k_tool2.c:47
#define PRINT_MESG
環境依存用の出力関数.print_message()
Definition tools.h:475

References free_jp2k(), init_jp2k(), JBXL_GRAPH_ERROR, JBXL_GRAPH_RDFILE_ERROR, PRINT_MESG, and setup_jp2k().

Referenced by read_jp2k_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_jp2k_file()

JP2KImage read_jp2k_file ( const char * fname)

Definition at line 116 of file jp2k_tool2.c.

117{
118 unsigned char head[12];
119 JP2KImage jp;
120 size_t rs;
121 UNUSED(rs);
122
123 init_jp2k(&jp);
124 //
125 if (fname==NULL) {
126 jp.state = JBXL_GRAPH_IVDARG_ERROR;
127 return jp;
128 }
129
130 FILE* fp = fopen(fname, "rb");
131 if (fp==NULL) {
132 jp.state = JBXL_GRAPH_OPFILE_ERROR;
133 return jp;
134 }
135
136 rs = fread(head, 12, 1, fp);
137 fseek(fp, 0, 0);
138 int format = get_jp2k_format(head);
139 if (format==JP2K_FMT_NONE) format = JP2K_FMT_JPT;
140
141#if OPENJPEG_VER < JP2K_VER_21
142 jp = read_jp2k_data(fp, format);
143 fclose(fp);
144#else
145 fclose(fp);
146 jp = read_jp2k_data(fname, format);
147#endif
148
149 return jp;
150}
#define UNUSED(x)
Definition common.h:264
#define JBXL_GRAPH_IVDARG_ERROR
無効な引数
Definition jbxl_state.h:178
#define JBXL_GRAPH_OPFILE_ERROR
ファイルのオープンエラー
Definition jbxl_state.h:173
int get_jp2k_format(uByte *buf)
Definition jp2k_tool2.c:102
JP2KImage read_jp2k_data(const char *fname, int format)
Definition jp2k_tool2.c:156

References get_jp2k_format(), init_jp2k(), JBXL_GRAPH_IVDARG_ERROR, JBXL_GRAPH_OPFILE_ERROR, read_jp2k_data(), and UNUSED.

Here is the call graph for this function:

◆ setup_jp2k()

void setup_jp2k ( JP2KImage * jp)

Definition at line 47 of file jp2k_tool2.c.

48{
49 if (jp==NULL || jp->image==NULL || jp->image->comps==NULL) return;
50 //
51 jp->xs = jp->image->x1 - jp->image->x0;
52 jp->ys = jp->image->y1 - jp->image->y0;
53
54 int fac = (int)jp->image->comps->factor;
55 jp->ws = (jp->xs + (1<<fac) - 1)>>fac;
56 jp->hs = (jp->ys + (1<<fac) - 1)>>fac;
57
58 jp->col = (int)jp->image->numcomps;
59 /*
60 if (jp->image->color_space==OPJ_CLRSPC_SRGB) { /// 1
61 jp->col = 3;
62 }
63 else if (jp->image->color_space==OPJ_CLRSPC_GRAY) { /// 2
64 jp->col = 1;
65 }
66 else if (jp->image->color_space==OPJ_CLRSPC_SYCC || jp->image->color_space==OPJ_CLRSPC_EYCC) { /// 3, 4
67 jp->col = 3;
68 }
69 else if (jp->image->color_space==OPJ_CLRSPC_CMYK) { /// 5
70 jp->col = 4;
71 }
72 */
73
74 // 設定されないものについては,未対応
75 jp->cmode = GRAPH_COLOR_UNKNOWN;
76#if OPENJPEG_VER < JP2K_VER_25
77 int depth = (int)jp->image->comps->bpp;
78#else
79 int depth = (int)jp->image->comps->prec;
80#endif
81
82 DEBUG_MODE PRINT_MESG("JBXL::JPEG2KIMage::setup_image: INFO: xs = %d, ys = %d, col = %d, depth = %d\n", jp->xs, jp->ys, jp->col, depth);
83 if (depth==0 || depth==8) {
84 if (jp->col==1) jp->cmode = GRAPH_COLOR_GRAY;
85 else if (jp->col==3) jp->cmode = GRAPH_COLOR_RGB;
86 else if (jp->col==4) jp->cmode = GRAPH_COLOR_RGBA;
87 }
88 if (jp->cmode==GRAPH_COLOR_UNKNOWN) {
89 PRINT_MESG("JBXL::JPEG2KIMage::setup_image: unknown color mode: col = %d, depth = %d\n", jp->col, depth);
90 jp->cmode = GRAPH_COLOR_RGBA;
91 }
92}
#define GRAPH_COLOR_UNKNOWN
0x0070
Definition gheader.h:302
#define GRAPH_COLOR_RGB
Definition gheader.h:270
#define GRAPH_COLOR_GRAY
Definition gheader.h:244
#define DEBUG_MODE
Definition tools.h:502

References DEBUG_MODE, GRAPH_COLOR_GRAY, GRAPH_COLOR_RGB, GRAPH_COLOR_RGBA, GRAPH_COLOR_UNKNOWN, and PRINT_MESG.

Referenced by read_jp2k_data().

Here is the caller graph for this function: