92 return ERROR_INT(
"filename not defined", __func__, 1);
94 if ((fp = fopenReadStream(filename)) == NULL)
95 return ERROR_INT_1(
"image file not found", filename, __func__, 1);
177l_int32 format, val, w, h, bps, spp, loc, found, index, codec;
178l_uint8 ihdr[4] = {0x69, 0x68, 0x64, 0x72};
184 if (pcodec) *pcodec = 0;
186 return ERROR_INT(
"data not defined", __func__, 1);
188 return ERROR_INT(
"size < 80", __func__, 1);
189 findFileFormatBuffer(data, &format);
190 if (format != IFF_JP2)
191 return ERROR_INT(
"not jp2 file", __func__, 1);
194 if (!memcmp(data,
"\xff\x4f\xff\x51", 4)) {
198 arrayFindSequence(data, size, ihdr, 4, &loc, &found);
200 return ERROR_INT(
"image parameters not found", __func__, 1);
205 L_INFO(
"Beginning of ihdr is at byte %d\n", __func__, loc);
208 if (pcodec) *pcodec = codec;
211 if (size < index + 4 * 3)
212 return ERROR_INT(
"header size is too small", __func__, 1);
213 val = *(l_uint32 *)(data + index);
214 h = convertOnLittleEnd32(val);
215 val = *(l_uint32 *)(data + index + 4);
216 w = convertOnLittleEnd32(val);
217 val = *(l_uint16 *)(data + index + 8);
218 spp = convertOnLittleEnd16(val);
219 bps = *(data + index + 10) + 1;
221 if (size < index + 4 * 9)
222 return ERROR_INT(
"header size is too small", __func__, 1);
223 val = *(l_uint32 *)(data + index);
224 w = convertOnLittleEnd32(val);
225 val = *(l_uint32 *)(data + index + 4);
226 h = convertOnLittleEnd32(val);
227 val = *(l_uint16 *)(data + index + 32);
228 spp = convertOnLittleEnd16(val);
229 bps = *(data + index + 34) + 1;
232 lept_stderr(
"h = %d, w = %d, codec: %s, spp = %d, bps = %d\n", h, w,
237 return ERROR_INT(
"w and h must both be > 0", __func__, 1);
238 if (w > MAX_JP2K_WIDTH || h > MAX_JP2K_HEIGHT)
239 return ERROR_INT(
"unrealistically large sizes", __func__, 1);
240 if (spp != 1 && spp != 3 && spp != 4)
241 return ERROR_INT(
"spp must be in 1, 3 or 4", __func__, 1);
242 if (bps != 8 && bps != 16)
243 return ERROR_INT(
"bps must be 8 or 16", __func__, 1);
246 if (pspp) *pspp = spp;
247 if (pbps) *pbps = bps;
315l_uint16 xnum, ynum, xdenom, ydenom;
317l_uint8 resc[4] = {0x72, 0x65, 0x73, 0x63};
318l_float64 xres, yres, maxres;
320 if (pxres) *pxres = 0;
321 if (pyres) *pyres = 0;
322 if (!pxres || !pyres)
323 return ERROR_INT(
"&xres and &yres not both defined", __func__, 1);
326 arrayFindSequence(data, nbytes, resc, 4, &loc, &found);
328 L_WARNING(
"image resolution not found\n", __func__);
331 if (nbytes < 80 || loc >= nbytes - 13) {
332 L_WARNING(
"image resolution found without enough space\n", __func__);
338 ynum = data[loc + 5] << 8 | data[loc + 4];
339 ynum = convertOnLittleEnd16(ynum);
340 ydenom = data[loc + 7] << 8 | data[loc + 6];
341 ydenom = convertOnLittleEnd16(ydenom);
342 xnum = data[loc + 9] << 8 | data[loc + 8];
343 xnum = convertOnLittleEnd16(xnum);
344 xdenom = data[loc + 11] << 8 | data[loc + 10];
345 xdenom = convertOnLittleEnd16(xdenom);
346 if (ydenom == 0 || xdenom == 0) {
347 L_WARNING(
"bad data: ydenom or xdenom is 0\n", __func__);
350 yexp = data[loc + 12];
351 xexp = data[loc + 13];
352 yres = ((l_float64)ynum / (l_float64)ydenom) * pow(10.0, (l_float64)yexp);
353 xres = ((l_float64)xnum / (l_float64)xdenom) * pow(10.0, (l_float64)xexp);
356 yres *= (300.0 / 11811.0);
357 xres *= (300.0 / 11811.0);
361 if (xres > maxres || yres > maxres) {
362 L_WARNING(
"ridiculously large resolution\n", __func__);
364 *pyres = (l_int32)(yres + 0.5);
365 *pxres = (l_int32)(xres + 0.5);