]> granicus.if.org Git - imagemagick/blobdiff - coders/xbm.c
...
[imagemagick] / coders / xbm.c
index fc37238e043288ff0c02207c10d427ec1190cb54..ab887c4c523b2019204232ae450ac4ab8a609b88 100644 (file)
 %                  Read/Write X Windows System Bitmap Format                  %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2017 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  %
@@ -131,41 +131,48 @@ static MagickBooleanType IsXBM(const unsigned char *magick,const size_t length)
 %
 */
 
-static int XBMInteger(Image *image,short int *hex_digits)
-{
+static unsigned int XBMInteger(Image *image,short int *hex_digits)
+{ 
   int
-    c,
-    flag,
+    c;
+  
+  unsigned int
     value;
-
-  value=0;
-  flag=0;
-  for ( ; ; )
-  {
+  
+  /*
+    Skip any leading whitespace.
+  */
+  do
+  { 
     c=ReadBlobByte(image);
     if (c == EOF)
-      {
-        value=(-1);
-        break;
-      }
+      return(0);
+  } while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
+  /*
+    Evaluate number.
+  */
+  value=0;
+  do
+  { 
+    if (value > (unsigned int) (INT_MAX/10))
+      break;
+    value*=16;
     c&=0xff;
-    if (isxdigit(c) != MagickFalse)
-      {
-        value=(int) ((size_t) value << 4)+hex_digits[c];
-        flag++;
-        continue;
-      }
-    if ((hex_digits[c]) < 0 && (flag != 0))
+    if (value > (unsigned int) (INT_MAX-hex_digits[c]))
       break;
-  }
+    value+=hex_digits[c];
+    c=ReadBlobByte(image);
+    if (c == EOF)
+      return(0);
+  } while (hex_digits[c] >= 0);
   return(value);
 }
 
 static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
   char
-    buffer[MaxTextExtent],
-    name[MaxTextExtent];
+    buffer[MagickPathExtent],
+    name[MagickPathExtent];
 
   Image
     *image;
@@ -186,35 +193,33 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   short int
     hex_digits[256];
 
-  size_t
-    bit,
-    byte,
-    bytes_per_line,
-    length,
-    padding,
-    value,
-    version;
-
   ssize_t
     y;
 
   unsigned char
     *data;
 
-  unsigned long
+  unsigned int
+    bit,
+    byte,
+    bytes_per_line,
     height,
+    length,
+    padding,
+    value,
+    version,
     width;
 
   /*
     Open image file.
   */
   assert(image_info != (const ImageInfo *) NULL);
-  assert(image_info->signature == MagickSignature);
+  assert(image_info->signature == MagickCoreSignature);
   if (image_info->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
       image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  assert(exception->signature == MagickSignature);
+  assert(exception->signature == MagickCoreSignature);
   image=AcquireImage(image_info,exception);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
@@ -228,12 +233,12 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   width=0;
   height=0;
   while (ReadBlobString(image,buffer) != (char *) NULL)
-    if (sscanf(buffer,"#define %s %lu",name,&width) == 2)
+    if (sscanf(buffer,"#define %32s %u",name,&width) == 2)
       if ((strlen(name) >= 6) &&
           (LocaleCompare(name+strlen(name)-6,"_width") == 0))
         break;
   while (ReadBlobString(image,buffer) != (char *) NULL)
-    if (sscanf(buffer,"#define %s %lu",name,&height) == 2)
+    if (sscanf(buffer,"#define %32s %u",name,&height) == 2)
       if ((strlen(name) >= 7) &&
           (LocaleCompare(name+strlen(name)-7,"_height") == 0))
         break;
@@ -248,13 +253,13 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   version=11;
   while (ReadBlobString(image,buffer) != (char *) NULL)
   {
-    if (sscanf(buffer,"static short %s = {",name) == 1)
+    if (sscanf(buffer,"static short %32s = {",name) == 1)
       version=10;
     else
-      if (sscanf(buffer,"static unsigned char %s = {",name) == 1)
+      if (sscanf(buffer,"static unsigned char %32s = {",name) == 1)
         version=11;
       else
-        if (sscanf(buffer,"static char %s = {",name) == 1)
+        if (sscanf(buffer,"static char %32s = {",name) == 1)
           version=11;
         else
           continue;
@@ -288,6 +293,9 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
       (void) CloseBlob(image);
       return(GetFirstImageInList(image));
     }
+  status=SetImageExtent(image,image->columns,image->rows,exception);
+  if (status == MagickFalse)
+    return(DestroyImageList(image));
   /*
     Initialize hex values.
   */
@@ -326,8 +334,8 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   if (((image->columns % 16) != 0) && ((image->columns % 16) < 9) &&
       (version == 10))
     padding=1;
-  bytes_per_line=(image->columns+7)/8+padding;
-  length=(size_t) image->rows;
+  bytes_per_line=(unsigned int) (image->columns+7)/8+padding;
+  length=(unsigned int) image->rows;
   data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line*
     sizeof(*data));
   if (data == (unsigned char *) NULL)
@@ -336,7 +344,7 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   if (version == 10)
     for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2))
     {
-      value=(size_t) XBMInteger(image,hex_digits);
+      value=XBMInteger(image,hex_digits);
       *p++=(unsigned char) value;
       if ((padding == 0) || (((i+2) % bytes_per_line) != 0))
         *p++=(unsigned char) (value >> 8);
@@ -344,7 +352,7 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
   else
     for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++)
     {
-      value=(size_t) XBMInteger(image,hex_digits);
+      value=XBMInteger(image,hex_digits);
       *p++=(unsigned char) value;
     }
   /*
@@ -410,14 +418,12 @@ ModuleExport size_t RegisterXBMImage(void)
   MagickInfo
     *entry;
 
-  entry=SetMagickInfo("XBM");
+  entry=AcquireMagickInfo("XBM","XBM",
+    "X Windows system bitmap (black and white)");
   entry->decoder=(DecodeImageHandler *) ReadXBMImage;
   entry->encoder=(EncodeImageHandler *) WriteXBMImage;
   entry->magick=(IsImageFormatHandler *) IsXBM;
-  entry->adjoin=MagickFalse;
-  entry->description=ConstantString(
-    "X Windows system bitmap (black and white)");
-  entry->module=ConstantString("XBM");
+  entry->flags^=CoderAdjoinFlag;
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }
@@ -477,8 +483,8 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
   ExceptionInfo *exception)
 {
   char
-    basename[MaxTextExtent],
-    buffer[MaxTextExtent];
+    basename[MagickPathExtent],
+    buffer[MagickPathExtent];
 
   MagickBooleanType
     status;
@@ -501,34 +507,31 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
     Open output image file.
   */
   assert(image_info != (const ImageInfo *) NULL);
