47 #include "allheaders.h" 58 static const l_int32 MAX_JP2K_WIDTH = 100000;
59 static const l_int32 MAX_JP2K_HEIGHT = 100000;
84 PROCNAME(
"readHeaderJp2k");
91 return ERROR_INT(
"filename not defined", procName, 1);
94 return ERROR_INT(
"image file not found", procName, 1);
121 PROCNAME(
"freadHeaderJp2k");
128 return ERROR_INT(
"fp not defined", procName, 1);
131 nread = fread(buf, 1,
sizeof(buf), fp);
132 if (nread !=
sizeof(buf))
133 return ERROR_INT(
"read failure", procName, 1);
174 l_int32 format, val,
w,
h, bps, spp, loc, found, windex;
175 l_uint8 ihdr[4] = {0x69, 0x68, 0x64, 0x72};
177 PROCNAME(
"readHeaderMemJp2k");
184 return ERROR_INT(
"data not defined", procName, 1);
186 return ERROR_INT(
"size < 80", procName, 1);
188 if (format != IFF_JP2)
189 return ERROR_INT(
"not jp2 file", procName, 1);
194 return ERROR_INT(
"image parameters not found", procName, 1);
197 L_INFO(
"Beginning of ihdr is at byte %d\n", procName, loc);
200 windex = loc / 4 + 1;
201 if (4 * (windex + 2) + 2 >= size)
202 return ERROR_INT(
"image parameters end are outside of header",
204 val = *((l_uint32 *)data + windex);
205 h = convertOnLittleEnd32(val);
206 val = *((l_uint32 *)data + windex + 1);
207 w = convertOnLittleEnd32(val);
208 val = *((l_uint16 *)data + 2 * (windex + 2));
209 spp = convertOnLittleEnd16(val);
210 bps = *(data + 4 * (windex + 2) + 2) + 1;
211 if (
w > MAX_JP2K_WIDTH ||
h > MAX_JP2K_HEIGHT)
212 return ERROR_INT(
"unrealistically large sizes", procName, 1);
215 if (pbps) *pbps = bps;
216 if (pspp) *pspp = spp;
243 fgetJp2kResolution(FILE *fp,
249 l_uint16 xnum, ynum, xdenom, ydenom;
251 l_uint8 resc[4] = {0x72, 0x65, 0x73, 0x63};
253 l_float64 xres, yres;
255 PROCNAME(
"fgetJp2kResolution");
257 if (pxres) *pxres = 0;
258 if (pyres) *pyres = 0;
259 if (!pxres || !pyres)
260 return ERROR_INT(
"&xres and &yres not both defined", procName, 1);
262 return ERROR_INT(
"stream not opened", procName, 1);
271 L_WARNING(
"image resolution not found\n", procName);
278 ynum = data[loc + 5] << 8 | data[loc + 4];
279 ynum = convertOnLittleEnd16(ynum);
280 ydenom = data[loc + 7] << 8 | data[loc + 6];
281 ydenom = convertOnLittleEnd16(ydenom);
282 xnum = data[loc + 9] << 8 | data[loc + 8];
283 xnum = convertOnLittleEnd16(xnum);
284 xdenom = data[loc + 11] << 8 | data[loc + 10];
285 xdenom = convertOnLittleEnd16(xdenom);
286 yexp = data[loc + 12];
287 xexp = data[loc + 13];
288 yres = ((l_float64)ynum / (l_float64)ydenom) * pow(10.0, (l_float64)yexp);
289 xres = ((l_float64)xnum / (l_float64)xdenom) * pow(10.0, (l_float64)xexp);
292 yres *= (300.0 / 11811.0);
293 xres *= (300.0 / 11811.0);
294 *pyres = (l_int32)(yres + 0.5);
295 *pxres = (l_int32)(xres + 0.5);
l_ok findFileFormatBuffer(const l_uint8 *buf, l_int32 *pformat)
findFileFormatBuffer()
FILE * fopenReadStream(const char *filename)
fopenReadStream()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
l_ok arrayFindSequence(const l_uint8 *data, size_t datalen, const l_uint8 *sequence, size_t seqlen, l_int32 *poffset, l_int32 *pfound)
arrayFindSequence()