2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 % Read DR Halo Image Format %
19 % Permission is hereby granted, free of charge, to any person obtaining a %
20 % copy of this software and associated documentation files ("ImageMagick"), %
21 % to deal in ImageMagick without restriction, including without limitation %
22 % the rights to use, copy, modify, merge, publish, distribute, sublicense, %
23 % and/or sell copies of ImageMagick, and to permit persons to whom the %
24 % ImageMagick is furnished to do so, subject to the following conditions: %
26 % The above copyright notice and this permission notice shall be included in %
27 % all copies or substantial portions of ImageMagick. %
29 % The software is provided "as is", without warranty of any kind, express or %
30 % implied, including but not limited to the warranties of merchantability, %
31 % fitness for a particular purpose and noninfringement. In no event shall %
32 % ImageMagick Studio be liable for any claim, damages or other liability, %
33 % whether in an action of contract, tort or otherwise, arising from, out of %
34 % or in connection with ImageMagick or the use or other dealings in %
37 % Except as contained in this notice, the name of the ImageMagick Studio %
38 % shall not be used in advertising or otherwise to promote the sale, use or %
39 % other dealings in ImageMagick without prior written authorization from the %
40 % ImageMagick Studio. %
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 #include "MagickCore/studio.h"
51 #include "MagickCore/attribute.h"
52 #include "MagickCore/blob.h"
53 #include "MagickCore/blob-private.h"
54 #include "MagickCore/cache.h"
55 #include "MagickCore/color.h"
56 #include "MagickCore/color-private.h"
57 #include "MagickCore/colormap.h"
58 #include "MagickCore/colormap-private.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/image.h"
62 #include "MagickCore/image-private.h"
63 #include "MagickCore/list.h"
64 #include "MagickCore/magick.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/pixel-accessor.h"
67 #include "MagickCore/quantum-private.h"
68 #include "MagickCore/static.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/module.h"
71 #include "MagickCore/utility.h"
72 #include "MagickCore/utility-private.h"
89 unsigned GraphicsMode;
98 static void InsertRow(Image *image,ssize_t depth,unsigned char *p,ssize_t y,
99 ExceptionInfo *exception)
101 size_t bit; ssize_t x;
108 case 1: /* Convert bitmap scanline. */
110 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
111 if (q == (Quantum *) NULL)
113 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
115 for (bit=0; bit < 8; bit++)
117 index=(Quantum) ((((*p) & (0x80 >> bit)) != 0) ? 0x01 : 0x00);
118 SetPixelIndex(image,index,q);
119 q+=GetPixelChannels(image);
123 if ((image->columns % 8) != 0)
125 for (bit=0; bit < (image->columns % 8); bit++)
127 index=(Quantum) ((((*p) & (0x80 >> bit)) != 0) ? 0x01 : 0x00);
128 SetPixelIndex(image,index,q);
129 q+=GetPixelChannels(image);
133 if (SyncAuthenticPixels(image,exception) == MagickFalse)
137 case 2: /* Convert PseudoColor scanline. */
139 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
140 if (q == (Quantum *) NULL)
142 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
144 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
145 SetPixelIndex(image,index,q);
146 q+=GetPixelChannels(image);
147 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
148 SetPixelIndex(image,index,q);
149 q+=GetPixelChannels(image);
150 index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,exception);
151 SetPixelIndex(image,index,q);
152 q+=GetPixelChannels(image);
153 index=ConstrainColormapIndex(image,(*p) & 0x3,exception);
154 SetPixelIndex(image,index,q);
155 q+=GetPixelChannels(image);
158 if ((image->columns % 4) != 0)
160 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
161 SetPixelIndex(image,index,q);
162 q+=GetPixelChannels(image);
163 if ((image->columns % 4) >= 1)
166 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
167 SetPixelIndex(image,index,q);
168 q+=GetPixelChannels(image);
169 if ((image->columns % 4) >= 2)
172 index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,
174 SetPixelIndex(image,index,q);
175 q+=GetPixelChannels(image);
180 if (SyncAuthenticPixels(image,exception) == MagickFalse)
185 case 4: /* Convert PseudoColor scanline. */
187 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
188 if (q == (Quantum *) NULL)
190 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
192 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
193 SetPixelIndex(image,index,q);
194 q+=GetPixelChannels(image);
195 index=ConstrainColormapIndex(image,(*p) & 0xf,exception);
196 SetPixelIndex(image,index,q);
197 q+=GetPixelChannels(image);
200 if ((image->columns % 2) != 0)
202 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
203 SetPixelIndex(image,index,q);
204 q+=GetPixelChannels(image);
207 if (SyncAuthenticPixels(image,exception) == MagickFalse)
211 case 8: /* Convert PseudoColor scanline. */
213 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
214 if (q == (Quantum *) NULL) break;
215 for (x=0; x < (ssize_t) image->columns; x++)
217 index=ConstrainColormapIndex(image,*p,exception);
218 SetPixelIndex(image,index,q);
220 q+=GetPixelChannels(image);
222 if (SyncAuthenticPixels(image,exception) == MagickFalse)
231 Compute the number of colors in Grayed R[i]=G[i]=B[i] image
233 static int GetCutColors(Image *image,ExceptionInfo *exception)
247 scale_intensity=ScaleCharToQuantum(16);
248 for (y=0; y < (ssize_t) image->rows; y++)
250 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
251 for (x=0; x < (ssize_t) image->columns; x++)
253 if (intensity < GetPixelRed(image,q))
254 intensity=GetPixelRed(image,q);
255 if (intensity >= scale_intensity)
257 q+=GetPixelChannels(image);
260 if (intensity < ScaleCharToQuantum(2))
262 if (intensity < ScaleCharToQuantum(16))
264 return((int) intensity);
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 % R e a d C U T I m a g e %
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 % ReadCUTImage() reads an CUT X image file and returns it. It
279 % allocates the memory necessary for the new Image structure and returns a
280 % pointer to the new image.
282 % The format of the ReadCUTImage method is:
284 % Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
286 % A description of each parameter follows:
288 % o image_info: the image info.
290 % o exception: return any errors or warnings in this structure.
293 static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
295 Image *image,*palette;
296 ImageInfo *clone_info;
297 MagickBooleanType status;
303 unsigned char RunCount,RunValue,RunCountMasked;
305 CUTPalHeader PalHeader;
309 unsigned char *BImgBuff=NULL,*ptrB;
315 assert(image_info != (const ImageInfo *) NULL);
316 assert(image_info->signature == MagickSignature);
317 if (image_info->debug != MagickFalse)
318 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
319 image_info->filename);
320 assert(exception != (ExceptionInfo *) NULL);
321 assert(exception->signature == MagickSignature);
322 image=AcquireImage(image_info,exception);
323 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
324 if (status == MagickFalse)
326 image=DestroyImageList(image);
327 return((Image *) NULL);
334 Header.Width=ReadBlobLSBShort(image);
335 Header.Height=ReadBlobLSBShort(image);
336 Header.Reserved=ReadBlobLSBShort(image);
338 if (Header.Width==0 || Header.Height==0 || Header.Reserved!=0)
339 CUT_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
341 /*---This code checks first line of image---*/
342 EncodedByte=ReadBlobLSBShort(image);
343 RunCount=(unsigned char) ReadBlobByte(image);
344 RunCountMasked=RunCount & 0x7F;
346 while((int) RunCountMasked!=0) /*end of line?*/
349 if((int) RunCount<0x80) i=(ssize_t) RunCountMasked;
350 offset=SeekBlob(image,TellBlob(image)+i,SEEK_SET);
352 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
353 if(EOFBlob(image) != MagickFalse) goto CUT_KO; /*wrong data*/
355 ldblk+=(ssize_t) RunCountMasked;
357 RunCount=(unsigned char) ReadBlobByte(image);
358 if(EOFBlob(image) != MagickFalse) goto CUT_KO; /*wrong data: unexpected eof in line*/
359 RunCountMasked=RunCount & 0x7F;
361 if(EncodedByte!=1) goto CUT_KO; /*wrong data: size incorrect*/
362 i=0; /*guess a number of bit planes*/
363 if(ldblk==(int) Header.Width) i=8;
364 if(2*ldblk==(int) Header.Width) i=4;
365 if(8*ldblk==(int) Header.Width) i=1;
366 if(i==0) goto CUT_KO; /*wrong data: incorrect bit planes*/
369 image->columns=Header.Width;
370 image->rows=Header.Height;
372 image->colors=(size_t) (GetQuantumRange(1UL*i)+1);
374 if (image_info->ping) goto Finish;
376 /* ----- Do something with palette ----- */
377 if ((clone_info=CloneImageInfo(image_info)) == NULL) goto NoPalette;
380 i=(ssize_t) strlen(clone_info->filename);
384 if(clone_info->filename[i]=='.')
388 if(clone_info->filename[i]=='/' || clone_info->filename[i]=='\\' ||
389 clone_info->filename[i]==':' )
396 (void) CopyMagickString(clone_info->filename+i,".PAL",(size_t)
398 if((clone_info->file=fopen_utf8(clone_info->filename,"rb"))==NULL)
400 (void) CopyMagickString(clone_info->filename+i,".pal",(size_t)
402 if((clone_info->file=fopen_utf8(clone_info->filename,"rb"))==NULL)
404 clone_info->filename[i]='\0';
405 if((clone_info->file=fopen_utf8(clone_info->filename,"rb"))==NULL)
407 clone_info=DestroyImageInfo(clone_info);
414 if( (palette=AcquireImage(clone_info,exception))==NULL ) goto NoPalette;
415 status=OpenBlob(clone_info,palette,ReadBinaryBlobMode,exception);
416 if (status == MagickFalse)
419 palette=DestroyImage(palette);
427 (void) ReadBlob(palette,2,(unsigned char *) PalHeader.FileId);
428 if(strncmp(PalHeader.FileId,"AH",2) != 0) goto ErasePalette;
429 PalHeader.Version=ReadBlobLSBShort(palette);
430 PalHeader.Size=ReadBlobLSBShort(palette);
431 PalHeader.FileType=(char) ReadBlobByte(palette);
432 PalHeader.SubType=(char) ReadBlobByte(palette);
433 PalHeader.BoardID=ReadBlobLSBShort(palette);
434 PalHeader.GraphicsMode=ReadBlobLSBShort(palette);
435 PalHeader.MaxIndex=ReadBlobLSBShort(palette);
436 PalHeader.MaxRed=ReadBlobLSBShort(palette);
437 PalHeader.MaxGreen=ReadBlobLSBShort(palette);
438 PalHeader.MaxBlue=ReadBlobLSBShort(palette);
439 (void) ReadBlob(palette,20,(unsigned char *) PalHeader.PaletteId);
441 if(PalHeader.MaxIndex<1) goto ErasePalette;
442 image->colors=PalHeader.MaxIndex+1;
443 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) goto NoMemory;
445 if(PalHeader.MaxRed==0) PalHeader.MaxRed=(unsigned int) QuantumRange; /*avoid division by 0*/
446 if(PalHeader.MaxGreen==0) PalHeader.MaxGreen=(unsigned int) QuantumRange;
447 if(PalHeader.MaxBlue==0) PalHeader.MaxBlue=(unsigned int) QuantumRange;
449 for(i=0;i<=(int) PalHeader.MaxIndex;i++)
450 { /*this may be wrong- I don't know why is palette such strange*/
451 j=(ssize_t) TellBlob(palette);
455 offset=SeekBlob(palette,j,SEEK_SET);
457 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
459 image->colormap[i].red=(Quantum) ReadBlobLSBShort(palette);
460 if (QuantumRange != (Quantum) PalHeader.MaxRed)
462 image->colormap[i].red=ClampToQuantum(((double)
463 image->colormap[i].red*QuantumRange+(PalHeader.MaxRed>>1))/
466 image->colormap[i].green=(Quantum) ReadBlobLSBShort(palette);
467 if (QuantumRange != (Quantum) PalHeader.MaxGreen)
469 image->colormap[i].green=ClampToQuantum
470 (((double) image->colormap[i].green*QuantumRange+(PalHeader.MaxGreen>>1))/PalHeader.MaxGreen);
472 image->colormap[i].blue=(Quantum) ReadBlobLSBShort(palette);
473 if (QuantumRange != (Quantum) PalHeader.MaxBlue)
475 image->colormap[i].blue=ClampToQuantum
476 (((double)image->colormap[i].blue*QuantumRange+(PalHeader.MaxBlue>>1))/PalHeader.MaxBlue);
489 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
492 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
495 for (i=0; i < (ssize_t)image->colors; i++)
497 image->colormap[i].red=ScaleCharToQuantum((unsigned char) i);
498 image->colormap[i].green=ScaleCharToQuantum((unsigned char) i);
499 image->colormap[i].blue=ScaleCharToQuantum((unsigned char) i);
504 /* ----- Load RLE compressed raster ----- */
505 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
506 sizeof(*BImgBuff)); /*Ldblk was set in the check phase*/
507 if(BImgBuff==NULL) goto NoMemory;
509 offset=SeekBlob(image,6 /*sizeof(Header)*/,SEEK_SET);
511 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
512 for (i=0; i < (int) Header.Height; i++)
514 EncodedByte=ReadBlobLSBShort(image);
519 RunCount=(unsigned char) ReadBlobByte(image);
520 RunCountMasked=RunCount & 0x7F;
522 while ((int) RunCountMasked != 0)
524 if((ssize_t) RunCountMasked>j)
526 RunCountMasked=(unsigned char) j;
533 if((int) RunCount>0x80)
535 RunValue=(unsigned char) ReadBlobByte(image);
536 (void) ResetMagickMemory(ptrB,(int) RunValue,(size_t) RunCountMasked);
539 (void) ReadBlob(image,(size_t) RunCountMasked,ptrB);
542 ptrB+=(int) RunCountMasked;
543 j-=(int) RunCountMasked;
545 if (EOFBlob(image) != MagickFalse) goto Finish; /* wrong data: unexpected eof in line */
546 RunCount=(unsigned char) ReadBlobByte(image);
547 RunCountMasked=RunCount & 0x7F;
550 InsertRow(image,depth,BImgBuff,i,exception);
552 (void) SyncImage(image,exception);
555 /*detect monochrome image*/
558 { /*attempt to detect binary (black&white) images*/
559 if ((image->storage_class == PseudoClass) &&
560 (IsImageGray(image,exception) != MagickFalse))
562 if(GetCutColors(image,exception)==2)
564 for (i=0; i < (ssize_t)image->colors; i++)
568 sample=ScaleCharToQuantum((unsigned char) i);
569 if(image->colormap[i].red!=sample) goto Finish;
570 if(image->colormap[i].green!=sample) goto Finish;
571 if(image->colormap[i].blue!=sample) goto Finish;
574 image->colormap[1].red=image->colormap[1].green=
575 image->colormap[1].blue=QuantumRange;
576 for (i=0; i < (ssize_t)image->rows; i++)
578 q=QueueAuthenticPixels(image,0,i,image->columns,1,exception);
579 for (j=0; j < (ssize_t)image->columns; j++)
581 if (GetPixelRed(image,q) == ScaleCharToQuantum(1))
583 SetPixelRed(image,QuantumRange,q);
584 SetPixelGreen(image,QuantumRange,q);
585 SetPixelBlue(image,QuantumRange,q);
587 q+=GetPixelChannels(image);
589 if (SyncAuthenticPixels(image,exception) == MagickFalse) goto Finish;
596 if (BImgBuff != NULL)
597 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
599 palette=DestroyImage(palette);
600 if (clone_info != NULL)
601 clone_info=DestroyImageInfo(clone_info);
602 if (EOFBlob(image) != MagickFalse)
603 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
605 (void) CloseBlob(image);
606 return(GetFirstImageInList(image));
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 % R e g i s t e r C U T I m a g e %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 % RegisterCUTImage() adds attributes for the CUT image format to
621 % the list of supported formats. The attributes include the image format
622 % tag, a method to read and/or write the format, whether the format
623 % supports the saving of more than one frame to the same file or blob,
624 % whether the format supports native in-memory I/O, and a brief
625 % description of the format.
627 % The format of the RegisterCUTImage method is:
629 % size_t RegisterCUTImage(void)
632 ModuleExport size_t RegisterCUTImage(void)
637 entry=SetMagickInfo("CUT");
638 entry->decoder=(DecodeImageHandler *) ReadCUTImage;
639 entry->seekable_stream=MagickTrue;
640 entry->description=ConstantString("DR Halo");
641 entry->module=ConstantString("CUT");
642 (void) RegisterMagickInfo(entry);
643 return(MagickImageCoderSignature);
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % U n r e g i s t e r C U T I m a g e %
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 % UnregisterCUTImage() removes format registrations made by the
658 % CUT module from the list of supported formats.
660 % The format of the UnregisterCUTImage method is:
662 % UnregisterCUTImage(void)
665 ModuleExport void UnregisterCUTImage(void)
667 (void) UnregisterMagickInfo("CUT");