Commit 4fe210d9f for imagemagick.org
commit 4fe210d9f38c1198793f8d735e21f9c9ec4b08d4
Author: Cristy <urban-warrior@imagemagick.org>
Date: Fri Dec 4 16:33:41 2026 -0500
image resolution was ignored on write, so a 400 dpi image was labeled 200
diff --git a/coders/cals.c b/coders/cals.c
index 70c8d06ed..bb64feb45 100644
--- a/coders/cals.c
+++ b/coders/cals.c
@@ -69,6 +69,7 @@
#include "MagickCore/string_.h"
#include "MagickCore/string-private.h"
#include "MagickCore/module.h"
+#include "MagickCore/utility-private.h"
#if defined(MAGICKCORE_TIFF_DELEGATE)
/*
@@ -143,6 +144,40 @@ static MagickBooleanType IsCALS(const unsigned char *magick,const size_t length)
% o exception: return any errors or warnings in this structure.
%
*/
+
+static OrientationType GetCALSOrientation(const unsigned long pel_path,
+ const unsigned long direction)
+{
+ /*
+ Pel path direction, measured counterclockwise from the left-to-right horizontal.
+ */
+ switch (pel_path)
+ {
+ case 0:
+ return(direction == 90 ? BottomLeftOrientation : TopLeftOrientation);
+ case 90:
+ {
+ /*
+ Line progressions of 0 and 180 were written by earlier releases.
+ */
+ if ((direction == 90) || (direction == 180))
+ return(RightBottomOrientation);
+ return(LeftBottomOrientation);
+ }
+ case 180:
+ return(direction == 90 ? TopRightOrientation : BottomRightOrientation);
+ case 270:
+ {
+ if ((direction == 90) || (direction == 0))
+ return(LeftTopOrientation);
+ return(RightTopOrientation);
+ }
+ default:
+ break;
+ }
+ return(TopLeftOrientation);
+}
+
static Image *ReadCALSImage(const ImageInfo *image_info,
ExceptionInfo *exception)
{
@@ -161,15 +196,18 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
*read_info;
int
- c,
unique_file;
MagickBooleanType
status;
ssize_t
+ count,
i;
+ unsigned char
+ buffer[16384];
+
unsigned long
density,
direction,
@@ -202,7 +240,7 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
(void) memset(header,0,sizeof(header));
density=0;
direction=0;
- orientation=1;
+ orientation=(unsigned long) TopLeftOrientation;
pel_path=0;
type=1;
width=0;
@@ -210,7 +248,7 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
for (i=0; i < 16; i++)
{
if (ReadBlob(image,128,(unsigned char *) header) != 128)
- break;
+ ThrowReaderException(CorruptImageError,"ImproperImageHeader");
switch (*header)
{
case 'R':
@@ -228,17 +266,9 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
}
if (LocaleNCompare(header,"rorient:",8) == 0)
{
- (void) MagickSscanf(header+8,"%lu,%lu",&pel_path,&direction);
- if (pel_path == 90)
- orientation=5;
- else
- if (pel_path == 180)
- orientation=3;
- else
- if (pel_path == 270)
- orientation=7;
- if (direction == 90)
- orientation++;
+ if (MagickSscanf(header+8,"%lu,%lu",&pel_path,&direction) == 2)
+ orientation=(unsigned long) GetCALSOrientation(pel_path,
+ direction);
break;
}
if (LocaleNCompare(header,"rtype:",6) == 0)
@@ -250,8 +280,7 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
}
}
}
- if ((width == 0) || (height == 0) || (type != 1) ||
- (orientation == 0) || (density == 0))
+ if ((width == 0) || (height == 0) || (type != 1) || (density == 0))
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
/*
Read CALS pixels.
@@ -259,14 +288,35 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
file=(FILE *) NULL;
unique_file=AcquireUniqueFileResource(filename);
if (unique_file != -1)
- file=fdopen(unique_file,"rb+");
- if ((unique_file == -1) || (file == (FILE *) NULL))
- ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
- while ((c=ReadBlobByte(image)) != EOF)
- if (fputc(c,file) != c)
- break;
- if (fseek(file,0,SEEK_SET) != 0)
- ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
+ file=fdopen(unique_file,"wb+");
+ if (file == (FILE *) NULL)
+ {
+ if (unique_file != -1)
+ {
+ (void) close_utf8(unique_file);
+ (void) RelinquishUniqueFileResource(filename);
+ }
+ ThrowReaderException(FileOpenError,"UnableToCreateTemporaryFile");
+ }
+ status=MagickTrue;
+ count=ReadBlob(image,sizeof(buffer),buffer);
+ while (count > 0)
+ {
+ if (fwrite(buffer,1,(size_t) count,file) != (size_t) count)
+ {
+ status=MagickFalse;
+ break;
+ }
+ count=ReadBlob(image,sizeof(buffer),buffer);
+ }
+ if ((fflush(file) != 0) || (fseek(file,0,SEEK_SET) != 0))
+ status=MagickFalse;
+ if (status == MagickFalse)
+ {
+ (void) fclose(file);
+ (void) RelinquishUniqueFileResource(filename);
+ ThrowReaderException(FileOpenError,"UnableToWriteFile");
+ }
(void) CloseBlob(image);
image=DestroyImage(image);
read_info=CloneImageInfo(image_info);
@@ -281,8 +331,10 @@ static Image *ReadCALSImage(const ImageInfo *image_info,
read_info->orientation=(OrientationType) orientation;
image=ReadImageWithoutPostProcessing(read_info,exception);
read_info->file=(FILE *) NULL;
+ (void) fclose(file);
if (image != (Image *) NULL)
{
+ image->orientation=(OrientationType) orientation;
(void) CopyMagickString(image->filename,image_info->filename,
MagickPathExtent);
(void) CopyMagickString(image->magick_filename,image_info->filename,
@@ -401,38 +453,68 @@ ModuleExport void UnregisterCALSImage(void)
%
*/
-static ssize_t WriteCALSRecord(Image *image,const char *data)
+static size_t GetCALSDensity(const ImageInfo *image_info,const Image *image)
{
- const char
- *p;
+ double
+ density;
- ssize_t
- i;
+ /*
+ An explicit density wins, followed by the image resolution if its units are
+ known. The rdensty record holds four digits and may not be zero.
+ */
+ density=0.0;
+ if (image_info->density != (char *) NULL)
+ {
+ GeometryInfo
+ geometry_info;
- ssize_t
- count;
+ if ((ParseGeometry(image_info->density,&geometry_info) & RhoValue) != 0)
+ density=geometry_info.rho;
+ }
+ else
+ if (image->units == PixelsPerInchResolution)
+ density=image->resolution.x;
+ else
+ if (image->units == PixelsPerCentimeterResolution)
+ density=2.54*image->resolution.x;
+ if ((density >= 0.5) && (density < 9999.5))
+ return((size_t) floor(density+0.5));
+ return(200);
+}
+
+static MagickBooleanType WriteCALSRecord(Image *image,const char *data)
+{
+ char
+ record[128];
+
+ size_t
+ length;
- i=0;
- count=0;
+ /*
+ Records are exactly 128 bytes, padded with blanks.
+ */
+ (void) memset(record,' ',sizeof(record));
if (data != (const char *) NULL)
{
- p=data;
- for (i=0; (i < 128) && (p[i] != '\0'); i++);
- count=WriteBlob(image,(size_t) i,(const unsigned char *) data);
- }
- if (i < 128)
- {
- i=128-i;
- count=WriteBlobPadding(image,(size_t) i,' ');
+ length=strlen(data);
+ (void) memcpy(record,data,MagickMin(length,sizeof(record)));
}
- return(count);
+ if (WriteBlob(image,sizeof(record),(const unsigned char *) record) !=
+ (ssize_t) sizeof(record))
+ return(MagickFalse);
+ return(MagickTrue);
}
static MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
Image *image,ExceptionInfo *exception)
{
char
- header[129];
+ density[129],
+ orientation[129],
+ pixel_count[129];
+
+ const char
+ *records[16];
Image
*group4_image;
@@ -443,23 +525,19 @@ static MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
MagickBooleanType
status;
- ssize_t
- i;
-
size_t
- density,
length,
orient_x,
orient_y;
ssize_t
- count;
+ i;
unsigned char
*group4;
/*
- Open output image file.
+ Validate arguments.
*/
assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickCoreSignature);
@@ -469,114 +547,116 @@ static MagickBooleanType WriteCALSImage(const ImageInfo *image_info,
assert(exception->signature == MagickCoreSignature);
if (IsEventLogging() != MagickFalse)
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+ if ((image->columns > 999999UL) || (image->rows > 999999UL))
+ {
+ (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
+ "WidthOrHeightExceedsLimit","`%s'",image->filename);
+ return(MagickFalse);
+ }
+ /*
+ Compress pixels first, so a failure does not leave a partial output file.
+ */
+ group4_image=CloneImage(image,0,0,MagickTrue,exception);
+ if (group4_image == (Image *) NULL)
+ return(MagickFalse);
+ write_info=CloneImageInfo(image_info);
+ (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent);
+ (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent);
+ length=0;
+ group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
+ exception);
+ write_info=DestroyImageInfo(write_info);
+ group4_image=DestroyImage(group4_image);
+ if (group4 == (unsigned char *) NULL)
+ return(MagickFalse);
+ /*
+ Open output image file.
+ */
status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
if (status == MagickFalse)
- return(status);
+ {
+ group4=(unsigned char *) RelinquishMagickMemory(group4);
+ return(status);
+ }
/*
Create standard CALS header.
*/
- count=WriteCALSRecord(image,"srcdocid: NONE");
- (void) count;
- count=WriteCALSRecord(image,"dstdocid: NONE");
- count=WriteCALSRecord(image,"txtfilid: NONE");
- count=WriteCALSRecord(image,"figid: NONE");
- count=WriteCALSRecord(image,"srcgph: NONE");
- count=WriteCALSRecord(image,"doccls: NONE");
- count=WriteCALSRecord(image,"rtype: 1");
- orient_x=0;
- orient_y=0;
switch (image->orientation)
{
case TopRightOrientation:
{
orient_x=180;
- orient_y=270;
+ orient_y=90;
break;
}
case BottomRightOrientation:
{
orient_x=180;
- orient_y=90;
+ orient_y=270;
break;
}
case BottomLeftOrientation:
{
+ orient_x=0;
orient_y=90;
break;
}
case LeftTopOrientation:
{
orient_x=270;
+ orient_y=90;
break;
}
case RightTopOrientation:
{
orient_x=270;
- orient_y=180;
+ orient_y=270;
break;
}
case RightBottomOrientation:
{
orient_x=90;
- orient_y=180;
+ orient_y=90;
break;
}
case LeftBottomOrientation:
{
orient_x=90;
+ orient_y=270;
break;
}
default:
{
+ orient_x=0;
orient_y=270;
break;
}
}
- (void) FormatLocaleString(header,sizeof(header),"rorient: %03ld,%03ld",
- (long) orient_x,(long) orient_y);
- count=WriteCALSRecord(image,header);
- (void) FormatLocaleString(header,sizeof(header),"rpelcnt: %06lu,%06lu",
- (unsigned long) image->columns,(unsigned long) image->rows);
- count=WriteCALSRecord(image,header);
- density=200;
- if (image_info->density != (char *) NULL)
- {
- GeometryInfo
- geometry_info;
-
- (void) ParseGeometry(image_info->density,&geometry_info);
- density=(size_t) floor(geometry_info.rho+0.5);
- }
- (void) FormatLocaleString(header,sizeof(header),"rdensty: %04lu",
- (unsigned long) density);
- count=WriteCALSRecord(image,header);
- count=WriteCALSRecord(image,"notes: NONE");
- (void) memset(header,' ',128);
- for (i=0; i < 5; i++)
- (void) WriteBlob(image,128,(unsigned char *) header);
+ (void) FormatLocaleString(orientation,sizeof(orientation),
+ "rorient: %03lu,%03lu",(unsigned long) orient_x,(unsigned long) orient_y);
+ (void) FormatLocaleString(pixel_count,sizeof(pixel_count),
+ "rpelcnt: %06lu,%06lu",(unsigned long) image->columns,(unsigned long)
+ image->rows);
+ (void) FormatLocaleString(density,sizeof(density),"rdensty: %04lu",
+ (unsigned long) GetCALSDensity(image_info,image));
+ (void) memset(records,0,sizeof(records));
+ records[0]="srcdocid: NONE";
+ records[1]="dstdocid: NONE";
+ records[2]="txtfilid: NONE";
+ records[3]="figid: NONE";
+ records[4]="srcgph: NONE";
+ records[5]="doccls: NONE";
+ records[6]="rtype: 1";
+ records[7]=orientation;
+ records[8]=pixel_count;
+ records[9]=density;
+ records[10]="notes: NONE";
+ for (i=0; i < 16; i++)
+ if (WriteCALSRecord(image,records[i]) == MagickFalse)
+ status=MagickFalse;
/*
Write CALS pixels.
*/
- write_info=CloneImageInfo(image_info);
- (void) CopyMagickString(write_info->filename,"GROUP4:",MagickPathExtent);
- (void) CopyMagickString(write_info->magick,"GROUP4",MagickPathExtent);
- group4_image=CloneImage(image,0,0,MagickTrue,exception);
- if (group4_image == (Image *) NULL)
- {
- write_info=DestroyImageInfo(write_info);
- (void) CloseBlob(image);
- return(MagickFalse);
- }
- group4=(unsigned char *) ImageToBlob(write_info,group4_image,&length,
- exception);
- group4_image=DestroyImage(group4_image);
- if (group4 == (unsigned char *) NULL)
- {
- write_info=DestroyImageInfo(write_info);
- (void) CloseBlob(image);
- return(MagickFalse);
- }
- write_info=DestroyImageInfo(write_info);
if (WriteBlob(image,length,group4) != (ssize_t) length)
status=MagickFalse;
group4=(unsigned char *) RelinquishMagickMemory(group4);