This patch fixes import-jpeg. There's a bug in the parsing of the JPEG
header - it was blindly searching for section headers in the file without
following the file structure. If the pattern of the section header happens
to appear wthin a section's data then the image size is reported
incorrectly.
Piers.
diff -r -u swfmill-0.3.0.orig\src\swft\swft_import_jpeg.cpp
swfmill-0.3.0\src\swft\swft_import_jpeg.cpp
--- swfmill-0.3.0.orig\src\swft\swft_import_jpeg.cpp Sun Nov 29 17:18:54
2009
+++ swfmill-0.3.0\src\swft\swft_import_jpeg.cpp Mon Mar 22
16:35:11 2010
@@ -23,6 +23,66 @@
return r;
}
+bool getJpegDimensions (FILE * infile, int &image_width, int &image_height)
+{
+ image_width = image_height = 0;
+
+ if (fgetc(infile) != 0xFF || fgetc(infile) != 0xD8 )
+ return false;
+
+ while (!feof (infile))
+ {
+ if (fgetc (infile) != 0xff)
+ return false;
+
+ int marker;
+ do
+ marker = fgetc (infile);
+ while (marker == 0xff);
+
+ switch (marker)
+ {
+ case 0xC0:
+ case 0xC1:
+ case 0xC2:
+ case 0xC3:
+ case 0xC5:
+ case 0xC6:
+ case 0xC7:
+ case 0xC9:
+ case 0xCA:
+ case 0xCB:
+ case 0xCD:
+ case 0xCE:
+ case 0xCF:
+ {
+
int length = getJpegWord (infile);
+
fgetc (infile);
+
image_height = getJpegWord (infile);
+
image_width = getJpegWord (infile);
+
fgetc (infile);
+
+
return true;
+ }
+ case 0xDA:
+ case 0xD9:
+ return false;
+ default:
+ {
+
int length = getJpegWord (infile);
+
+
if (length < 2)
+
return false;
+
+
fseek (infile, length - 2, SEEK_CUR);
+ }
+ break;
+ }
+ }
+
+ return false;
+}
+
void swft_import_jpeg( xmlXPathParserContextPtr ctx, int nargs ) {
xsltTransformContextPtr tctx;
unsigned char *filename;
@@ -155,9 +215,10 @@
// figure width/height
int width=-1, height=-1;
+ /*
while( !feof( fp ) ) { // could do a && width==-1 here, but
that captures preview imgs...
if( fgetc(fp) == 0xff ) {
- if( fgetc(fp) == 0xc0 ) {
+ if( (fgetc(fp) &0xf0) == 0xc0
) {
//
StartOfFrame
// skip
length and precision (UGLY, eh?)
fgetc(fp);
fgetc(fp); fgetc(fp);
@@ -168,14 +229,24 @@
}
}
}
+ */
+
+ unsigned char *jpegdata = NULL;
+ unsigned char *data = NULL;
+
+ if (!getJpegDimensions (fp, width, height))
+ {
+ fprintf(stderr,"WARNING: could not extract
dimensions for jpeg %s\n", filename );
+ goto fail;
+ }
+
+
snprintf(tmp,TMP_STRLEN,"%i", width);
xmlSetProp( node, (const xmlChar *)"width", (const xmlChar
*)&tmp );
snprintf(tmp,TMP_STRLEN,"%i", height);
xmlSetProp( node, (const xmlChar *)"height", (const xmlChar
*)&tmp );
// add data
- unsigned char *jpegdata = NULL;
- unsigned char *data = NULL;
int data_size, mask_size;
int size, ofs;
struct stat filestat;
@@ -252,4 +323,3 @@
if( data ) delete data;
if( jpegdata ) delete jpegdata;
}
-
_______________________________________________
swfmill mailing list
[email protected]
http://osflash.org/mailman/listinfo/swfmill_osflash.org