| /* | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                             AAA   RRRR   TTTTT                              % | 
 | %                            A   A  R   R    T                                % | 
 | %                            AAAAA  RRRR     T                                % | 
 | %                            A   A  R R      T                                % | 
 | %                            A   A  R  R     T                                % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                 Support PFS: 1st Publisher Clip Art Format                  % | 
 | %                                                                             % | 
 | %                              Software Design                                % | 
 | %                                John Cristy                                  % | 
 | %                                 July 1992                                   % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      % | 
 | %  dedicated to making software imaging solutions freely available.           % | 
 | %                                                                             % | 
 | %  You may not use this file except in compliance with the License.  You may  % | 
 | %  obtain a copy of the License at                                            % | 
 | %                                                                             % | 
 | %    http://www.imagemagick.org/script/license.php                            % | 
 | %                                                                             % | 
 | %  Unless required by applicable law or agreed to in writing, software        % | 
 | %  distributed under the License is distributed on an "AS IS" BASIS,          % | 
 | %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   % | 
 | %  See the License for the specific language governing permissions and        % | 
 | %  limitations under the License.                                             % | 
 | %                                                                             % | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | % | 
 | % | 
 | */ | 
 |  | 
 | /* | 
 |   Include declarations. | 
 | */ | 
 | #include "MagickCore/studio.h" | 
 | #include "MagickCore/blob.h" | 
 | #include "MagickCore/blob-private.h" | 
 | #include "MagickCore/cache.h" | 
 | #include "MagickCore/color-private.h" | 
 | #include "MagickCore/colormap.h" | 
 | #include "MagickCore/colorspace.h" | 
 | #include "MagickCore/colorspace-private.h" | 
 | #include "MagickCore/exception.h" | 
 | #include "MagickCore/exception-private.h" | 
 | #include "MagickCore/image.h" | 
 | #include "MagickCore/image-private.h" | 
 | #include "MagickCore/list.h" | 
 | #include "MagickCore/magick.h" | 
 | #include "MagickCore/memory_.h" | 
 | #include "MagickCore/monitor.h" | 
 | #include "MagickCore/monitor-private.h" | 
 | #include "MagickCore/quantum-private.h" | 
 | #include "MagickCore/static.h" | 
 | #include "MagickCore/string_.h" | 
 | #include "MagickCore/module.h" | 
 |  | 
 | /* | 
 |   Forward declarations. | 
 | */ | 
 | static MagickBooleanType | 
 |   WriteARTImage(const ImageInfo *,Image *,ExceptionInfo *); | 
 |  | 
 | /* | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %   R e a d A R T I m a g e                                                   % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | % | 
 | %  ReadARTImage() reads an image of raw bits in LSB order and returns it. | 
 | %  It allocates the memory necessary for the new Image structure and returns | 
 | %  a pointer to the new image. | 
 | % | 
 | %  The format of the ReadARTImage method is: | 
 | % | 
 | %      Image *ReadARTImage(const ImageInfo *image_info, | 
 | %        ExceptionInfo *exception) | 
 | % | 
 | %  A description of each parameter follows: | 
 | % | 
 | %    o image_info: the image info. | 
 | % | 
 | %    o exception: return any errors or warnings in this structure. | 
 | % | 
 | */ | 
 | static Image *ReadARTImage(const ImageInfo *image_info,ExceptionInfo *exception) | 
 | { | 
 |   Image | 
 |     *image; | 
 |  | 
 |   QuantumInfo | 
 |     *quantum_info; | 
 |  | 
 |   QuantumType | 
 |     quantum_type; | 
 |  | 
 |   MagickBooleanType | 
 |     status; | 
 |  | 
 |   size_t | 
 |     length; | 
 |  | 
 |   ssize_t | 
 |     count, | 
 |     y; | 
 |  | 
 |   unsigned char | 
 |     *pixels; | 
 |  | 
 |   /* | 
 |     Open image file. | 
 |   */ | 
 |   assert(image_info != (const ImageInfo *) NULL); | 
 |   assert(image_info->signature == MagickSignature); | 
 |   if (image_info->debug != MagickFalse) | 
 |     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", | 
 |       image_info->filename); | 
 |   assert(exception != (ExceptionInfo *) NULL); | 
 |   assert(exception->signature == MagickSignature); | 
 |   image=AcquireImage(image_info,exception); | 
 |   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); | 
 |   if (status == MagickFalse) | 
 |     { | 
 |       image=DestroyImageList(image); | 
 |       return((Image *) NULL); | 
 |     } | 
 |   image->depth=1; | 
 |   image->endian=MSBEndian; | 
 |   (void) ReadBlobLSBShort(image); | 
 |   image->columns=(size_t) ReadBlobLSBShort(image); | 
 |   (void) ReadBlobLSBShort(image); | 
 |   image->rows=(size_t) ReadBlobLSBShort(image); | 
 |   /* | 
 |     Initialize image colormap. | 
 |   */ | 
 |   if (AcquireImageColormap(image,2,exception) == MagickFalse) | 
 |     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); | 
 |   if (image_info->ping != MagickFalse) | 
 |     { | 
 |       (void) CloseBlob(image); | 
 |       return(GetFirstImageInList(image)); | 
 |     } | 
 |   /* | 
 |     Convert bi-level image to pixel packets. | 
 |   */ | 
 |   quantum_type=IndexQuantum; | 
 |   quantum_info=AcquireQuantumInfo(image_info,image); | 
 |   if (quantum_info == (QuantumInfo *) NULL) | 
 |     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); | 
 |   pixels=GetQuantumPixels(quantum_info); | 
 |   length=GetQuantumExtent(image,quantum_info,quantum_type); | 
 |   for (y=0; y < (ssize_t) image->rows; y++) | 
 |   { | 
 |     register Quantum | 
 |       *restrict q; | 
 |  | 
 |     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); | 
 |     if (q == (Quantum *) NULL) | 
 |       break; | 
 |     count=ReadBlob(image,length,pixels); | 
 |     if (count != (ssize_t) length) | 
 |       ThrowReaderException(CorruptImageError,"UnableToReadImageData"); | 
 |     (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, | 
 |       quantum_type,pixels,exception); | 
 |     count=ReadBlob(image,(size_t) (-(ssize_t) length) & 0x01,pixels); | 
 |     if (SyncAuthenticPixels(image,exception) == MagickFalse) | 
 |       break; | 
 |     if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse) | 
 |       break; | 
 |   } | 
 |   SetQuantumImageType(image,quantum_type); | 
 |   quantum_info=DestroyQuantumInfo(quantum_info); | 
 |   if (EOFBlob(image) != MagickFalse) | 
 |     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", | 
 |       image->filename); | 
 |   (void) CloseBlob(image); | 
 |   return(GetFirstImageInList(image)); | 
 | } | 
 |  | 
 | /* | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %   R e g i s t e r A R T I m a g e                                           % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | % | 
 | %  RegisterARTImage() adds attributes for the ART image format to | 
 | %  the list of supported formats.  The attributes include the image format | 
 | %  tag, a method to read and/or write the format, whether the format | 
 | %  supports the saving of more than one frame to the same file or blob, | 
 | %  whether the format supports native in-memory I/O, and a brief | 
 | %  description of the format. | 
 | % | 
 | %  The format of the RegisterARTImage method is: | 
 | % | 
 | %      size_t RegisterARTImage(void) | 
 | % | 
 | */ | 
 | ModuleExport size_t RegisterARTImage(void) | 
 | { | 
 |   MagickInfo | 
 |     *entry; | 
 |  | 
 |   entry=SetMagickInfo("ART"); | 
 |   entry->decoder=(DecodeImageHandler *) ReadARTImage; | 
 |   entry->encoder=(EncodeImageHandler *) WriteARTImage; | 
 |   entry->raw=MagickTrue; | 
 |   entry->adjoin=MagickFalse; | 
 |   entry->description=ConstantString("PFS: 1st Publisher Clip Art"); | 
 |   entry->module=ConstantString("ART"); | 
 |   (void) RegisterMagickInfo(entry); | 
 |   return(MagickImageCoderSignature); | 
 | } | 
 |  | 
 | /* | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %   U n r e g i s t e r A R T I m a g e                                       % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | % | 
 | %  UnregisterARTImage() removes format registrations made by the | 
 | %  ART module from the list of supported formats. | 
 | % | 
 | %  The format of the UnregisterARTImage method is: | 
 | % | 
 | %      UnregisterARTImage(void) | 
 | % | 
 | */ | 
 | ModuleExport void UnregisterARTImage(void) | 
 | { | 
 |   (void) UnregisterMagickInfo("ART"); | 
 | } | 
 |  | 
 | /* | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %   W r i t e A R T I m a g e                                                 % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %                                                                             % | 
 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
 | % | 
 | %  WriteARTImage() writes an image of raw bits in LSB order to a file. | 
 | % | 
 | %  The format of the WriteARTImage method is: | 
 | % | 
 | %      MagickBooleanType WriteARTImage(const ImageInfo *image_info, | 
 | %        Image *image,ExceptionInfo *exception) | 
 | % | 
 | %  A description of each parameter follows. | 
 | % | 
 | %    o image_info: the image info. | 
 | % | 
 | %    o image:  The image. | 
 | % | 
 | %    o exception: return any errors or warnings in this structure. | 
 | % | 
 | */ | 
 | static MagickBooleanType WriteARTImage(const ImageInfo *image_info,Image *image, | 
 |   ExceptionInfo *exception) | 
 | { | 
 |   MagickBooleanType | 
 |     status; | 
 |  | 
 |   QuantumInfo | 
 |     *quantum_info; | 
 |  | 
 |   register const Quantum | 
 |     *p; | 
 |  | 
 |   size_t | 
 |     length; | 
 |  | 
 |   ssize_t | 
 |     count, | 
 |     y; | 
 |  | 
 |   unsigned char | 
 |     *pixels; | 
 |  | 
 |   /* | 
 |     Open output image file. | 
 |   */ | 
 |   assert(image_info != (const ImageInfo *) NULL); | 
 |   assert(image_info->signature == MagickSignature); | 
 |   assert(image != (Image *) NULL); | 
 |   assert(image->signature == MagickSignature); | 
 |   if (image->debug != MagickFalse) | 
 |     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); | 
 |   assert(exception != (ExceptionInfo *) NULL); | 
 |   assert(exception->signature == MagickSignature); | 
 |   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); | 
 |   if (status == MagickFalse) | 
 |     return(status); | 
 |   if ((image->columns > 65535UL) || (image->rows > 65535UL)) | 
 |     ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); | 
 |   image->endian=MSBEndian; | 
 |   image->depth=1; | 
 |   (void) WriteBlobLSBShort(image,0); | 
 |   (void) WriteBlobLSBShort(image,(unsigned short) image->columns); | 
 |   (void) WriteBlobLSBShort(image,0); | 
 |   (void) WriteBlobLSBShort(image,(unsigned short) image->rows); | 
 |   if (IsRGBColorspace(image->colorspace) == MagickFalse) | 
 |     (void) TransformImageColorspace(image,RGBColorspace); | 
 |   length=(image->columns+7)/8; | 
 |   pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels)); | 
 |   if (pixels == (unsigned char *) NULL) | 
 |     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); | 
 |   /* | 
 |     Convert image to a bi-level image. | 
 |   */ | 
 |   (void) SetImageType(image,BilevelType,exception); | 
 |   quantum_info=AcquireQuantumInfo(image_info,image); | 
 |   for (y=0; y < (ssize_t) image->rows; y++) | 
 |   { | 
 |     p=GetVirtualPixels(image,0,y,image->columns,1,exception); | 
 |     if (p == (const Quantum *) NULL) | 
 |       break; | 
 |     (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, | 
 |       GrayQuantum,pixels,exception); | 
 |     count=WriteBlob(image,length,pixels); | 
 |     if (count != (ssize_t) length) | 
 |       ThrowWriterException(CorruptImageError,"UnableToWriteImageData"); | 
 |     count=WriteBlob(image,(size_t) (-(ssize_t) length) & 0x01,pixels); | 
 |     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, | 
 |       image->rows); | 
 |     if (status == MagickFalse) | 
 |       break; | 
 |   } | 
 |   quantum_info=DestroyQuantumInfo(quantum_info); | 
 |   pixels=(unsigned char *) RelinquishMagickMemory(pixels); | 
 |   (void) CloseBlob(image); | 
 |   return(MagickTrue); | 
 | } |