-  assert(image_info->signature == MagickSignature);
+  assert(image_info->signature == MagickCoreSignature);
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(exception != (ExceptionInfo *) NULL);
-  assert(exception->signature == MagickSignature);
+  assert(exception->signature == MagickCoreSignature);
   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
   if (status == MagickFalse)
     return(status);
-  if ((IssRGBColorspace(image->colorspace) == MagickFalse) &&
-      (IsRGBColorspace(image->colorspace) == MagickFalse) &&
-      (IsImageGray(image,exception) == MagickFalse))
-    (void) TransformImageColorspace(image,sRGBColorspace,exception);
+  (void) TransformImageColorspace(image,sRGBColorspace,exception);
   /*
     Write X bitmap header.
   */
   GetPathComponent(image->filename,BasePath,basename);
-  (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_width %.20g\n",
+  (void) FormatLocaleString(buffer,MagickPathExtent,"#define %s_width %.20g\n",
     basename,(double) image->columns);
   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
-  (void) FormatLocaleString(buffer,MaxTextExtent,"#define %s_height %.20g\n",
+  (void) FormatLocaleString(buffer,MagickPathExtent,"#define %s_height %.20g\n",
     basename,(double) image->rows);
   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
-  (void) FormatLocaleString(buffer,MaxTextExtent,
+  (void) FormatLocaleString(buffer,MagickPathExtent,
     "static char %s_bits[] = {\n",basename);
   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
-  (void) CopyMagickString(buffer," ",MaxTextExtent);
+  (void) CopyMagickString(buffer," ",MagickPathExtent);
   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
   /*
     Convert MIFF to X bitmap pixels.
@@ -539,7 +542,7 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
   count=0;
   x=0;
   y=0;
-  (void) CopyMagickString(buffer," ",MaxTextExtent);
+  (void) CopyMagickString(buffer," ",MagickPathExtent);
   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -549,7 +552,7 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
     for (x=0; x < (ssize_t) image->columns; x++)
     {
       byte>>=1;
-      if (GetPixelIntensity(image,p) < (QuantumRange/2))
+      if (GetPixelLuma(image,p) < (QuantumRange/2))
         byte|=0x80;
       bit++;
       if (bit == 8)
@@ -557,13 +560,13 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
           /*
             Write a bitmap byte to the image file.
           */
-          (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ",
+          (void) FormatLocaleString(buffer,MagickPathExtent,"0x%02X, ",
             (unsigned int) (byte & 0xff));
           (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
           count++;
           if (count == 12)
             {
-              (void) CopyMagickString(buffer,"\n  ",MaxTextExtent);
+              (void) CopyMagickString(buffer,"\n  ",MagickPathExtent);
               (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
               count=0;
             };
@@ -578,13 +581,13 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
           Write a bitmap byte to the image file.
         */
         byte>>=(8-bit);
-        (void) FormatLocaleString(buffer,MaxTextExtent,"0x%02X, ",
+        (void) FormatLocaleString(buffer,MagickPathExtent,"0x%02X, ",
           (unsigned int) (byte & 0xff));
         (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
         count++;
         if (count == 12)
           {
-            (void) CopyMagickString(buffer,"\n  ",MaxTextExtent);
+            (void) CopyMagickString(buffer,"\n  ",MagickPathExtent);
             (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
             count=0;
           };
@@ -596,7 +599,7 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
     if (status == MagickFalse)
       break;
   }
-  (void) CopyMagickString(buffer,"};\n",MaxTextExtent);
+  (void) CopyMagickString(buffer,"};\n",MagickPathExtent);
   (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
   (void) CloseBlob(image);
   return(MagickTrue);