$NetBSD: patch-aa,v 1.3 2004/12/14 12:16:43 markd Exp $ --- app/compressedgif.cpp.orig 2003-02-09 17:56:50.000000000 +1300 +++ app/compressedgif.cpp @@ -9,6 +9,51 @@ #include #include +/* from */ +extern "C" { + +typedef enum +{ + UndefinedBlobMode, + ReadBlobMode, + ReadBinaryBlobMode, + WriteBlobMode, + WriteBinaryBlobMode, + IOBinaryBlobMode +} BlobMode; + +extern MagickExport MagickBooleanType + OpenBlob(const ImageInfo *,Image *,const BlobMode,ExceptionInfo *); + +extern MagickExport ssize_t + WriteBlob(Image *,const size_t,const unsigned char *), + WriteBlobByte(Image *,const unsigned char), + WriteBlobLSBShort(Image *,const unsigned short); + +extern MagickExport void + CloseBlob(Image *); + + +extern MagickExport const char + *SaveImageTag, + *SaveImagesTag; + +} + +#define ThrowWriterException(severity,tag) \ +{ \ + assert(image != (Image *) NULL); \ + (void) ThrowMagickException(&image->exception,GetMagickModule(),severity, \ + tag,image->filename); \ + if (image_info->adjoin != MagickFalse) \ + while (image->previous != (Image *) NULL) \ + image=image->previous; \ + CloseBlob(image); \ + return(MagickFalse); \ +} + +/* end of defns from */ + #ifndef False #define False 0 #endif @@ -40,31 +85,31 @@ unsigned int EncodeCompressedGIFImage(co Image *image, const unsigned int data_size) { -#define MaxCode(number_bits) ((1 << (number_bits))-1) +#define MaxCode(number_bits) ((1UL << (number_bits))-1) #define MaxHashTable 5003 -#define MaxGIFBits 12 -#define MaxGIFTable (1 << MaxGIFBits) +#define MaxGIFBits 12UL +#define MaxGIFTable (1UL << MaxGIFBits) #define GIFOutputCode(code) \ { \ /* \ Emit a code. \ */ \ if (bits > 0) \ - datum|=((long) code << bits); \ + datum|=(code) << bits; \ else \ - datum=(long) code; \ + datum=code; \ bits+=number_bits; \ while (bits >= 8) \ { \ /* \ Add a character to current packet. \ */ \ - packet[byte_count++]=(unsigned char) (datum & 0xff); \ - if (byte_count >= 254) \ + packet[length++]=(unsigned char) (datum & 0xff); \ + if (length >= 254) \ { \ - (void) WriteBlobByte(image,byte_count); \ - (void) WriteBlob(image,byte_count,(char *) packet); \ - byte_count=0; \ + (void) WriteBlobByte(image,(unsigned char) length); \ + (void) WriteBlob(image,length,packet); \ + length=0; \ } \ datum>>=8; \ bits-=8; \ @@ -79,18 +124,14 @@ unsigned int EncodeCompressedGIFImage(co } \ } - int - displacement, - next_pixel, - bits, - byte_count, - k, - number_bits, - offset, - pass; + + IndexPacket + index; long - datum, + displacement, + offset, + k, y; register const PixelPacket @@ -103,28 +144,40 @@ unsigned int EncodeCompressedGIFImage(co i, x; + size_t + length; + short - clear_code, - end_of_information_code, - free_code, *hash_code, *hash_prefix, - index, - max_code, waiting_code; unsigned char *packet, *hash_suffix; + unsigned int + status; + + unsigned long + bits, + clear_code, + datum, + end_of_information_code, + free_code, + max_code, + next_pixel, + number_bits, + pass; + /* Allocate encoder tables. */ assert(image != (Image *) NULL); - packet=(unsigned char *) AcquireMemory(256); - hash_code=(short *) AcquireMemory(MaxHashTable*sizeof(short)); - hash_prefix=(short *) AcquireMemory(MaxHashTable*sizeof(short)); - hash_suffix=(unsigned char *) AcquireMemory(MaxHashTable); + packet=(unsigned char *) AcquireMagickMemory(256); + hash_code=(short *) AcquireMagickMemory(MaxHashTable*sizeof(short)); + hash_prefix=(short *) AcquireMagickMemory(MaxHashTable*sizeof(short)); + hash_suffix=(unsigned char *) AcquireMagickMemory(MaxHashTable); if ((packet == (unsigned char *) NULL) || (hash_code == (short *) NULL) || (hash_prefix == (short *) NULL) || (hash_suffix == (unsigned char *) NULL)) @@ -137,7 +190,7 @@ unsigned int EncodeCompressedGIFImage(co clear_code=((short) 1 << (data_size-1)); end_of_information_code=clear_code+1; free_code=clear_code+2; - byte_count=0; + length=0; datum=0; bits=0; for (i=0; i < MaxHashTable; i++) @@ -156,7 +209,7 @@ unsigned int EncodeCompressedGIFImage(co break; indexes=GetIndexes(image); if (y == 0) - waiting_code=(*indexes); + waiting_code=(short) (*indexes); for (x=(y == 0) ? 1 : 0; x < (long) image->columns; x++) { /* @@ -164,14 +217,15 @@ unsigned int EncodeCompressedGIFImage(co */ index=indexes[x] & 0xff; p++; - k=(int) ((int) index << (MaxGIFBits-8))+waiting_code; + k=(int) (index << (MaxGIFBits-8))+waiting_code; if (k >= MaxHashTable) k-=MaxHashTable; next_pixel=false; displacement=1; if ((image_info->compression != NoCompression) && (hash_code[k] > 0)) { - if ((hash_prefix[k] == waiting_code) && (hash_suffix[k] == index)) + if ((hash_prefix[k] == waiting_code) && + (hash_suffix[k] == (unsigned char) index)) { waiting_code=hash_code[k]; continue; @@ -185,7 +239,8 @@ unsigned int EncodeCompressedGIFImage(co k+=MaxHashTable; if (hash_code[k] == 0) break; - if ((hash_prefix[k] == waiting_code) && (hash_suffix[k] == index)) + if ((hash_prefix[k] == waiting_code) && + (hash_suffix[k] == (unsigned char) index)) { waiting_code=hash_code[k]; next_pixel=true; @@ -195,10 +250,10 @@ unsigned int EncodeCompressedGIFImage(co if (next_pixel == true) continue; } - GIFOutputCode(waiting_code); + GIFOutputCode((unsigned long) waiting_code); if (free_code < MaxGIFTable) { - hash_code[k]=free_code++; + hash_code[k]=(short) free_code++; hash_prefix[k]=waiting_code; hash_suffix[k]=(unsigned char) index; } @@ -217,7 +272,7 @@ unsigned int EncodeCompressedGIFImage(co number_bits=data_size; max_code=MaxCode(number_bits); } - waiting_code=index; + waiting_code=(short) index; } if (image_info->interlace == NoInterlace) offset++; @@ -263,42 +318,45 @@ unsigned int EncodeCompressedGIFImage(co } if (image->previous == (Image *) NULL) if (QuantumTick(y,image->rows)) - if (!MagickMonitor(SaveImageTag,y,image->rows,&image->exception)) - break; + { + status=MagickMonitor(SaveImageTag,y,image->rows,&image->exception); + if (status == false) + break; + } } /* Flush out the buffered code. */ - GIFOutputCode(waiting_code); + GIFOutputCode((unsigned long) waiting_code); GIFOutputCode(end_of_information_code); if (bits > 0) { /* Add a character to current packet. */ - packet[byte_count++]=(unsigned char) (datum & 0xff); - if (byte_count >= 254) + packet[length++]=(unsigned char) (datum & 0xff); + if (length >= 254) { - (void) WriteBlobByte(image,byte_count); - (void) WriteBlob(image,byte_count,(char *) packet); - byte_count=0; + (void) WriteBlobByte(image,(unsigned char) length); + (void) WriteBlob(image,length,packet); + length=0; } } /* Flush accumulated data. */ - if (byte_count > 0) + if (length > 0) { - (void) WriteBlobByte(image,byte_count); - (void) WriteBlob(image,byte_count,(char *) packet); + (void) WriteBlobByte(image,(unsigned char) length); + (void) WriteBlob(image,length,packet); } /* Free encoder memory. */ - LiberateMemory((void **) &hash_suffix); - LiberateMemory((void **) &hash_prefix); - LiberateMemory((void **) &hash_code); - LiberateMemory((void **) &packet); + hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix); + hash_prefix=(short *) RelinquishMagickMemory(hash_prefix); + hash_code=(short *) RelinquishMagickMemory(hash_code); + packet=(unsigned char *) RelinquishMagickMemory(packet); return(true); } @@ -308,10 +366,15 @@ unsigned int WriteCompressedGIFImage(con *next_image; int + c; + + long + j, + opacity, y; - long - opacity; + MagickOffsetType + scene; QuantizeInfo quantize_info; @@ -335,11 +398,9 @@ unsigned int WriteCompressedGIFImage(con *q; size_t - j; + length; unsigned char - bits_per_pixel, - c, *colormap, *global_colormap; @@ -348,7 +409,7 @@ unsigned int WriteCompressedGIFImage(con status; unsigned long - scene; + bits_per_pixel; /* Open output image file. @@ -357,9 +418,11 @@ unsigned int WriteCompressedGIFImage(con assert(image_info->signature == MagickSignature); assert(image != (Image *) NULL); assert(image->signature == MagickSignature); + if (image->debug != false) + (void) LogMagickEvent(TraceEvent,GetMagickModule(),image->filename); status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); if (status == false) - ThrowWriterException(FileOpenError,"Unable to open file",image); + return(status); /* Determine image bounding box. */ @@ -380,37 +443,36 @@ unsigned int WriteCompressedGIFImage(con /* Allocate colormap. */ - global_colormap=(unsigned char *) AcquireMemory(768); - colormap=(unsigned char *) AcquireMemory(768); + global_colormap=(unsigned char *) AcquireMagickMemory(768); + colormap=(unsigned char *) AcquireMagickMemory(768); if ((global_colormap == (unsigned char *) NULL) || (colormap == (unsigned char *) NULL)) - ThrowWriterException(ResourceLimitError,"Memory allocation failed",image); + ThrowWriterException(ResourceLimitError,"Memory allocation failed"); for (i=0; i < 768; i++) - colormap[i]=0; + colormap[i]=(unsigned char) 0; /* Write GIF header. */ if ((GetImageAttribute(image,"comment") == (ImageAttribute *) NULL) && - !image_info->adjoin && !image->matte) - (void) WriteBlob(image,6,"GIF87a"); + (image_info->adjoin == false) && (image->matte == false)) + (void) WriteBlob(image,6,(unsigned char *) "GIF87a"); else if (LocaleCompare(image_info->magick,"GIF87") == 0) - (void) WriteBlob(image,6,"GIF87a"); + (void) WriteBlob(image,6,(unsigned char *) "GIF87a"); else - (void) WriteBlob(image,6,"GIF89a"); + (void) WriteBlob(image,6,(unsigned char *) "GIF89a"); page.x=image->page.x; page.y=image->page.y; if ((image->page.width != 0) && (image->page.height != 0)) page=image->page; - (void) WriteBlobLSBShort(image,page.width); - (void) WriteBlobLSBShort(image,page.height); + (void) WriteBlobLSBShort(image,(unsigned short) page.width); + (void) WriteBlobLSBShort(image,(unsigned short) page.height); /* Write images to file. */ interlace=image_info->interlace; - if (image_info->adjoin && (image->next != (Image *) NULL)) + if ((image_info->adjoin != false) && (image->next != (Image *) NULL)) interlace=NoInterlace; - opacity=(-1); scene=0; do { @@ -422,22 +484,24 @@ unsigned int WriteCompressedGIFImage(con */ GetQuantizeInfo(&quantize_info); quantize_info.dither=image_info->dither; - quantize_info.number_colors=image->matte ? 255 : 256; + quantize_info.number_colors=(unsigned long) + (image->matte != false ? 255 : 256); (void) QuantizeImage(&quantize_info,image); - if (image->matte) + if (image->matte != false) { /* Set transparent pixel. */ opacity=(long) image->colors++; - ReacquireMemory((void **) &image->colormap, - image->colors*sizeof(PixelPacket)); + image->colormap=(PixelPacket *) ResizeMagickMemory(image->colormap, + (size_t) image->colors*sizeof(PixelPacket)); if (image->colormap == (PixelPacket *) NULL) { - LiberateMemory((void **) &global_colormap); - LiberateMemory((void **) &colormap); + global_colormap=(unsigned char *) + RelinquishMagickMemory(global_colormap); + colormap=(unsigned char *) RelinquishMagickMemory(colormap); ThrowWriterException(ResourceLimitError, - "Memory allocation failed",image) + "Memory allocation failed") } image->colormap[opacity]=image->background_color; for (y=0; y < (long) image->rows; y++) @@ -449,17 +513,17 @@ unsigned int WriteCompressedGIFImage(con indexes=GetIndexes(image); for (x=0; x < (long) image->columns; x++) { - if (p->opacity == TransparentOpacity) + if ((double) p->opacity > image->fuzz) indexes[x]=(IndexPacket) opacity; p++; } - if (!SyncImagePixels(image)) + if (SyncImagePixels(image) == false) break; } } } else - if (image->matte) + if (image->matte != false) { /* Identify transparent pixel index. @@ -472,7 +536,7 @@ unsigned int WriteCompressedGIFImage(con indexes=GetIndexes(image); for (x=0; x < (long) image->columns; x++) { - if (p->opacity == TransparentOpacity) + if ((double) p->opacity > image->fuzz) { opacity=(long) indexes[x]; break; @@ -495,13 +559,13 @@ unsigned int WriteCompressedGIFImage(con *q++=ScaleQuantumToChar(image->colormap[i].green); *q++=ScaleQuantumToChar(image->colormap[i].blue); } - for ( ; i < (1L << bits_per_pixel); i++) + for ( ; i < (long) (1UL << bits_per_pixel); i++) { - *q++=(Quantum) 0x0; - *q++=(Quantum) 0x0; - *q++=(Quantum) 0x0; + *q++=(unsigned char) 0x0; + *q++=(unsigned char) 0x0; + *q++=(unsigned char) 0x0; } - if ((image->previous == (Image *) NULL) || !image_info->adjoin) + if ((image->previous == (Image *) NULL) || (image_info->adjoin == false)) { /* Write global colormap. @@ -509,13 +573,14 @@ unsigned int WriteCompressedGIFImage(con c=0x80; c|=(8-1) << 4; /* color resolution */ c|=(bits_per_pixel-1); /* size of global colormap */ - (void) WriteBlobByte(image,c); - for (j=0; j < Max(image->colors-1,1); j++) + (void) WriteBlobByte(image,(unsigned char) c); + for (j=0; j < (long) Max(image->colors-1,1); j++) if (FuzzyColorMatch(&image->background_color,image->colormap+j,0)) break; - (void) WriteBlobByte(image,(long) j); /* background color */ - (void) WriteBlobByte(image,0x0); /* reserved */ - (void) WriteBlob(image,3*(1 << bits_per_pixel),(char *) colormap); + (void) WriteBlobByte(image,(unsigned char) j); /* background color */ + (void) WriteBlobByte(image,(unsigned char) 0x0); /* reserved */ + length=(size_t) (3*(1 << bits_per_pixel)); + (void) WriteBlob(image,length,colormap); for (j=0; j < 768; j++) global_colormap[j]=colormap[j]; } @@ -524,16 +589,17 @@ unsigned int WriteCompressedGIFImage(con /* Write Graphics Control extension. */ - (void) WriteBlobByte(image,0x21); - (void) WriteBlobByte(image,0xf9); - (void) WriteBlobByte(image,0x04); - c=(unsigned char) ((int) image->dispose << 2); + (void) WriteBlobByte(image,(unsigned char) 0x21); + (void) WriteBlobByte(image,(unsigned char) 0xf9); + (void) WriteBlobByte(image,(unsigned char) 0x04); + c=((image->dispose) << 2); if (opacity >= 0) c|=0x01; - (void) WriteBlobByte(image,c); - (void) WriteBlobLSBShort(image,image->delay); - (void) WriteBlobByte(image,opacity >= 0 ? opacity : 0); - (void) WriteBlobByte(image,0x00); + (void) WriteBlobByte(image,(unsigned char) c); + (void) WriteBlobLSBShort(image,(unsigned short) image->delay); + (void) WriteBlobByte(image,(unsigned char) + (opacity >= 0 ? opacity : 0)); + (void) WriteBlobByte(image,(unsigned char) 0x00); if (GetImageAttribute(image,"comment") != (ImageAttribute *) NULL) { const ImageAttribute @@ -548,18 +614,18 @@ unsigned int WriteCompressedGIFImage(con /* Write Comment extension. */ - (void) WriteBlobByte(image,0x21); - (void) WriteBlobByte(image,0xfe); + (void) WriteBlobByte(image,(unsigned char) 0x21); + (void) WriteBlobByte(image,(unsigned char) 0xfe); attribute=GetImageAttribute(image,"comment"); p=attribute->value; while (strlen(p) != 0) { count=Min(strlen(p),255); - (void) WriteBlobByte(image,(long) count); + (void) WriteBlobByte(image,(unsigned char) count); for (i=0; i < (long) count; i++) - (void) WriteBlobByte(image,*p++); + (void) WriteBlobByte(image,(unsigned char) *p++); } - (void) WriteBlobByte(image,0x0); + (void) WriteBlobByte(image,(unsigned char) 0x00); } if ((image->previous == (Image *) NULL) && (image->next != (Image *) NULL) && (image->iterations != 1)) @@ -567,14 +633,14 @@ unsigned int WriteCompressedGIFImage(con /* Write Netscape Loop extension. */ - (void) WriteBlobByte(image,0x21); - (void) WriteBlobByte(image,0xff); - (void) WriteBlobByte(image,0x0b); - (void) WriteBlob(image,11,"NETSCAPE2.0"); - (void) WriteBlobByte(image,0x03); - (void) WriteBlobByte(image,0x01); - (void) WriteBlobLSBShort(image,image->iterations); - (void) WriteBlobByte(image,0x00); + (void) WriteBlobByte(image,(unsigned char) 0x21); + (void) WriteBlobByte(image,(unsigned char) 0xff); + (void) WriteBlobByte(image,(unsigned char) 0x0b); + (void) WriteBlob(image,11,(unsigned char *) "NETSCAPE2.0"); + (void) WriteBlobByte(image,(unsigned char) 0x03); + (void) WriteBlobByte(image,(unsigned char) 0x01); + (void) WriteBlobLSBShort(image,(unsigned short) image->iterations); + (void) WriteBlobByte(image,(unsigned char) 0x00); } } (void) WriteBlobByte(image,','); /* image separator */ @@ -585,51 +651,50 @@ unsigned int WriteCompressedGIFImage(con page.y=image->page.y; if ((image->page.width != 0) && (image->page.height != 0)) page=image->page; - (void) WriteBlobLSBShort(image,page.x); - (void) WriteBlobLSBShort(image,page.y); - (void) WriteBlobLSBShort(image,image->columns); - (void) WriteBlobLSBShort(image,image->rows); + (void) WriteBlobLSBShort(image,(unsigned short) (page.x < 0 ? 0 : page.x)); + (void) WriteBlobLSBShort(image,(unsigned short) (page.y < 0 ? 0 : page.y)); + (void) WriteBlobLSBShort(image,(unsigned short) image->columns); + (void) WriteBlobLSBShort(image,(unsigned short) image->rows); c=0x00; if (interlace != NoInterlace) c|=0x40; /* pixel data is interlaced */ - for (j=0; j < (3*image->colors); j++) + for (j=0; j < (long) (3*image->colors); j++) if (colormap[j] != global_colormap[j]) break; - if (j == (3*image->colors)) - (void) WriteBlobByte(image,c); + if (j == (long) (3*image->colors)) + (void) WriteBlobByte(image,(unsigned char) c); else { c|=0x80; c|=(bits_per_pixel-1); /* size of local colormap */ - (void) WriteBlobByte(image,c); - (void) WriteBlob(image,3*(1 << bits_per_pixel),(char *) colormap); + (void) WriteBlobByte(image,(unsigned char) c); + length=(size_t) (3*(1UL << bits_per_pixel)); + (void) WriteBlob(image,length,colormap); } /* Write the image data. */ - c=Max(bits_per_pixel,2); - (void) WriteBlobByte(image,c); + c=(int) Max(bits_per_pixel,2); + (void) WriteBlobByte(image,(unsigned char) c); status=EncodeCompressedGIFImage(image_info,image,Max(bits_per_pixel,2)+1); if (status == false) { - LiberateMemory((void **) &global_colormap); - LiberateMemory((void **) &colormap); - ThrowWriterException(ResourceLimitError,"Memory allocation failed", - image) + global_colormap=(unsigned char *) + RelinquishMagickMemory(global_colormap); + colormap=(unsigned char *) RelinquishMagickMemory(colormap); + ThrowWriterException(ResourceLimitError,"Memory allocation failed"); } - (void) WriteBlobByte(image,0x0); + (void) WriteBlobByte(image,(unsigned char) 0x00); if (image->next == (Image *) NULL) break; image=GetNextImage(image); - if (!MagickMonitor(SaveImagesTag,scene++,GetImageListSize(image),&image->exception)) + status=MagickMonitor(SaveImagesTag,scene++,GetImageListSize(image),&image->exception); + if (status == false) break; - } while (image_info->adjoin); + } while (image_info->adjoin != false); (void) WriteBlobByte(image,';'); /* terminator */ - LiberateMemory((void **) &global_colormap); - LiberateMemory((void **) &colormap); - if (image_info->adjoin) - while (image->previous != (Image *) NULL) - image=image->previous; + global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap); + colormap=(unsigned char *) RelinquishMagickMemory(colormap); CloseBlob(image); return(true); }