PHPAPI const char php_sig_swc[3] = {'C', 'W', 'S'};
PHPAPI const char php_sig_jpg[3] = {(char) 0xff, (char) 0xd8, (char) 0xff};
PHPAPI const char php_sig_png[8] = {(char) 0x89, (char) 0x50, (char) 0x4e, (char) 0x47,
-(char) 0x0d, (char) 0x0a, (char) 0x1a, (char) 0x0a};
+ (char) 0x0d, (char) 0x0a, (char) 0x1a, (char) 0x0a};
PHPAPI const char php_sig_tif_ii[4] = {'I','I', (char)0x2A, (char)0x00};
PHPAPI const char php_sig_tif_mm[4] = {'M','M', (char)0x00, (char)0x2A};
-PHPAPI const char php_sig_jpc[3] = {(char)0xFF, (char)0x4F, (char)0xff};
+PHPAPI const char php_sig_jpc[3] = {(char)0xff, (char)0x4f, (char)0xff};
+PHPAPI const char php_sig_jp2[12] = {(char)0x00, (char)0x00, (char)0x00, (char)0x0c,
+ (char)0x6a, (char)0x50, (char)0x20, (char)0x20,
+ (char)0x0d, (char)0x0a, (char)0x87, (char)0x0a};
PHPAPI const char php_sig_iff[4] = {'F','O','R','M'};
/* REMEMBER TO ADD MIME-TYPE TO FUNCTION php_image_type_to_mime_type */
REGISTER_LONG_CONSTANT("IMAGETYPE_SWC", IMAGE_FILETYPE_SWC, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("IMAGETYPE_IFF", IMAGE_FILETYPE_IFF, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMAGETYPE_WBMP", IMAGE_FILETYPE_WBMP, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMAGETYPE_JPEG2000",IMAGE_FILETYPE_JPC, CONST_CS | CONST_PERSISTENT); /* keep alias */
+ REGISTER_LONG_CONSTANT("IMAGETYPE_XBM", IMAGE_FILETYPE_XBM, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
/* }}} */
do {
szlength=slength*(1<<factor++);
buf = (char *) erealloc(buf,szlength);
- if (!buf) {
- status = 1;
- break;
- }
status = uncompress(buf, &szlength, bufz, slength);
} while ((status==Z_BUF_ERROR)&&(factor<maxfactor));
length -= 2; /* length includes itself */
buffer = emalloc(length);
- if ( !buffer) return;
if (php_stream_read(stream, buffer, (long) length) <= 0) {
efree(buffer);
if (result == NULL) {
/* handle SOFn block */
result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
- if ( !result)
- return NULL;
length = php_read2(stream TSRMLS_CC);
result->bits = php_stream_getc(stream);
result->height = php_read2(stream TSRMLS_CC);
}
/* }}} */
-/* {{{ jpeg2000 constants
- See ext/exif for more */
-#define JC_SOC 0x4F /* Start of codestream */
-#define JC_SIZ 0x51 /* Image and tile size */
-/* }}} */
-
/* {{{ php_read4
*/
static unsigned int php_read4(php_stream * stream TSRMLS_DC)
}
/* }}} */
-/* {{{ php_handle_tiff
- main loop to parse TIFF structure */
+/* {{{ JPEG 2000 Marker Codes */
+#define JPEG2000_MARKER_PREFIX 0xFF /* All marker codes start with this */
+#define JPEG2000_MARKER_SOC 0x4F /* Start of Codestream */
+#define JPEG2000_MARKER_SOT 0x90 /* Start of Tile part */
+#define JPEG2000_MARKER_SOD 0x93 /* Start of Data */
+#define JPEG2000_MARKER_EOC 0xD9 /* End of Codestream */
+#define JPEG2000_MARKER_SIZ 0x51 /* Image and tile size */
+#define JPEG2000_MARKER_COD 0x52 /* Coding style default */
+#define JPEG2000_MARKER_COC 0x53 /* Coding style component */
+#define JPEG2000_MARKER_RGN 0x5E /* Region of interest */
+#define JPEG2000_MARKER_QCD 0x5C /* Quantization default */
+#define JPEG2000_MARKER_QCC 0x5D /* Quantization component */
+#define JPEG2000_MARKER_POC 0x5F /* Progression order change */
+#define JPEG2000_MARKER_TLM 0x55 /* Tile-part lengths */
+#define JPEG2000_MARKER_PLM 0x57 /* Packet length, main header */
+#define JPEG2000_MARKER_PLT 0x58 /* Packet length, tile-part header */
+#define JPEG2000_MARKER_PPM 0x60 /* Packed packet headers, main header */
+#define JPEG2000_MARKER_PPT 0x61 /* Packed packet headers, tile part header */
+#define JPEG2000_MARKER_SOP 0x91 /* Start of packet */
+#define JPEG2000_MARKER_EPH 0x92 /* End of packet header */
+#define JPEG2000_MARKER_CRG 0x63 /* Component registration */
+#define JPEG2000_MARKER_COM 0x64 /* Comment */
+/* }}} */
+
+/* {{{ php_handle_jpc
+ Main loop to parse JPEG2000 raw codestream structure */
static struct gfxinfo *php_handle_jpc(php_stream * stream TSRMLS_DC)
{
struct gfxinfo *result = NULL;
- unsigned int marker, dummy;
- unsigned short length, ff_read = 1;
+ unsigned short dummy_short;
+ int dummy_int, highest_bit_depth, bit_depth;
+ unsigned char first_marker_id;
+ unsigned int i;
+
+ /* JPEG 2000 components can be vastly different from one another.
+ Each component can be sampled at a different resolution, use
+ a different colour space, have a seperate colour depth, and
+ be compressed totally differently! This makes giving a single
+ "bit depth" answer somewhat problematic. For this implementation
+ we'll use the highest depth encountered. */
+
+ /* Get the single byte that remains after the file type indentification */
+ first_marker_id = php_stream_getc(stream);
+
+ /* Ensure that this marker is SIZ (as is mandated by the standard) */
+ if (first_marker_id != JPEG2000_MARKER_SIZ) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "JPEG2000 codestream corrupt(Expected SIZ marker not found after SOC)");
+ return NULL;
+ }
+
+ result = (struct gfxinfo *)ecalloc(1, sizeof(struct gfxinfo));
+
+ dummy_short = php_read2(stream TSRMLS_CC); /* Lsiz */
+ dummy_short = php_read2(stream TSRMLS_CC); /* Rsiz */
+ result->height = php_read4(stream TSRMLS_CC); /* Xsiz */
+ result->width = php_read4(stream TSRMLS_CC); /* Ysiz */
+
+ dummy_int = php_read4(stream TSRMLS_CC); /* XOsiz */
+ dummy_int = php_read4(stream TSRMLS_CC); /* YOsiz */
+ dummy_int = php_read4(stream TSRMLS_CC); /* XTsiz */
+ dummy_int = php_read4(stream TSRMLS_CC); /* YTsiz */
+ dummy_int = php_read4(stream TSRMLS_CC); /* XTOsiz */
+ dummy_int = php_read4(stream TSRMLS_CC); /* YTOsiz */
+
+ result->channels = php_read2(stream TSRMLS_CC); /* Csiz */
+
+ /* Collect bit depth info */
+ highest_bit_depth = bit_depth = 0;
+ for (i = 0; i < result->channels; i++) {
+ bit_depth = php_stream_getc(stream); /* Ssiz[i] */
+ bit_depth++;
+ if (bit_depth > highest_bit_depth) {
+ highest_bit_depth = bit_depth;
+ }
+
+ php_stream_getc(stream); /* XRsiz[i] */
+ php_stream_getc(stream); /* YRsiz[i] */
+ }
+
+ result->bits = highest_bit_depth;
- marker = php_next_marker(stream, 0, 0, ff_read TSRMLS_CC);
- ff_read = 0;
- if ( marker == JC_SIZ)
+ return result;
+}
+/* }}} */
+
+/* {{{ php_handle_jp2
+ main loop to parse JPEG 2000 JP2 wrapper format structure */
+static struct gfxinfo *php_handle_jp2(php_stream *stream TSRMLS_DC)
+{
+ struct gfxinfo *result = NULL;
+ unsigned int box_length;
+ unsigned int box_type;
+ char jp2c_box_id[] = {(char)0x6a, (char)0x70, (char)0x32, (char)0x63};
+
+ /* JP2 is a wrapper format for JPEG 2000. Data is contained within "boxes".
+ Boxes themselves can be contained within "super-boxes". Super-Boxes can
+ contain super-boxes which provides us with a hierarchical storage system.
+
+ It is valid for a JP2 file to contain multiple individual codestreams.
+ We'll just look for the first codestream at the root of the box structure
+ and handle that.
+ */
+
+ for (;;)
{
- length = php_read2(stream TSRMLS_CC); /* Lsiz: length of segment */
- if ( length<42 || length>49191) /* read the spec */
- return NULL;
- result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
- if ( !result)
+ box_length = php_read4(stream TSRMLS_CC); /* LBox */
+ /* TBox */
+ if (php_stream_read(stream, (void *)&box_type, sizeof(box_type)) != sizeof(box_type)) {
+ /* Use this as a general "out of stream" error */
+ break;
+ }
+
+ if (box_length == 1) {
+ /* We won't handle XLBoxes */
return NULL;
- dummy = php_read2(stream TSRMLS_CC); /* Rsiz: capabilities */
- result->height = php_read4(stream TSRMLS_CC); /* Xsiz */
- result->width = php_read4(stream TSRMLS_CC); /* Ysiz */
- dummy = php_read4(stream TSRMLS_CC); /* X0siz */
- dummy = php_read4(stream TSRMLS_CC); /* Y0siz */
- dummy = php_read4(stream TSRMLS_CC); /* XTsiz */
- dummy = php_read4(stream TSRMLS_CC); /* YTsiz */
- dummy = php_read4(stream TSRMLS_CC); /* XT0siz */
- dummy = php_read4(stream TSRMLS_CC); /* YT0siz */
- result->bits = php_read2(stream TSRMLS_CC); /* Csiz: precision in bitss */
- result->channels = 0; /* don't know yet */
- return result;
+ }
+
+ if (!memcmp(&box_type, jp2c_box_id, 4))
+ {
+ /* Skip the first 3 bytes to emulate the file type examination */
+ php_stream_seek(stream, 3, SEEK_CUR);
+
+ result = php_handle_jpc(stream TSRMLS_CC);
+ break;
+ }
+
+ /* Skip over LBox (Which includes both TBox and LBox itself */
+ php_stream_seek(stream, box_length - 8, SEEK_CUR);
+ }
+
+ if (result == NULL) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "JP2 file has no codestreams at root level");
}
+
return result;
}
/* }}} */
}
/* }}} */
+/* {{{ php_get_wbmp
+ * int WBMP file format type
+ * byte Header Type
+ * byte Extended Header
+ * byte Header Data (type 00 = multibyte)
+ * byte Header Data (type 11 = name/pairs)
+ * int Number of columns
+ * int Number of rows
+ */
+static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check TSRMLS_DC)
+{
+ int i, width = 0, height = 0;
+
+ if (php_stream_rewind(stream)) {
+ return 0;
+ }
+
+ /* get type */
+ if (php_stream_getc(stream) != 0) {
+ return 0;
+ }
+
+ /* skip header */
+ do {
+ i = php_stream_getc(stream);
+ if (i < 0) {
+ return 0;
+ }
+ } while (i & 0x80);
+
+ /* get width */
+ do {
+ i = php_stream_getc(stream);
+ if (i < 0) {
+ return 0;
+ }
+ width = (width << 7) | (i & 0x7f);
+ } while (i & 0x80);
+
+ /* get height */
+ do {
+ i = php_stream_getc(stream);
+ if (i < 0) {
+ return 0;
+ }
+ height = (height << 7) | (i & 0x7f);
+ } while (i & 0x80);
+
+ if (!check) {
+ (*result)->width = width;
+ (*result)->height = height;
+ }
+
+ return IMAGE_FILETYPE_WBMP;
+}
+/* }}} */
+
+/* {{{ php_handle_wbmp
+*/
+static struct gfxinfo *php_handle_wbmp(php_stream * stream TSRMLS_DC)
+{
+ struct gfxinfo *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
+
+ if (!php_get_wbmp(stream, &result, 0 TSRMLS_CC)) {
+ efree(result);
+ return NULL;
+ }
+
+ return result;
+}
+/* }}} */
+
+/* {{{ php_get_xbm
+ */
+#define MAX_XBM_LINE_SIZE 255
+static int php_get_xbm(php_stream *stream, struct gfxinfo **result TSRMLS_DC)
+{
+ char fline[MAX_XBM_LINE_SIZE];
+ char iname[MAX_XBM_LINE_SIZE];
+ char *type;
+ int value;
+ unsigned int width = 0, height = 0;
+
+ if (result) {
+ *result = NULL;
+ }
+ if (php_stream_rewind(stream)) {
+ return 0;
+ }
+ while (php_stream_gets(stream, fline, MAX_XBM_LINE_SIZE)) {
+ fline[MAX_XBM_LINE_SIZE-1] = '\0';
+ if (strlen(fline) == MAX_XBM_LINE_SIZE-1) {
+ return 0;
+ }
+
+ if (sscanf(fline, "#define %s %d", iname, &value) == 2) {
+ if (!(type = strrchr(iname, '_'))) {
+ type = iname;
+ } else {
+ type++;
+ }
+
+ if (!strcmp("width", type)) {
+ width = (unsigned int) value;
+ if (height) {
+ break;
+ }
+ }
+ if (!strcmp("height", type)) {
+ height = (unsigned int) value;
+ if (width) {
+ break;
+ }
+ }
+ }
+ }
+
+ if (width && height) {
+ if (result) {
+ *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
+ (*result)->width = width;
+ (*result)->height = height;
+ }
+ return IMAGE_FILETYPE_XBM;
+ }
+
+ return 0;
+}
+/* }}} */
+
+/* {{{ php_handle_xbm
+ */
+static struct gfxinfo *php_handle_xbm(php_stream * stream TSRMLS_DC)
+{
+ struct gfxinfo *result;
+ php_get_xbm(stream, &result TSRMLS_CC);
+ return result;
+}
+/* }}} */
+
/* {{{ php_image_type_to_mime_type
* Convert internal image_type to mime type */
PHPAPI const char * php_image_type_to_mime_type(int image_type)
case IMAGE_FILETYPE_GIF:
return "image/gif";
case IMAGE_FILETYPE_JPEG:
- case IMAGE_FILETYPE_JPC:
return "image/jpeg";
case IMAGE_FILETYPE_PNG:
return "image/png";
return "image/tiff";
case IMAGE_FILETYPE_IFF:
return "image/iff";
+ case IMAGE_FILETYPE_WBMP:
+ return "image/vnd.wap.wbmp";
+ case IMAGE_FILETYPE_JPC:
+ return "application/octet-stream";
+ case IMAGE_FILETYPE_JP2:
+ return "image/jp2";
+ case IMAGE_FILETYPE_XBM:
+ return "image/xbm";
default:
case IMAGE_FILETYPE_UNKNOWN:
return "application/octet-stream"; /* suppose binary format */
detect filetype from first bytes */
PHPAPI int php_getimagetype(php_stream * stream, char *filetype TSRMLS_DC)
{
- char tmp[8];
+ char tmp[12];
if ( !filetype) filetype = tmp;
- if((php_stream_read(stream, filetype, 3)) <= 0) {
+ if((php_stream_read(stream, filetype, 3)) != 3) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Read error!");
return IMAGE_FILETYPE_UNKNOWN;
}
+/* BYTES READ: 3 */
if (!memcmp(filetype, php_sig_gif, 3)) {
return IMAGE_FILETYPE_GIF;
} else if (!memcmp(filetype, php_sig_jpg, 3)) {
return IMAGE_FILETYPE_JPEG;
} else if (!memcmp(filetype, php_sig_png, 3)) {
- php_stream_read(stream, filetype+3, 5);
+ if (php_stream_read(stream, filetype+3, 5) != 5) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Read error!");
+ return IMAGE_FILETYPE_UNKNOWN;
+ }
if (!memcmp(filetype, php_sig_png, 8)) {
return IMAGE_FILETYPE_PNG;
} else {
} else if (!memcmp(filetype, php_sig_jpc, 3)) {
return IMAGE_FILETYPE_JPC;
}
- php_stream_read(stream, filetype+3, 1);
+
+ if (php_stream_read(stream, filetype+3, 1) != 1) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Read error!");
+ return IMAGE_FILETYPE_UNKNOWN;
+ }
+/* BYTES READ: 4 */
if (!memcmp(filetype, php_sig_tif_ii, 4)) {
return IMAGE_FILETYPE_TIFF_II;
} else
if (!memcmp(filetype, php_sig_iff, 4)) {
return IMAGE_FILETYPE_IFF;
}
-
+
+ if (php_stream_read(stream, filetype+4, 8) != 8) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Read error!");
+ return IMAGE_FILETYPE_UNKNOWN;
+ }
+/* BYTES READ: 12 */
+ if (!memcmp(filetype, php_sig_jp2, 12)) {
+ return IMAGE_FILETYPE_JP2;
+ }
+
+/* AFTER ALL ABOVE FAILED */
+ if (php_get_wbmp(stream, NULL, 1 TSRMLS_CC)) {
+ return IMAGE_FILETYPE_WBMP;
+ }
+ if (php_get_xbm(stream, NULL TSRMLS_CC)) {
+ return IMAGE_FILETYPE_XBM;
+ }
return IMAGE_FILETYPE_UNKNOWN;
}
/* }}} */
case IMAGE_FILETYPE_JPC:
result = php_handle_jpc(stream TSRMLS_CC);
break;
+ case IMAGE_FILETYPE_JP2:
+ result = php_handle_jp2(stream TSRMLS_CC);
+ break;
case IMAGE_FILETYPE_IFF:
- result = php_handle_iff(stream TSRMLS_CC);
+ result = php_handle_iff(stream TSRMLS_CC);
+ break;
+ case IMAGE_FILETYPE_WBMP:
+ result = php_handle_wbmp(stream TSRMLS_CC);
+ break;
+ case IMAGE_FILETYPE_XBM:
+ result = php_handle_xbm(stream TSRMLS_CC);
+ break;
default:
case IMAGE_FILETYPE_UNKNOWN:
break;