compress.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %           CCCC   OOO   M   M  PPPP   RRRR   EEEEE   SSSSS  SSSSS            %
00007 %          C      O   O  MM MM  P   P  R   R  E       SS     SS               %
00008 %          C      O   O  M M M  PPPP   RRRR   EEE      SSS    SSS             %
00009 %          C      O   O  M   M  P      R R    E          SS     SS            %
00010 %           CCCC   OOO   M   M  P      R  R   EEEEE   SSSSS  SSSSS            %
00011 %                                                                             %
00012 %                                                                             %
00013 %             MagickCore Image Compression/Decompression Methods              %
00014 %                                                                             %
00015 %                           Software Design                                   %
00016 %                             John Cristy                                     %
00017 %                              May  1993                                      %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.imagemagick.org/script/license.php                            %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  See the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %
00037 %
00038 */
00039 
00040 /*
00041   Include declarations.
00042 */
00043 #include "magick/studio.h"
00044 #include "magick/blob.h"
00045 #include "magick/blob-private.h"
00046 #include "magick/color-private.h"
00047 #include "magick/cache.h"
00048 #include "magick/compress.h"
00049 #include "magick/constitute.h"
00050 #include "magick/exception.h"
00051 #include "magick/exception-private.h"
00052 #include "magick/image-private.h"
00053 #include "magick/list.h"
00054 #include "magick/memory_.h"
00055 #include "magick/monitor.h"
00056 #include "magick/monitor-private.h"
00057 #include "magick/option.h"
00058 #include "magick/resource_.h"
00059 #include "magick/string_.h"
00060 #if defined(MAGICKCORE_TIFF_DELEGATE)
00061 #if defined(MAGICKCORE_HAVE_TIFFCONF_H)
00062 #include "tiffconf.h"
00063 #endif
00064 #include "tiffio.h"
00065 #define CCITTParam  "-1"
00066 #else
00067 #define CCITTParam  "0"
00068 #endif
00069 #if defined(MAGICKCORE_ZLIB_DELEGATE)
00070 #include "zlib.h"
00071 #endif
00072 
00073 /*
00074   Typedef declarations.
00075 */
00076 struct _Ascii85Info
00077 {
00078   long
00079     offset,
00080     line_break;
00081 
00082   unsigned char
00083     buffer[10];
00084 };
00085 
00086 typedef struct HuffmanTable
00087 {
00088   unsigned long
00089     id,
00090     code,
00091     length,
00092     count;
00093 } HuffmanTable;
00094 
00095 /*
00096   Huffman coding declarations.
00097 */
00098 #define TWId  23
00099 #define MWId  24
00100 #define TBId  25
00101 #define MBId  26
00102 #define EXId  27
00103 
00104 static const HuffmanTable
00105   MBTable[]=
00106   {
00107     { MBId, 0x0f, 10, 64 }, { MBId, 0xc8, 12, 128 },
00108     { MBId, 0xc9, 12, 192 }, { MBId, 0x5b, 12, 256 },
00109     { MBId, 0x33, 12, 320 }, { MBId, 0x34, 12, 384 },
00110     { MBId, 0x35, 12, 448 }, { MBId, 0x6c, 13, 512 },
00111     { MBId, 0x6d, 13, 576 }, { MBId, 0x4a, 13, 640 },
00112     { MBId, 0x4b, 13, 704 }, { MBId, 0x4c, 13, 768 },
00113     { MBId, 0x4d, 13, 832 }, { MBId, 0x72, 13, 896 },
00114     { MBId, 0x73, 13, 960 }, { MBId, 0x74, 13, 1024 },
00115     { MBId, 0x75, 13, 1088 }, { MBId, 0x76, 13, 1152 },
00116     { MBId, 0x77, 13, 1216 }, { MBId, 0x52, 13, 1280 },
00117     { MBId, 0x53, 13, 1344 }, { MBId, 0x54, 13, 1408 },
00118     { MBId, 0x55, 13, 1472 }, { MBId, 0x5a, 13, 1536 },
00119     { MBId, 0x5b, 13, 1600 }, { MBId, 0x64, 13, 1664 },
00120     { MBId, 0x65, 13, 1728 }, { MBId, 0x00, 0, 0 }
00121   };
00122 
00123 static const HuffmanTable
00124   EXTable[]=
00125   {
00126     { EXId, 0x08, 11, 1792 }, { EXId, 0x0c, 11, 1856 },
00127     { EXId, 0x0d, 11, 1920 }, { EXId, 0x12, 12, 1984 },
00128     { EXId, 0x13, 12, 2048 }, { EXId, 0x14, 12, 2112 },
00129     { EXId, 0x15, 12, 2176 }, { EXId, 0x16, 12, 2240 },
00130     { EXId, 0x17, 12, 2304 }, { EXId, 0x1c, 12, 2368 },
00131     { EXId, 0x1d, 12, 2432 }, { EXId, 0x1e, 12, 2496 },
00132     { EXId, 0x1f, 12, 2560 }, { EXId, 0x00, 0, 0 }
00133   };
00134 
00135 static const HuffmanTable
00136   MWTable[]=
00137   {
00138     { MWId, 0x1b, 5, 64 }, { MWId, 0x12, 5, 128 },
00139     { MWId, 0x17, 6, 192 }, { MWId, 0x37, 7, 256 },
00140     { MWId, 0x36, 8, 320 }, { MWId, 0x37, 8, 384 },
00141     { MWId, 0x64, 8, 448 }, { MWId, 0x65, 8, 512 },
00142     { MWId, 0x68, 8, 576 }, { MWId, 0x67, 8, 640 },
00143     { MWId, 0xcc, 9, 704 }, { MWId, 0xcd, 9, 768 },
00144     { MWId, 0xd2, 9, 832 }, { MWId, 0xd3, 9, 896 },
00145     { MWId, 0xd4, 9, 960 }, { MWId, 0xd5, 9, 1024 },
00146     { MWId, 0xd6, 9, 1088 }, { MWId, 0xd7, 9, 1152 },
00147     { MWId, 0xd8, 9, 1216 }, { MWId, 0xd9, 9, 1280 },
00148     { MWId, 0xda, 9, 1344 }, { MWId, 0xdb, 9, 1408 },
00149     { MWId, 0x98, 9, 1472 }, { MWId, 0x99, 9, 1536 },
00150     { MWId, 0x9a, 9, 1600 }, { MWId, 0x18, 6, 1664 },
00151     { MWId, 0x9b, 9, 1728 }, { MWId, 0x00, 0, 0 }
00152   };
00153 
00154 static const HuffmanTable
00155   TBTable[]=
00156   {
00157     { TBId, 0x37, 10, 0 }, { TBId, 0x02, 3, 1 }, { TBId, 0x03, 2, 2 },
00158     { TBId, 0x02, 2, 3 }, { TBId, 0x03, 3, 4 }, { TBId, 0x03, 4, 5 },
00159     { TBId, 0x02, 4, 6 }, { TBId, 0x03, 5, 7 }, { TBId, 0x05, 6, 8 },
00160     { TBId, 0x04, 6, 9 }, { TBId, 0x04, 7, 10 }, { TBId, 0x05, 7, 11 },
00161     { TBId, 0x07, 7, 12 }, { TBId, 0x04, 8, 13 }, { TBId, 0x07, 8, 14 },
00162     { TBId, 0x18, 9, 15 }, { TBId, 0x17, 10, 16 }, { TBId, 0x18, 10, 17 },
00163     { TBId, 0x08, 10, 18 }, { TBId, 0x67, 11, 19 }, { TBId, 0x68, 11, 20 },
00164     { TBId, 0x6c, 11, 21 }, { TBId, 0x37, 11, 22 }, { TBId, 0x28, 11, 23 },
00165     { TBId, 0x17, 11, 24 }, { TBId, 0x18, 11, 25 }, { TBId, 0xca, 12, 26 },
00166     { TBId, 0xcb, 12, 27 }, { TBId, 0xcc, 12, 28 }, { TBId, 0xcd, 12, 29 },
00167     { TBId, 0x68, 12, 30 }, { TBId, 0x69, 12, 31 }, { TBId, 0x6a, 12, 32 },
00168     { TBId, 0x6b, 12, 33 }, { TBId, 0xd2, 12, 34 }, { TBId, 0xd3, 12, 35 },
00169     { TBId, 0xd4, 12, 36 }, { TBId, 0xd5, 12, 37 }, { TBId, 0xd6, 12, 38 },
00170     { TBId, 0xd7, 12, 39 }, { TBId, 0x6c, 12, 40 }, { TBId, 0x6d, 12, 41 },
00171     { TBId, 0xda, 12, 42 }, { TBId, 0xdb, 12, 43 }, { TBId, 0x54, 12, 44 },
00172     { TBId, 0x55, 12, 45 }, { TBId, 0x56, 12, 46 }, { TBId, 0x57, 12, 47 },
00173     { TBId, 0x64, 12, 48 }, { TBId, 0x65, 12, 49 }, { TBId, 0x52, 12, 50 },
00174     { TBId, 0x53, 12, 51 }, { TBId, 0x24, 12, 52 }, { TBId, 0x37, 12, 53 },
00175     { TBId, 0x38, 12, 54 }, { TBId, 0x27, 12, 55 }, { TBId, 0x28, 12, 56 },
00176     { TBId, 0x58, 12, 57 }, { TBId, 0x59, 12, 58 }, { TBId, 0x2b, 12, 59 },
00177     { TBId, 0x2c, 12, 60 }, { TBId, 0x5a, 12, 61 }, { TBId, 0x66, 12, 62 },
00178     { TBId, 0x67, 12, 63 }, { TBId, 0x00, 0, 0 }
00179   };
00180 
00181 static const HuffmanTable
00182   TWTable[]=
00183   {
00184     { TWId, 0x35, 8, 0 }, { TWId, 0x07, 6, 1 }, { TWId, 0x07, 4, 2 },
00185     { TWId, 0x08, 4, 3 }, { TWId, 0x0b, 4, 4 }, { TWId, 0x0c, 4, 5 },
00186     { TWId, 0x0e, 4, 6 }, { TWId, 0x0f, 4, 7 }, { TWId, 0x13, 5, 8 },
00187     { TWId, 0x14, 5, 9 }, { TWId, 0x07, 5, 10 }, { TWId, 0x08, 5, 11 },
00188     { TWId, 0x08, 6, 12 }, { TWId, 0x03, 6, 13 }, { TWId, 0x34, 6, 14 },
00189     { TWId, 0x35, 6, 15 }, { TWId, 0x2a, 6, 16 }, { TWId, 0x2b, 6, 17 },
00190     { TWId, 0x27, 7, 18 }, { TWId, 0x0c, 7, 19 }, { TWId, 0x08, 7, 20 },
00191     { TWId, 0x17, 7, 21 }, { TWId, 0x03, 7, 22 }, { TWId, 0x04, 7, 23 },
00192     { TWId, 0x28, 7, 24 }, { TWId, 0x2b, 7, 25 }, { TWId, 0x13, 7, 26 },
00193     { TWId, 0x24, 7, 27 }, { TWId, 0x18, 7, 28 }, { TWId, 0x02, 8, 29 },
00194     { TWId, 0x03, 8, 30 }, { TWId, 0x1a, 8, 31 }, { TWId, 0x1b, 8, 32 },
00195     { TWId, 0x12, 8, 33 }, { TWId, 0x13, 8, 34 }, { TWId, 0x14, 8, 35 },
00196     { TWId, 0x15, 8, 36 }, { TWId, 0x16, 8, 37 }, { TWId, 0x17, 8, 38 },
00197     { TWId, 0x28, 8, 39 }, { TWId, 0x29, 8, 40 }, { TWId, 0x2a, 8, 41 },
00198     { TWId, 0x2b, 8, 42 }, { TWId, 0x2c, 8, 43 }, { TWId, 0x2d, 8, 44 },
00199     { TWId, 0x04, 8, 45 }, { TWId, 0x05, 8, 46 }, { TWId, 0x0a, 8, 47 },
00200     { TWId, 0x0b, 8, 48 }, { TWId, 0x52, 8, 49 }, { TWId, 0x53, 8, 50 },
00201     { TWId, 0x54, 8, 51 }, { TWId, 0x55, 8, 52 }, { TWId, 0x24, 8, 53 },
00202     { TWId, 0x25, 8, 54 }, { TWId, 0x58, 8, 55 }, { TWId, 0x59, 8, 56 },
00203     { TWId, 0x5a, 8, 57 }, { TWId, 0x5b, 8, 58 }, { TWId, 0x4a, 8, 59 },
00204     { TWId, 0x4b, 8, 60 }, { TWId, 0x32, 8, 61 }, { TWId, 0x33, 8, 62 },
00205     { TWId, 0x34, 8, 63 }, { TWId, 0x00, 0, 0 }
00206   };
00207 
00208 /*
00209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00210 %                                                                             %
00211 %                                                                             %
00212 %                                                                             %
00213 %   A S C I I 8 5 E n c o d e                                                 %
00214 %                                                                             %
00215 %                                                                             %
00216 %                                                                             %
00217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00218 %
00219 %  ASCII85Encode() encodes data in ASCII base-85 format.  ASCII base-85
00220 %  encoding produces five ASCII printing characters from every four bytes of
00221 %  binary data.
00222 %
00223 %  The format of the ASCII85Encode method is:
00224 %
00225 %      void Ascii85Encode(Image *image,const unsigned long code)
00226 %
00227 %  A description of each parameter follows:
00228 %
00229 %    o code: a binary unsigned char to encode to ASCII 85.
00230 %
00231 %    o file: write the encoded ASCII character to this file.
00232 %
00233 %
00234 */
00235 #define MaxLineExtent  36
00236 
00237 static char *Ascii85Tuple(unsigned char *data)
00238 {
00239   static char
00240     tuple[6];
00241 
00242   register long
00243     i,
00244     x;
00245 
00246   unsigned long
00247     code,
00248     quantum;
00249 
00250   code=((((unsigned long) data[0] << 8) | (unsigned long) data[1]) << 16) |
00251     ((unsigned long) data[2] << 8) | (unsigned long) data[3];
00252   if (code == 0L)
00253     {
00254       tuple[0]='z';
00255       tuple[1]='\0';
00256       return(tuple);
00257     }
00258   quantum=85UL*85UL*85UL*85UL;
00259   for (i=0; i < 4; i++)
00260   {
00261     x=(long) (code/quantum);
00262     code-=quantum*x;
00263     tuple[i]=(char) (x+(int) '!');
00264     quantum/=85L;
00265   }
00266   tuple[4]=(char) ((code % 85L)+(int) '!');
00267   tuple[5]='\0';
00268   return(tuple);
00269 }
00270 
00271 MagickExport void Ascii85Initialize(Image *image)
00272 {
00273   /*
00274     Allocate image structure.
00275   */
00276   if (image->ascii85 == (Ascii85Info *) NULL)
00277     image->ascii85=(Ascii85Info *) AcquireMagickMemory(sizeof(*image->ascii85));
00278   if (image->ascii85 == (Ascii85Info *) NULL)
00279     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00280   (void) ResetMagickMemory(image->ascii85,0,sizeof(*image->ascii85));
00281   image->ascii85->line_break=MaxLineExtent << 1;
00282   image->ascii85->offset=0;
00283 }
00284 
00285 MagickExport void Ascii85Flush(Image *image)
00286 {
00287   register char
00288     *tuple;
00289 
00290   assert(image != (Image *) NULL);
00291   assert(image->signature == MagickSignature);
00292   if (image->debug != MagickFalse)
00293     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00294   assert(image->ascii85 != (Ascii85Info *) NULL);
00295   if (image->ascii85->offset > 0)
00296     {
00297       image->ascii85->buffer[image->ascii85->offset]='\0';
00298       image->ascii85->buffer[image->ascii85->offset+1]='\0';
00299       image->ascii85->buffer[image->ascii85->offset+2]='\0';
00300       tuple=Ascii85Tuple(image->ascii85->buffer);
00301       (void) WriteBlob(image,(size_t) image->ascii85->offset+1,
00302         (const unsigned char *) (*tuple == 'z' ? "!!!!" : tuple));
00303     }
00304   (void) WriteBlobByte(image,'~');
00305   (void) WriteBlobByte(image,'>');
00306   (void) WriteBlobByte(image,'\n');
00307 }
00308 
00309 MagickExport void Ascii85Encode(Image *image,const unsigned char code)
00310 {
00311   long
00312     n;
00313 
00314   register char
00315     *q;
00316 
00317   register unsigned char
00318     *p;
00319 
00320   assert(image != (Image *) NULL);
00321   assert(image->signature == MagickSignature);
00322   assert(image->ascii85 != (Ascii85Info *) NULL);
00323   image->ascii85->buffer[image->ascii85->offset]=code;
00324   image->ascii85->offset++;
00325   if (image->ascii85->offset < 4)
00326     return;
00327   p=image->ascii85->buffer;
00328   for (n=image->ascii85->offset; n >= 4; n-=4)
00329   {
00330     for (q=Ascii85Tuple(p); *q != '\0'; q++)
00331     {
00332       image->ascii85->line_break--;
00333       if ((image->ascii85->line_break < 0) && (*q != '%'))
00334         {
00335           (void) WriteBlobByte(image,'\n');
00336           image->ascii85->line_break=2*MaxLineExtent;
00337         }
00338       (void) WriteBlobByte(image,(unsigned char) *q);
00339     }
00340     p+=8;
00341   }
00342   image->ascii85->offset=n;
00343   p-=4;
00344   for (n=0; n < 4; n++)
00345     image->ascii85->buffer[n]=(*p++);
00346 }
00347 
00348 /*
00349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00350 %                                                                             %
00351 %                                                                             %
00352 %                                                                             %
00353 %   H u f f m a n D e c o d e I m a g e                                       %
00354 %                                                                             %
00355 %                                                                             %
00356 %                                                                             %
00357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00358 %
00359 %  HuffmanDecodeImage() uncompresses an image via Huffman-coding.
00360 %
00361 %  The format of the HuffmanDecodeImage method is:
00362 %
00363 %      MagickBooleanType HuffmanDecodeImage(Image *image)
00364 %
00365 %  A description of each parameter follows:
00366 %
00367 %    o image: the image.
00368 %
00369 */
00370 
00371 static inline size_t MagickMax(const size_t x,const size_t y)
00372 {
00373   if (x > y)
00374     return(x);
00375   return(y);
00376 }
00377 
00378 static inline size_t MagickMin(const size_t x,const size_t y)
00379 {
00380   if (x < y)
00381     return(x);
00382   return(y);
00383 }
00384 
00385 MagickExport MagickBooleanType HuffmanDecodeImage(Image *image)
00386 {
00387 #define HashSize  1021
00388 #define MBHashA  293
00389 #define MBHashB  2695
00390 #define MWHashA  3510
00391 #define MWHashB  1178
00392 
00393 #define InitializeHashTable(hash,table,a,b) \
00394 { \
00395   entry=table; \
00396   while (entry->code != 0) \
00397   {  \
00398     hash[((entry->length+a)*(entry->code+b)) % HashSize]=(HuffmanTable *) entry; \
00399     entry++; \
00400   } \
00401 }
00402 
00403 #define InputBit(bit)  \
00404 {  \
00405   if ((mask & 0xff) == 0)  \
00406     {  \
00407       byte=ReadBlobByte(image);  \
00408       if (byte == EOF)  \
00409         break;  \
00410       mask=0x80;  \
00411     }  \
00412   runlength++;  \
00413   bit=(unsigned long) ((byte & mask) != 0 ? 0x01 : 0x00); \
00414   mask>>=1;  \
00415   if (bit != 0)  \
00416     runlength=0;  \
00417 }
00418 
00419   const HuffmanTable
00420     *entry;
00421 
00422   ExceptionInfo
00423     *exception;
00424 
00425   HuffmanTable
00426     **mb_hash,
00427     **mw_hash;
00428 
00429   IndexPacket
00430     index;
00431 
00432   int
00433     byte;
00434 
00435   long
00436     y;
00437 
00438   MagickBooleanType
00439     proceed;
00440 
00441   register IndexPacket
00442     *indexes;
00443 
00444   register long
00445     i;
00446 
00447   register unsigned char
00448     *p;
00449 
00450   ssize_t
00451     count;
00452 
00453   unsigned char
00454     *scanline;
00455 
00456   unsigned int
00457     bail,
00458     color;
00459 
00460   unsigned long
00461     bit,
00462     code,
00463     mask,
00464     length,
00465     null_lines,
00466     runlength;
00467 
00468   /*
00469     Allocate buffers.
00470   */
00471   assert(image != (Image *) NULL);
00472   assert(image->signature == MagickSignature);
00473   if (image->debug != MagickFalse)
00474     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00475   mb_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mb_hash));
00476   mw_hash=(HuffmanTable **) AcquireQuantumMemory(HashSize,sizeof(*mw_hash));
00477   scanline=(unsigned char *) AcquireQuantumMemory((size_t) image->columns,
00478     sizeof(*scanline));
00479   if ((mb_hash == (HuffmanTable **) NULL) ||
00480       (mw_hash == (HuffmanTable **) NULL) ||
00481       (scanline == (unsigned char *) NULL))
00482     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
00483       image->filename);
00484   /*
00485     Initialize Huffman tables.
00486   */
00487   for (i=0; i < HashSize; i++)
00488   {
00489     mb_hash[i]=(HuffmanTable *) NULL;
00490     mw_hash[i]=(HuffmanTable *) NULL;
00491   }
00492   InitializeHashTable(mw_hash,TWTable,MWHashA,MWHashB);
00493   InitializeHashTable(mw_hash,MWTable,MWHashA,MWHashB);
00494   InitializeHashTable(mw_hash,EXTable,MWHashA,MWHashB);
00495   InitializeHashTable(mb_hash,TBTable,MBHashA,MBHashB);
00496   InitializeHashTable(mb_hash,MBTable,MBHashA,MBHashB);
00497   InitializeHashTable(mb_hash,EXTable,MBHashA,MBHashB);
00498   /*
00499     Uncompress 1D Huffman to runlength encoded pixels.
00500   */
00501   byte=0;
00502   mask=0;
00503   null_lines=0;
00504   runlength=0;
00505   while (runlength < 11)
00506    InputBit(bit);
00507   do { InputBit(bit); } while ((int) bit == 0);
00508   image->x_resolution=204.0;
00509   image->y_resolution=196.0;
00510   image->units=PixelsPerInchResolution;
00511   exception=(&image->exception);
00512   for (y=0; ((y < (long) image->rows) && (null_lines < 3)); )
00513   {
00514     register long
00515       x;
00516 
00517     register PixelPacket
00518       *__restrict q;
00519 
00520     /*
00521       Initialize scanline to white.
00522     */
00523     p=scanline;
00524     for (x=0; x < (long) image->columns; x++)
00525       *p++=(unsigned char) 0;
00526     /*
00527       Decode Huffman encoded scanline.
00528     */
00529     color=MagickTrue;
00530     code=0;
00531     count=0;
00532     length=0;
00533     runlength=0;
00534     x=0;
00535     for ( ; ; )
00536     {
00537       if (byte == EOF)
00538         break;
00539       if (x >= (long) image->columns)
00540         {
00541           while (runlength < 11)
00542             InputBit(bit);
00543           do { InputBit(bit); } while ((int) bit == 0);
00544           break;
00545         }
00546       bail=MagickFalse;
00547       do
00548       {
00549         if (runlength < 11)
00550           InputBit(bit)
00551         else
00552           {
00553             InputBit(bit);
00554             if ((int) bit != 0)
00555               {
00556                 null_lines++;
00557                 if (x != 0)
00558                   null_lines=0;
00559                 bail=MagickTrue;
00560                 break;
00561               }
00562           }
00563         code=(code << 1)+(unsigned long) bit;
00564         length++;
00565       } while (code == 0);
00566       if (bail != MagickFalse)
00567         break;
00568       if (length > 13)
00569         {
00570           while (runlength < 11)
00571            InputBit(bit);
00572           do { InputBit(bit); } while ((int) bit == 0);
00573           break;
00574         }
00575       if (color != MagickFalse)
00576         {
00577           if (length < 4)
00578             continue;
00579           entry=mw_hash[((length+MWHashA)*(code+MWHashB)) % HashSize];
00580         }
00581       else
00582         {
00583           if (length < 2)
00584             continue;
00585           entry=mb_hash[((length+MBHashA)*(code+MBHashB)) % HashSize];
00586         }
00587       if (entry == (const HuffmanTable *) NULL)
00588         continue;
00589       if ((entry->length != length) || (entry->code != code))
00590         continue;
00591       switch (entry->id)
00592       {
00593         case TWId:
00594         case TBId:
00595         {
00596           count+=entry->count;
00597           if ((x+count) > (long) image->columns)
00598             count=(ssize_t) image->columns-x;
00599           if (count > 0)
00600             {
00601               if (color != MagickFalse)
00602                 {
00603                   x+=count;
00604                   count=0;
00605                 }
00606               else
00607                 for ( ; count > 0; count--)
00608                   scanline[x++]=(unsigned char) 1;
00609             }
00610           color=(unsigned int)
00611             ((color == MagickFalse) ? MagickTrue : MagickFalse);
00612           break;
00613         }
00614         case MWId:
00615         case MBId:
00616         case EXId:
00617         {
00618           count+=entry->count;
00619           break;
00620         }
00621         default:
00622           break;
00623       }
00624       code=0;
00625       length=0;
00626     }
00627     /*
00628       Transfer scanline to image pixels.
00629     */
00630     p=scanline;
00631     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
00632     if (q == (PixelPacket *) NULL)
00633       break;
00634     indexes=GetAuthenticIndexQueue(image);
00635     for (x=0; x < (long) image->columns; x++)
00636     {
00637       index=(IndexPacket) (*p++);
00638       indexes[x]=index;
00639       *q++=image->colormap[(long) index];
00640     }
00641     if (SyncAuthenticPixels(image,exception) == MagickFalse)
00642       break;
00643     proceed=SetImageProgress(image,LoadImageTag,y,image->rows);
00644     if (proceed == MagickFalse)
00645       break;
00646     y++;
00647   }
00648   image->rows=(unsigned long) MagickMax((size_t) y-3,1);
00649   image->compression=FaxCompression;
00650   /*
00651     Free decoder memory.
00652   */
00653   mw_hash=(HuffmanTable **) RelinquishMagickMemory(mw_hash);
00654   mb_hash=(HuffmanTable **) RelinquishMagickMemory(mb_hash);
00655   scanline=(unsigned char *) RelinquishMagickMemory(scanline);
00656   return(MagickTrue);
00657 }
00658 
00659 /*
00660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00661 %                                                                             %
00662 %                                                                             %
00663 %                                                                             %
00664 %   H u f f m a n E n c o d e I m a g e                                       %
00665 %                                                                             %
00666 %                                                                             %
00667 %                                                                             %
00668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00669 %
00670 %  HuffmanEncodeImage() compresses an image via Huffman-coding.
00671 %
00672 %  The format of the HuffmanEncodeImage method is:
00673 %
00674 %      MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
00675 %        Image *image,Image *inject_image)
00676 %
00677 %  A description of each parameter follows:
00678 %
00679 %    o image_info: the image info..
00680 %
00681 %    o image: the image.
00682 %
00683 %    o inject_image: inject into the image stream.
00684 %
00685 */
00686 MagickExport MagickBooleanType HuffmanEncodeImage(const ImageInfo *image_info,
00687   Image *image,Image *inject_image)
00688 {
00689 #define HuffmanOutputCode(entry)  \
00690 {  \
00691   mask=1 << (entry->length-1);  \
00692   while (mask != 0)  \
00693   {  \
00694     OutputBit(((entry->code & mask) != 0 ? 1 : 0));  \
00695     mask>>=1;  \
00696   }  \
00697 }
00698 
00699 #define OutputBit(count)  \
00700 {  \
00701   if (count > 0)  \
00702     byte=byte | bit;  \
00703   bit>>=1;  \
00704   if ((int) (bit & 0xff) == 0)   \
00705     {  \
00706       if (LocaleCompare(image_info->magick,"FAX") == 0) \
00707         (void) WriteBlobByte(image,(unsigned char) byte);  \
00708       else \
00709         Ascii85Encode(image,byte); \
00710       byte='\0';  \
00711       bit=(unsigned char) 0x80;  \
00712     }  \
00713 }
00714 
00715   const HuffmanTable
00716     *entry;
00717 
00718   ExceptionInfo
00719     *exception;
00720 
00721   int
00722     k,
00723     runlength;
00724 
00725   long
00726     n,
00727     y;
00728 
00729   Image
00730     *huffman_image;
00731 
00732   MagickBooleanType
00733     proceed;
00734 
00735   register long
00736     i,
00737     x;
00738 
00739   register const PixelPacket
00740     *p;
00741 
00742   register unsigned char
00743     *q;
00744 
00745   unsigned char
00746     byte,
00747     bit,
00748     *scanline;
00749 
00750   unsigned long
00751     mask,
00752     width;
00753 
00754   /*
00755     Allocate scanline buffer.
00756   */
00757   assert(image_info != (ImageInfo *) NULL);
00758   assert(image_info->signature == MagickSignature);
00759   assert(image != (Image *) NULL);
00760   assert(image->signature == MagickSignature);
00761   if (image->debug != MagickFalse)
00762     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00763   assert(inject_image != (Image *) NULL);
00764   assert(inject_image->signature == MagickSignature);
00765   width=inject_image->columns;
00766   if (LocaleCompare(image_info->magick,"FAX") == 0)
00767     width=(unsigned long) MagickMax(inject_image->columns,1728);
00768   scanline=(unsigned char *) AcquireQuantumMemory((size_t) width+1UL,
00769     sizeof(*scanline));
00770   if (scanline == (unsigned char *) NULL)
00771     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
00772       inject_image->filename);
00773   (void) ResetMagickMemory(scanline,0,width*sizeof(*scanline));
00774   huffman_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
00775   if (huffman_image == (Image *) NULL)
00776     {
00777       scanline=(unsigned char *) RelinquishMagickMemory(scanline);
00778       return(MagickFalse);
00779     }
00780   (void) SetImageType(huffman_image,BilevelType);
00781   byte='\0';
00782   bit=(unsigned char) 0x80;
00783   if (LocaleCompare(image_info->magick,"FAX") != 0)
00784     Ascii85Initialize(image);
00785   else
00786     {
00787       /*
00788         End of line.
00789       */
00790       for (k=0; k < 11; k++)
00791         OutputBit(0);
00792       OutputBit(1);
00793     }
00794   /*
00795     Compress to 1D Huffman pixels.
00796   */
00797   exception=(&huffman_image->exception);
00798   q=scanline;
00799   for (y=0; y < (long) huffman_image->rows; y++)
00800   {
00801     p=GetVirtualPixels(huffman_image,0,y,huffman_image->columns,1,exception);
00802     if (p == (const PixelPacket *) NULL)
00803       break;
00804     for (x=0; x < (long) huffman_image->columns; x++)
00805     {
00806       *q++=(unsigned char) (PixelIntensity(p) >= ((MagickRealType)
00807         QuantumRange/2.0) ? 0 : 1);
00808       p++;
00809     }
00810     /*
00811       Huffman encode scanline.
00812     */
00813     q=scanline;
00814     for (n=(long) width; n > 0; )
00815     {
00816       /*
00817         Output white run.
00818       */
00819       for (runlength=0; ((n > 0) && (*q == 0)); n--)
00820       {
00821         q++;
00822         runlength++;
00823       }
00824       if (runlength >= 64)
00825         {
00826           if (runlength < 1792)
00827             entry=MWTable+((runlength/64)-1);
00828           else
00829             entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
00830           runlength-=entry->count;
00831           HuffmanOutputCode(entry);
00832         }
00833       entry=TWTable+MagickMin((size_t) runlength,63);
00834       HuffmanOutputCode(entry);
00835       if (n != 0)
00836         {
00837           /*
00838             Output black run.
00839           */
00840           for (runlength=0; ((*q != 0) && (n > 0)); n--)
00841           {
00842             q++;
00843             runlength++;
00844           }
00845           if (runlength >= 64)
00846             {
00847               entry=MBTable+((runlength/64)-1);
00848               if (runlength >= 1792)
00849                 entry=EXTable+(MagickMin((size_t) runlength,2560)-1792)/64;
00850               runlength-=entry->count;
00851               HuffmanOutputCode(entry);
00852             }
00853           entry=TBTable+MagickMin((size_t) runlength,63);
00854           HuffmanOutputCode(entry);
00855         }
00856     }
00857     /*
00858       End of line.
00859     */
00860     for (k=0; k < 11; k++)
00861       OutputBit(0);
00862     OutputBit(1);
00863     q=scanline;
00864     if (GetPreviousImageInList(huffman_image) == (Image *) NULL)
00865       {
00866         proceed=SetImageProgress(huffman_image,LoadImageTag,y,
00867           huffman_image->rows);
00868         if (proceed == MagickFalse)
00869           break;
00870       }
00871   }
00872   /*
00873     End of page.
00874   */
00875   for (i=0; i < 6; i++)
00876   {
00877     for (k=0; k < 11; k++)
00878       OutputBit(0);
00879     OutputBit(1);
00880   }
00881   /*
00882     Flush bits.
00883   */
00884   if (((int) bit != 0x80) != 0)
00885     {
00886       if (LocaleCompare(image_info->magick,"FAX") == 0)
00887         (void) WriteBlobByte(image,byte);
00888       else
00889         Ascii85Encode(image,byte);
00890     }
00891   if (LocaleCompare(image_info->magick,"FAX") != 0)
00892     Ascii85Flush(image);
00893   huffman_image=DestroyImage(huffman_image);
00894   scanline=(unsigned char *) RelinquishMagickMemory(scanline);
00895   return(MagickTrue);
00896 }
00897 
00898 #if defined(MAGICKCORE_TIFF_DELEGATE)
00899 /*
00900 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00901 %                                                                             %
00902 %                                                                             %
00903 %                                                                             %
00904 %   H u f f m a n 2 D E n c o d e I m a g e                                   %
00905 %                                                                             %
00906 %                                                                             %
00907 %                                                                             %
00908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00909 %
00910 %  Huffman2DEncodeImage() compresses an image via two-dimensional
00911 %  Huffman-coding.
00912 %
00913 %  The format of the Huffman2DEncodeImage method is:
00914 %
00915 %      MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
00916 %        Image *image,Image *inject_image)
00917 %
00918 %  A description of each parameter follows:
00919 %
00920 %    o image_info: the image info.
00921 %
00922 %    o image: the image.
00923 %
00924 %    o inject_image: inject into the image stream.
00925 %
00926 */
00927 MagickExport MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
00928   Image *image,Image *inject_image)
00929 {
00930   char
00931     filename[MaxTextExtent];
00932 
00933   FILE
00934     *file;
00935 
00936   Image
00937     *huffman_image;
00938 
00939   ImageInfo
00940     *write_info;
00941 
00942   int
00943     unique_file;
00944 
00945   MagickBooleanType
00946     status;
00947 
00948   register long
00949     i;
00950 
00951   ssize_t
00952     count;
00953 
00954   TIFF
00955     *tiff;
00956 
00957   uint16
00958     fillorder;
00959 
00960   uint32
00961     *byte_count,
00962     strip_size;
00963 
00964   unsigned char
00965     *buffer;
00966 
00967   /*
00968     Write image as CCITTFax4 TIFF image to a temporary file.
00969   */
00970   assert(image_info != (const ImageInfo *) NULL);
00971   assert(image_info->signature == MagickSignature);
00972   assert(image != (Image *) NULL);
00973   assert(image->signature == MagickSignature);
00974   if (image->debug != MagickFalse)
00975     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00976   assert(inject_image != (Image *) NULL);
00977   assert(inject_image->signature == MagickSignature);
00978   huffman_image=CloneImage(inject_image,0,0,MagickTrue,&image->exception);
00979   if (huffman_image == (Image *) NULL)
00980     return(MagickFalse);
00981   file=(FILE *) NULL;
00982   unique_file=AcquireUniqueFileResource(filename);
00983   if (unique_file != -1)
00984     file=fdopen(unique_file,"wb"); 
00985   if ((unique_file == -1) || (file == (FILE *) NULL))
00986     {
00987       ThrowFileException(&image->exception,FileOpenError,
00988         "UnableToCreateTemporaryFile",filename);
00989       return(MagickFalse);
00990     }
00991   (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
00992     filename);
00993   write_info=CloneImageInfo(image_info);
00994   SetImageInfoFile(write_info,file);
00995   write_info->compression=Group4Compression;
00996   (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
00997   status=WriteImage(write_info,huffman_image);
00998   (void) fflush(file);
00999   write_info=DestroyImageInfo(write_info);
01000   if (status == MagickFalse)
01001     return(MagickFalse);
01002   tiff=TIFFOpen(filename,"rb");
01003   if (tiff == (TIFF *) NULL)
01004     {
01005       huffman_image=DestroyImage(huffman_image);
01006       (void) fclose(file);
01007       (void) RelinquishUniqueFileResource(filename);
01008       ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
01009         image_info->filename);
01010       return(MagickFalse);
01011     }
01012   /*
01013     Allocate raw strip buffer.
01014   */
01015   byte_count=0;
01016   (void) TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count);
01017   strip_size=byte_count[0];
01018   for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
01019     if (byte_count[i] > strip_size)
01020       strip_size=byte_count[i];
01021   buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
01022     sizeof(*buffer));
01023   if (buffer == (unsigned char *) NULL)
01024     {
01025       TIFFClose(tiff);
01026       huffman_image=DestroyImage(huffman_image);
01027       (void) fclose(file);
01028       (void) RelinquishUniqueFileResource(filename);
01029       ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
01030         image_info->filename);
01031     }
01032   /*
01033     Compress runlength encoded to 2D Huffman pixels.
01034   */
01035   fillorder=FILLORDER_LSB2MSB;
01036   (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&fillorder);
01037   for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
01038   {
01039     count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,(long)
01040       byte_count[i]);
01041     if (fillorder == FILLORDER_LSB2MSB)
01042       TIFFReverseBits(buffer,(unsigned long) count);
01043     (void) WriteBlob(image,(size_t) count,buffer);
01044   }
01045   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
01046   TIFFClose(tiff);
01047   huffman_image=DestroyImage(huffman_image);
01048   (void) fclose(file);
01049   (void) RelinquishUniqueFileResource(filename);
01050   return(MagickTrue);
01051 }
01052 #else
01053 MagickExport MagickBooleanType Huffman2DEncodeImage(const ImageInfo *image_info,
01054   Image *image,Image *inject_image)
01055 {
01056   assert(image_info != (const ImageInfo *) NULL);
01057   assert(image_info->signature == MagickSignature);
01058   assert(image != (Image *) NULL);
01059   assert(image->signature == MagickSignature);
01060   (void) inject_image;
01061   if (image->debug != MagickFalse)
01062     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01063   (void) ThrowMagickException(&image->exception,GetMagickModule(),
01064     MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (TIFF)",
01065     image->filename);
01066   return(MagickFalse);
01067 }
01068 #endif
01069 
01070 /*
01071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01072 %                                                                             %
01073 %                                                                             %
01074 %                                                                             %
01075 %   L Z W E n c o d e I m a g e                                               %
01076 %                                                                             %
01077 %                                                                             %
01078 %                                                                             %
01079 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01080 %
01081 %  LZWEncodeImage() compresses an image via LZW-coding specific to Postscript
01082 %  Level II or Portable Document Format.
01083 %
01084 %  The format of the LZWEncodeImage method is:
01085 %
01086 %      MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
01087 %        unsigned char *pixels)
01088 %
01089 %  A description of each parameter follows:
01090 %
01091 %    o image: the image.
01092 %
01093 %    o length:  A value that specifies the number of pixels to compress.
01094 %
01095 %    o pixels: the address of an unsigned array of characters containing the
01096 %      pixels to compress.
01097 %
01098 */
01099 MagickExport MagickBooleanType LZWEncodeImage(Image *image,const size_t length,
01100   unsigned char *pixels)
01101 {
01102 #define LZWClr  256UL  /* Clear Table Marker */
01103 #define LZWEod  257UL  /* End of Data marker */
01104 #define OutputCode(code) \
01105 { \
01106     accumulator+=code << (32-code_width-number_bits); \
01107     number_bits+=code_width; \
01108     while (number_bits >= 8) \
01109     { \
01110         (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24)); \
01111         accumulator=accumulator << 8; \
01112         number_bits-=8; \
01113     } \
01114 }
01115 
01116   typedef struct _TableType
01117   {
01118     long
01119       prefix,
01120       suffix,
01121       next;
01122   } TableType;
01123 
01124   long
01125     index;
01126 
01127   register long
01128     i;
01129 
01130   TableType
01131     *table;
01132 
01133   unsigned long
01134     accumulator,
01135     number_bits,
01136     code_width,
01137     last_code,
01138     next_index;
01139 
01140   /*
01141     Allocate string table.
01142   */
01143   assert(image != (Image *) NULL);
01144   assert(image->signature == MagickSignature);
01145   if (image->debug != MagickFalse)
01146     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01147   assert(pixels != (unsigned char *) NULL);
01148   table=(TableType *) AcquireQuantumMemory(1UL << 12,sizeof(*table));
01149   if (table == (TableType *) NULL)
01150     return(MagickFalse);
01151   /*
01152     Initialize variables.
01153   */
01154   accumulator=0;
01155   code_width=9;
01156   number_bits=0;
01157   last_code=0;
01158   OutputCode(LZWClr);
01159   for (index=0; index < 256; index++)
01160   {
01161     table[index].prefix=(-1);
01162     table[index].suffix=(short) index;
01163     table[index].next=(-1);
01164   }
01165   next_index=LZWEod+1;
01166   code_width=9;
01167   last_code=(unsigned long) pixels[0];
01168   for (i=1; i < (long) length; i++)
01169   {
01170     /*
01171       Find string.
01172     */
01173     index=(long) last_code;
01174     while (index != -1)
01175       if ((table[index].prefix != (long) last_code) ||
01176           (table[index].suffix != (long) pixels[i]))
01177         index=table[index].next;
01178       else
01179         {
01180           last_code=(unsigned long) index;
01181           break;
01182         }
01183     if (last_code != (unsigned long) index)
01184       {
01185         /*
01186           Add string.
01187         */
01188         OutputCode(last_code);
01189         table[next_index].prefix=(long) last_code;
01190         table[next_index].suffix=(short) pixels[i];
01191         table[next_index].next=table[last_code].next;
01192         table[last_code].next=(long) next_index;
01193         next_index++;
01194         /*
01195           Did we just move up to next bit width?
01196         */
01197         if ((next_index >> code_width) != 0)
01198           {
01199             code_width++;
01200             if (code_width > 12)
01201               {
01202                 /*
01203                   Did we overflow the max bit width?
01204                 */
01205                 code_width--;
01206                 OutputCode(LZWClr);
01207                 for (index=0; index < 256; index++)
01208                 {
01209                   table[index].prefix=(-1);
01210                   table[index].suffix=index;
01211                   table[index].next=(-1);
01212                 }
01213                 next_index=LZWEod+1;
01214                 code_width=9;
01215               }
01216             }
01217           last_code=(unsigned long) pixels[i];
01218       }
01219   }
01220   /*
01221     Flush tables.
01222   */
01223   OutputCode(last_code);
01224   OutputCode(LZWEod);
01225   if (number_bits != 0)
01226     (void) WriteBlobByte(image,(unsigned char) (accumulator >> 24));
01227   table=(TableType *) RelinquishMagickMemory(table);
01228   return(MagickTrue);
01229 }
01230 
01231 /*
01232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01233 %                                                                             %
01234 %                                                                             %
01235 %                                                                             %
01236 %   P a c k b i t s E n c o d e I m a g e                                     %
01237 %                                                                             %
01238 %                                                                             %
01239 %                                                                             %
01240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01241 %
01242 %  PackbitsEncodeImage() compresses an image via Macintosh Packbits encoding
01243 %  specific to Postscript Level II or Portable Document Format.  To ensure
01244 %  portability, the binary Packbits bytes are encoded as ASCII Base-85.
01245 %
01246 %  The format of the PackbitsEncodeImage method is:
01247 %
01248 %      MagickBooleanType PackbitsEncodeImage(Image *image,const size_t length,
01249 %        unsigned char *pixels)
01250 %
01251 %  A description of each parameter follows:
01252 %
01253 %    o image: the image.
01254 %
01255 %    o length:  A value that specifies the number of pixels to compress.
01256 %
01257 %    o pixels: the address of an unsigned array of characters containing the
01258 %      pixels to compress.
01259 %
01260 */
01261 MagickExport MagickBooleanType PackbitsEncodeImage(Image *image,
01262   const size_t length,unsigned char *pixels)
01263 {
01264   int
01265     count;
01266 
01267   register long
01268     i,
01269     j;
01270 
01271   unsigned char
01272     *packbits;
01273 
01274   /*
01275     Compress pixels with Packbits encoding.
01276   */
01277   assert(image != (Image *) NULL);
01278   assert(image->signature == MagickSignature);
01279   if (image->debug != MagickFalse)
01280     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01281   assert(pixels != (unsigned char *) NULL);
01282   packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
01283   if (packbits == (unsigned char *) NULL)
01284     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
01285       image->filename);
01286   for (i=(long) length; i != 0; )
01287   {
01288     switch (i)
01289     {
01290       case 1:
01291       {
01292         i--;
01293         (void) WriteBlobByte(image,(unsigned char) 0);
01294         (void) WriteBlobByte(image,*pixels);
01295         break;
01296       }
01297       case 2:
01298       {
01299         i-=2;
01300         (void) WriteBlobByte(image,(unsigned char) 1);
01301         (void) WriteBlobByte(image,*pixels);
01302         (void) WriteBlobByte(image,pixels[1]);
01303         break;
01304       }
01305       case 3:
01306       {
01307         i-=3;
01308         if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
01309           {
01310             (void) WriteBlobByte(image,(unsigned char) ((256-3)+1));
01311             (void) WriteBlobByte(image,*pixels);
01312             break;
01313           }
01314         (void) WriteBlobByte(image,(unsigned char) 2);
01315         (void) WriteBlobByte(image,*pixels);
01316         (void) WriteBlobByte(image,pixels[1]);
01317         (void) WriteBlobByte(image,pixels[2]);
01318         break;
01319       }
01320       default:
01321       {
01322         if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
01323           {
01324             /*
01325               Packed run.
01326             */
01327             count=3;
01328             while (((long) count < i) && (*pixels == *(pixels+count)))
01329             {
01330               count++;
01331               if (count >= 127)
01332                 break;
01333             }
01334             i-=count;
01335             (void) WriteBlobByte(image,(unsigned char) ((256-count)+1));
01336             (void) WriteBlobByte(image,*pixels);
01337             pixels+=count;
01338             break;
01339           }
01340         /*
01341           Literal run.
01342         */
01343         count=0;
01344         while ((*(pixels+count) != *(pixels+count+1)) ||
01345                (*(pixels+count+1) != *(pixels+count+2)))
01346         {
01347           packbits[count+1]=pixels[count];
01348           count++;
01349           if (((long) count >= (i-3)) || (count >= 127))
01350             break;
01351         }
01352         i-=count;
01353         *packbits=(unsigned char) (count-1);
01354         for (j=0; j <= (long) count; j++)
01355           (void) WriteBlobByte(image,packbits[j]);
01356         pixels+=count;
01357         break;
01358       }
01359     }
01360   }
01361   (void) WriteBlobByte(image,(unsigned char) 128);  /* EOD marker */
01362   packbits=(unsigned char *) RelinquishMagickMemory(packbits);
01363   return(MagickTrue);
01364 }
01365 
01366 #if defined(MAGICKCORE_ZLIB_DELEGATE)
01367 /*
01368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01369 %                                                                             %
01370 %                                                                             %
01371 %                                                                             %
01372 %   Z L I B E n c o d e I m a g e                                             %
01373 %                                                                             %
01374 %                                                                             %
01375 %                                                                             %
01376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01377 %
01378 %  ZLIBEncodeImage compresses an image via ZLIB-coding specific to
01379 %  Postscript Level II or Portable Document Format.
01380 %
01381 %  The format of the ZLIBEncodeImage method is:
01382 %
01383 %      MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
01384 %        unsigned char *pixels)
01385 %
01386 %  A description of each parameter follows:
01387 %
01388 %    o file: the address of a structure of type FILE.  ZLIB encoded pixels
01389 %      are written to this file.
01390 %
01391 %    o length:  A value that specifies the number of pixels to compress.
01392 %
01393 %    o pixels: the address of an unsigned array of characters containing the
01394 %      pixels to compress.
01395 %
01396 */
01397 
01398 static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
01399   unsigned int size)
01400 {
01401   (void) context;
01402   return((voidpf) AcquireQuantumMemory(items,size));
01403 }
01404 
01405 static void RelinquishZIPMemory(voidpf context,voidpf memory)
01406 {
01407   (void) context;
01408   memory=RelinquishMagickMemory(memory);
01409 }
01410 
01411 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,const size_t length,
01412   unsigned char *pixels)
01413 {
01414   int
01415     status;
01416 
01417   register long
01418     i;
01419 
01420   size_t
01421     compress_packets;
01422 
01423   unsigned char
01424     *compress_pixels;
01425 
01426   z_stream
01427     stream;
01428 
01429   assert(image != (Image *) NULL);
01430   assert(image->signature == MagickSignature);
01431   if (image->debug != MagickFalse)
01432     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01433   compress_packets=(size_t) (1.001*length+12);
01434   compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_packets,
01435     sizeof(*compress_pixels));
01436   if (compress_pixels == (unsigned char *) NULL)
01437     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
01438       image->filename);
01439   stream.next_in=pixels;
01440   stream.avail_in=(unsigned int) length;
01441   stream.next_out=compress_pixels;
01442   stream.avail_out=(unsigned int) compress_packets;
01443   stream.zalloc=AcquireZIPMemory;
01444   stream.zfree=RelinquishZIPMemory;
01445   stream.opaque=(voidpf) NULL;
01446   status=deflateInit(&stream,(int) (image->quality ==
01447     UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
01448   if (status == Z_OK)
01449     {
01450       status=deflate(&stream,Z_FINISH);
01451       if (status == Z_STREAM_END)
01452         status=deflateEnd(&stream);
01453       else
01454         (void) deflateEnd(&stream);
01455       compress_packets=(size_t) stream.total_out;
01456     }
01457   if (status != Z_OK)
01458     ThrowBinaryException(CoderError,"UnableToZipCompressImage",image->filename)
01459   else
01460     for (i=0; i < (long) compress_packets; i++)
01461       (void) WriteBlobByte(image,compress_pixels[i]);
01462   compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
01463   return(status == Z_OK ? MagickTrue : MagickFalse);
01464 }
01465 #else
01466 MagickExport MagickBooleanType ZLIBEncodeImage(Image *image,
01467   const size_t magick_unused(length),unsigned char *magick_unused(pixels))
01468 {
01469   assert(image != (Image *) NULL);
01470   assert(image->signature == MagickSignature);
01471   if (image->debug != MagickFalse)
01472     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
01473   (void) ThrowMagickException(&image->exception,GetMagickModule(),
01474     MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (ZIP)",
01475     image->filename);
01476   return(MagickFalse);
01477 }
01478 #endif

Generated on Thu Jul 2 12:03:14 2009 for MagickCore by  doxygen 1.5.8