% March 2008 %
% %
% %
-% Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
+% Copyright 1999-2012 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 %
/*
Include declarations.
*/
-#include "magick/studio.h"
-#include "magick/blob.h"
-#include "magick/blob-private.h"
-#include "magick/cache.h"
-#include "magick/colorspace.h"
-#include "magick/exception.h"
-#include "magick/exception-private.h"
-#include "magick/image.h"
-#include "magick/image-private.h"
-#include "magick/list.h"
-#include "magick/log.h"
-#include "magick/magick.h"
-#include "magick/memory_.h"
-#include "magick/monitor.h"
-#include "magick/monitor-private.h"
-#include "magick/profile.h"
-#include "magick/quantum-private.h"
-#include "magick/static.h"
-#include "magick/string_.h"
-#include "magick/module.h"
-#include "magick/transform.h"
-#include "magick/studio.h"
-#include "magick/blob.h"
-#include "magick/blob-private.h"
-#include "magick/colorspace.h"
-#include "magick/exception.h"
-#include "magick/exception-private.h"
-#include "magick/compress.h"
-#include "magick/image.h"
-#include "magick/image-private.h"
-#include "magick/list.h"
-#include "magick/magick.h"
-#include "magick/memory_.h"
-#include "magick/monitor.h"
-#include "magick/monitor-private.h"
-#include "magick/quantum.h"
-#include "magick/static.h"
-#include "magick/string_.h"
+#include "MagickCore/studio.h"
+#include "MagickCore/blob.h"
+#include "MagickCore/blob-private.h"
+#include "MagickCore/cache.h"
+#include "MagickCore/colorspace.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/log.h"
+#include "MagickCore/magick.h"
+#include "MagickCore/memory_.h"
+#include "MagickCore/monitor.h"
+#include "MagickCore/monitor-private.h"
+#include "MagickCore/profile.h"
+#include "MagickCore/quantum-private.h"
+#include "MagickCore/static.h"
+#include "MagickCore/string_.h"
+#include "MagickCore/module.h"
+#include "MagickCore/transform.h"
+#include "MagickCore/studio.h"
+#include "MagickCore/blob.h"
+#include "MagickCore/blob-private.h"
+#include "MagickCore/colorspace.h"
+#include "MagickCore/exception.h"
+#include "MagickCore/exception-private.h"
+#include "MagickCore/compress.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/pixel-accessor.h"
+#include "MagickCore/quantum.h"
+#include "MagickCore/static.h"
+#include "MagickCore/string_.h"
\f
/*
Definitions
*/
typedef struct _DDSPixelFormat
{
- unsigned long
+ size_t
flags,
fourcc,
rgb_bitcount,
typedef struct _DDSInfo
{
- unsigned long
- flags,
- height,
- width,
- pitchOrLinearSize,
- depth,
- mipmapcount,
+ size_t
+ flags,
+ height,
+ width,
+ pitchOrLinearSize,
+ depth,
+ mipmapcount,
ddscaps1,
ddscaps2;
} DDSColors;
typedef MagickBooleanType
- DDSDecoder(Image *,DDSInfo *);
+ DDSDecoder(Image *,DDSInfo *,ExceptionInfo *);
/*
Macros
DDSColors *c, MagickBooleanType ignoreAlpha);
static MagickBooleanType
- ReadDXT1(Image *image, DDSInfo *dds_info);
+ ReadDXT1(Image *, DDSInfo *,ExceptionInfo *);
static MagickBooleanType
- ReadDXT3(Image *image, DDSInfo *dds_info);
+ ReadDXT3(Image *image, DDSInfo *dds_info,ExceptionInfo *);
static MagickBooleanType
- ReadDXT5(Image *image, DDSInfo *dds_info);
+ ReadDXT5(Image *image, DDSInfo *dds_info,ExceptionInfo *);
static MagickBooleanType
- ReadUncompressedRGB(Image *image, DDSInfo *dds_info);
+ ReadUncompressedRGB(Image *image, DDSInfo *dds_info,ExceptionInfo *);
static MagickBooleanType
- ReadUncompressedRGBA(Image *image, DDSInfo *dds_info);
+ ReadUncompressedRGBA(Image *image, DDSInfo *dds_info,ExceptionInfo *);
static void
SkipDXTMipmaps(Image *image, DDSInfo *dds_info, int texel_size);
%
*/
-static inline unsigned long Min(unsigned long one, unsigned long two)
+static inline size_t Min(size_t one, size_t two)
{
if (one < two)
return one;
compression;
DDSInfo
- dds_info;
+ dds_info;
DDSDecoder
*decoder;
- unsigned long
+ size_t
n, num_images;
/*
image_info->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
- image=AcquireImage(image_info);
+ image=AcquireImage(image_info,exception);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
{
Initialize image structure.
*/
if (ReadDDSInfo(image, &dds_info) != MagickTrue) {
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ ThrowReaderException(CorruptImageError,"ImproperImageHeader");
}
if (dds_info.ddscaps2 & DDSCAPS2_CUBEMAP)
if (n != 0)
{
/* Start a new image */
- AcquireNextImage(image_info,image);
+ AcquireNextImage(image_info,image,exception);
if (GetNextImageInList(image) == (Image *) NULL)
{
image = DestroyImageList(image);
return(GetFirstImageInList(image));
}
- if ((decoder)(image, &dds_info) != MagickTrue)
+ if ((decoder)(image, &dds_info, exception) != MagickTrue)
{
(void) CloseBlob(image);
return(GetFirstImageInList(image));
static MagickBooleanType ReadDDSInfo(Image *image, DDSInfo *dds_info)
{
- unsigned long
+ size_t
hdr_size,
required;
-
- /* Seek to start of header */
- (void) SeekBlob(image, 4, SEEK_SET);
-
- /* Check header field */
- hdr_size = ReadBlobLSBLong(image);
- if (hdr_size != 124)
- return MagickFalse;
-
- /* Fill in DDS info struct */
- dds_info->flags = ReadBlobLSBLong(image);
-
- /* Check required flags */
- required=(unsigned long) (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT);
+
+ /* Seek to start of header */
+ (void) SeekBlob(image, 4, SEEK_SET);
+
+ /* Check header field */
+ hdr_size = ReadBlobLSBLong(image);
+ if (hdr_size != 124)
+ return MagickFalse;
+
+ /* Fill in DDS info struct */
+ dds_info->flags = ReadBlobLSBLong(image);
+
+ /* Check required flags */
+ required=(size_t) (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT);
if ((dds_info->flags & required) != required)
- return MagickFalse;
+ return MagickFalse;
- dds_info->height = ReadBlobLSBLong(image);
- dds_info->width = ReadBlobLSBLong(image);
- dds_info->pitchOrLinearSize = ReadBlobLSBLong(image);
- dds_info->depth = ReadBlobLSBLong(image);
- dds_info->mipmapcount = ReadBlobLSBLong(image);
+ dds_info->height = ReadBlobLSBLong(image);
+ dds_info->width = ReadBlobLSBLong(image);
+ dds_info->pitchOrLinearSize = ReadBlobLSBLong(image);
+ dds_info->depth = ReadBlobLSBLong(image);
+ dds_info->mipmapcount = ReadBlobLSBLong(image);
- (void) SeekBlob(image, 44, SEEK_CUR); /* reserved region of 11 DWORDs */
+ (void) SeekBlob(image, 44, SEEK_CUR); /* reserved region of 11 DWORDs */
/* Read pixel format structure */
hdr_size = ReadBlobLSBLong(image);
dds_info->pixelformat.b_bitmask = ReadBlobLSBLong(image);
dds_info->pixelformat.alpha_bitmask = ReadBlobLSBLong(image);
- dds_info->ddscaps1 = ReadBlobLSBLong(image);
- dds_info->ddscaps2 = ReadBlobLSBLong(image);
- (void) SeekBlob(image, 12, SEEK_CUR); /* 3 reserved DWORDs */
+ dds_info->ddscaps1 = ReadBlobLSBLong(image);
+ dds_info->ddscaps2 = ReadBlobLSBLong(image);
+ (void) SeekBlob(image, 12, SEEK_CUR); /* 3 reserved DWORDs */
- return MagickTrue;
+ return MagickTrue;
}
static void CalculateColors(unsigned short c0, unsigned short c1,
}
}
-static MagickBooleanType ReadDXT1(Image *image, DDSInfo *dds_info)
+static MagickBooleanType ReadDXT1(Image *image, DDSInfo *dds_info,
+ ExceptionInfo *exception)
{
DDSColors
colors;
- ExceptionInfo
- *exception;
-
- long
- j,
- y;
-
- PixelPacket
+ register Quantum
*q;
- register long
+ register ssize_t
i,
x;
+ size_t
+ bits;
+
+ ssize_t
+ j,
+ y;
+
unsigned char
code;
- unsigned short
+ unsigned short
c0,
c1;
-
- unsigned long
- bits;
-
- exception=(&image->exception);
- for (y = 0; y < (long) dds_info->height; y += 4)
+
+ for (y = 0; y < (ssize_t) dds_info->height; y += 4)
{
- for (x = 0; x < (long) dds_info->width; x += 4)
+ for (x = 0; x < (ssize_t) dds_info->width; x += 4)
{
/* Get 4x4 patch of pixels to write on */
q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
- Min(4, dds_info->height - y),exception);
+ Min(4, dds_info->height - y),exception);
- if (q == (PixelPacket *) NULL)
+ if (q == (Quantum *) NULL)
return MagickFalse;
/* Read 8 bytes of data from the image */
{
for (i = 0; i < 4; i++)
{
- if ((x + i) < (long) dds_info->width && (y + j) < (long) dds_info->height)
+ if ((x + i) < (ssize_t) dds_info->width &&
+ (y + j) < (ssize_t) dds_info->height)
{
code = (unsigned char) ((bits >> ((j*4+i)*2)) & 0x3);
- q->red = ScaleCharToQuantum( colors.r[code] );
- q->green = ScaleCharToQuantum( colors.g[code] );
- q->blue = ScaleCharToQuantum( colors.b[code] );
- q->opacity = ScaleCharToQuantum( colors.a[code] );
- if (colors.a[code] && image->matte == MagickFalse)
- /* Correct matte */
- image->matte = MagickTrue;
- q++;
+ SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
+ SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
+ SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
+ SetPixelAlpha(image,ScaleCharToQuantum(colors.a[code]),q);
+ if (colors.a[code] && (image->matte == MagickFalse))
+ image->matte=MagickTrue; /* Correct matte */
+ q+=GetPixelChannels(image);
}
}
}
return MagickTrue;
}
-static MagickBooleanType ReadDXT3(Image *image, DDSInfo *dds_info)
+static MagickBooleanType ReadDXT3(Image *image, DDSInfo *dds_info,
+ ExceptionInfo *exception)
{
DDSColors
colors;
- ExceptionInfo
- *exception;
-
- long
- j,
- y;
-
- PixelPacket
+ register Quantum
*q;
- register long
+ register ssize_t
i,
x;
unsigned char
alpha;
- unsigned long
+ size_t
a0,
a1,
bits,
code;
+ ssize_t
+ j,
+ y;
+
unsigned short
c0,
c1;
- exception=(&image->exception);
- for (y = 0; y < (long) dds_info->height; y += 4)
+ for (y = 0; y < (ssize_t) dds_info->height; y += 4)
{
- for (x = 0; x < (long) dds_info->width; x += 4)
+ for (x = 0; x < (ssize_t) dds_info->width; x += 4)
{
/* Get 4x4 patch of pixels to write on */
q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
Min(4, dds_info->height - y),exception);
- if (q == (PixelPacket *) NULL)
+ if (q == (Quantum *) NULL)
return MagickFalse;
/* Read alpha values (8 bytes) */
{
for (i = 0; i < 4; i++)
{
- if ((x + i) < (long) dds_info->width && (y + j) < (long) dds_info->height)
+ if ((x + i) < (ssize_t) dds_info->width && (y + j) < (ssize_t) dds_info->height)
{
code = (bits >> ((4*j+i)*2)) & 0x3;
- q->red = ScaleCharToQuantum( colors.r[code] );
- q->green = ScaleCharToQuantum( colors.g[code] );
- q->blue = ScaleCharToQuantum( colors.b[code] );
-
+ SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
+ SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
+ SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
/*
Extract alpha value: multiply 0..15 by 17 to get range 0..255
*/
alpha = 17U * (unsigned char) ((a0 >> (4*(4*j+i))) & 0xf);
else
alpha = 17U * (unsigned char) ((a1 >> (4*(4*(j-2)+i))) & 0xf);
-
- q->opacity = ScaleCharToQuantum( (unsigned char) (255 - alpha) );
-
- q++;
+ SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
+ q+=GetPixelChannels(image);
}
}
}
return MagickTrue;
}
-static MagickBooleanType ReadDXT5(Image *image, DDSInfo *dds_info)
+static MagickBooleanType ReadDXT5(Image *image, DDSInfo *dds_info,
+ ExceptionInfo *exception)
{
DDSColors
colors;
- ExceptionInfo
- *exception;
-
- long
- j,
- y;
-
MagickSizeType
alpha_bits;
- PixelPacket
+ register Quantum
*q;
- register long
+ register ssize_t
i,
x;
a0,
a1;
- unsigned long
+ size_t
alpha,
bits,
code,
alpha_code;
+ ssize_t
+ j,
+ y;
+
unsigned short
c0,
c1;
- exception=(&image->exception);
- for (y = 0; y < (long) dds_info->height; y += 4)
+ for (y = 0; y < (ssize_t) dds_info->height; y += 4)
{
- for (x = 0; x < (long) dds_info->width; x += 4)
+ for (x = 0; x < (ssize_t) dds_info->width; x += 4)
{
/* Get 4x4 patch of pixels to write on */
q = QueueAuthenticPixels(image, x, y, Min(4, dds_info->width - x),
Min(4, dds_info->height - y),exception);
- if (q == (PixelPacket *) NULL)
+ if (q == (Quantum *) NULL)
return MagickFalse;
/* Read alpha values (8 bytes) */
{
for (i = 0; i < 4; i++)
{
- if ((x + i) < (long) dds_info->width && (y + j) < (long) dds_info->height)
+ if ((x + i) < (ssize_t) dds_info->width &&
+ (y + j) < (ssize_t) dds_info->height)
{
code = (bits >> ((4*j+i)*2)) & 0x3;
- q->red = ScaleCharToQuantum( colors.r[code] );
- q->green = ScaleCharToQuantum( colors.g[code] );
- q->blue = ScaleCharToQuantum( colors.b[code] );
-
+ SetPixelRed(image,ScaleCharToQuantum(colors.r[code]),q);
+ SetPixelGreen(image,ScaleCharToQuantum(colors.g[code]),q);
+ SetPixelBlue(image,ScaleCharToQuantum(colors.b[code]),q);
/* Extract alpha value */
- alpha_code = (unsigned long) (alpha_bits >> (3*(4*j+i))) & 0x7;
+ alpha_code = (size_t) (alpha_bits >> (3*(4*j+i))) & 0x7;
if (alpha_code == 0)
alpha = a0;
else if (alpha_code == 1)
alpha = 255;
else
alpha = (((6-alpha_code) * a0 + (alpha_code-1) * a1) / 5);
-
- q->opacity = ScaleCharToQuantum( (unsigned char) (255 - alpha) );
-
- q++;
+ SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) alpha),q);
+ q+=GetPixelChannels(image);
}
}
}
return MagickTrue;
}
-static MagickBooleanType ReadUncompressedRGB(Image *image, DDSInfo *dds_info)
+static MagickBooleanType ReadUncompressedRGB(Image *image, DDSInfo *dds_info,
+ ExceptionInfo *exception)
{
- ExceptionInfo
- *exception;
-
- long
+ ssize_t
x, y;
- PixelPacket
+ register Quantum
*q;
- exception=(&image->exception);
- for (y = 0; y < (long) dds_info->height; y++)
+ for (y = 0; y < (ssize_t) dds_info->height; y++)
{
q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
- if (q == (PixelPacket *) NULL)
+ if (q == (Quantum *) NULL)
return MagickFalse;
- for (x = 0; x < (long) dds_info->width; x++)
+ for (x = 0; x < (ssize_t) dds_info->width; x++)
{
- q->blue = ScaleCharToQuantum( (unsigned char) ReadBlobByte(image) );
- q->green = ScaleCharToQuantum( (unsigned char) ReadBlobByte(image) );
- q->red = ScaleCharToQuantum( (unsigned char) ReadBlobByte(image) );
- q++;
+ SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
+ ReadBlobByte(image)),q);
+ SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
+ ReadBlobByte(image)),q);
+ SetPixelRed(image,ScaleCharToQuantum((unsigned char)
+ ReadBlobByte(image)),q);
+ if (dds_info->pixelformat.rgb_bitcount == 32)
+ (void) ReadBlobByte(image);
+ q+=GetPixelChannels(image);
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
return MagickTrue;
}
-static MagickBooleanType ReadUncompressedRGBA(Image *image, DDSInfo *dds_info)
+static MagickBooleanType ReadUncompressedRGBA(Image *image, DDSInfo *dds_info,
+ ExceptionInfo *exception)
{
- ExceptionInfo
- *exception;
-
- long
+ ssize_t
x, y;
- PixelPacket
+ register Quantum
*q;
- exception=(&image->exception);
- for (y = 0; y < (long) dds_info->height; y++)
+ for (y = 0; y < (ssize_t) dds_info->height; y++)
{
q = QueueAuthenticPixels(image, 0, y, dds_info->width, 1,exception);
- if (q == (PixelPacket *) NULL)
+ if (q == (Quantum *) NULL)
return MagickFalse;
- for (x = 0; x < (long) dds_info->width; x++)
+ for (x = 0; x < (ssize_t) dds_info->width; x++)
{
- q->blue = ScaleCharToQuantum( (unsigned char) ReadBlobByte(image) );
- q->green = ScaleCharToQuantum( (unsigned char) ReadBlobByte(image) );
- q->red = ScaleCharToQuantum( (unsigned char) ReadBlobByte(image) );
- q->opacity = ScaleCharToQuantum( (unsigned char) (255 - ReadBlobByte(image)) );
- q++;
+ SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
+ ReadBlobByte(image)),q);
+ SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
+ ReadBlobByte(image)),q);
+ SetPixelRed(image,ScaleCharToQuantum((unsigned char)
+ ReadBlobByte(image)),q);
+ SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
+ ReadBlobByte(image)),q);
+ q+=GetPixelChannels(image);
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
*/
static void SkipDXTMipmaps(Image *image, DDSInfo *dds_info, int texel_size)
{
- register long
- i;
-
MagickOffsetType
offset;
- unsigned long
+ register ssize_t
+ i;
+
+ size_t
h,
w;
/*
Mipmapcount includes the main image, so start from one
*/
- for (i = 1; (i < (long) dds_info->mipmapcount) && w && h; i++)
+ for (i = 1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
{
offset = (MagickOffsetType) ((w + 3) / 4) * ((h + 3) / 4) * texel_size;
(void) SeekBlob(image, offset, SEEK_CUR);
MagickOffsetType
offset;
- register long
+ register ssize_t
i;
- unsigned long
+ size_t
h,
w;
/*
Mipmapcount includes the main image, so start from one
*/
- for (i=1; (i < (long) dds_info->mipmapcount) && w && h; i++)
+ for (i=1; (i < (ssize_t) dds_info->mipmapcount) && w && h; i++)
{
offset = (MagickOffsetType) w * h * pixel_size;
(void) SeekBlob(image, offset, SEEK_CUR);
% RegisterDDSImage(void)
%
*/
-ModuleExport unsigned long RegisterDDSImage(void)
+ModuleExport size_t RegisterDDSImage(void)
{
MagickInfo
*entry;
entry = SetMagickInfo("DDS");
entry->decoder = (DecodeImageHandler *) ReadDDSImage;
entry->magick = (IsImageFormatHandler *) IsDDS;
+ entry->seekable_stream=MagickTrue;
entry->description = ConstantString("Microsoft DirectDraw Surface");
entry->module = ConstantString("DDS");
(void) RegisterMagickInfo(entry);