Commit 5c6411b8e for imagemagick.org
commit 5c6411b8ec1a17bb33f7949520b350b932cfcb5c
Author: Cristy <urban-warrior@imagemagick.org>
Date: Sat Nov 28 18:14:16 2026 -0500
DX10 sRGB variants (R8G8B8A8, B8G8R8A8, B8G8R8X8, BC1, BC2, BC3) are now decoded
diff --git a/coders/dds.c b/coders/dds.c
index fd43aba0c..d04386437 100644
--- a/coders/dds.c
+++ b/coders/dds.c
@@ -229,12 +229,12 @@ typedef enum DXGI_FORMAT
DXGI_FORMAT_P8,
DXGI_FORMAT_A8P8,
DXGI_FORMAT_B4G4R4A4_UNORM,
- DXGI_FORMAT_P208,
+ DXGI_FORMAT_P208 = 130,
DXGI_FORMAT_V208,
DXGI_FORMAT_V408,
- DXGI_FORMAT_SAMPLER_FEEDBACK_MIN_MIP_OPAQUE,
+ DXGI_FORMAT_SAMPLER_FEEDBACK_MIN_MIP_OPAQUE = 189,
DXGI_FORMAT_SAMPLER_FEEDBACK_MIP_REGION_USED_OPAQUE,
- DXGI_FORMAT_FORCE_UINT
+ DXGI_FORMAT_FORCE_UINT = 0xffffffff
} DXGI_FORMAT;
@@ -1246,14 +1246,11 @@ static inline void VectorTruncate3(DDSVector3 *value)
static inline size_t ClampToLimit(const float value, const size_t limit)
{
- size_t
- result = (size_t) (value + 0.5f);
-
- if (result < 0.0f)
+ if (!(value > 0.0f))
return(0);
- if (result > limit)
+ if (value >= (float) limit)
return(limit);
- return result;
+ return((size_t) (value + 0.5f));
}
static inline size_t ColorTo565(const DDSVector3 point)
@@ -1375,6 +1372,8 @@ static MagickBooleanType ReadDDSInfo(Image *image, DDSInfo *dds_info)
dds_info->extFlags2 = 0;
}
+ if (EOFBlob(image) != MagickFalse)
+ return(MagickFalse);
return(MagickTrue);
}
@@ -1451,6 +1450,9 @@ static MagickBooleanType ReadMipmaps(const ImageInfo *image_info,Image *image,
if (image->next == (Image *) NULL)
return(MagickFalse);
image->next->alpha_trait=image->alpha_trait;
+ image->next->compression=image->compression;
+ image->next->endian=LSBEndian;
+ image->next->depth=image->depth;
image=SyncNextImageInList(image);
status=SetImageExtent(image,w,h,exception);
if (status == MagickFalse)
@@ -1880,8 +1882,8 @@ static unsigned char GetBits(const unsigned char *block,size_t *start_bit,
{
first_bits=8-base;
next_bits=num_bits-first_bits;
- ret=((block[index] >> base) | (((block[index + 1]) &
- ((1u << next_bits) - 1)) << first_bits));
+ ret=(unsigned char) ((block[index] >> base) | (((index < 15 ?
+ block[index + 1] : 0) & ((1u << next_bits) - 1)) << first_bits));
}
else
{
@@ -2063,15 +2065,17 @@ static MagickBooleanType ReadBC5Pixels(Image *image,
for (x = 0; x < (ssize_t)image->columns; x += 4)
{
size_t
- area;
+ columns,
+ rows;
ssize_t
count;
+ columns=(size_t) MagickMin(4,(ssize_t) image->columns-x);
+ rows=(size_t) MagickMin(4,(ssize_t) image->rows-y);
+
/* Get 4x4 patch of pixels to write on */
- q=QueueAuthenticPixels(image,x,y,(size_t)
- MagickMin(4,(ssize_t) image->columns-x),(size_t)
- MagickMin(4,(ssize_t) image->rows-y),exception);
+ q=QueueAuthenticPixels(image,x,y,columns,rows,exception);
if (q == (Quantum *)NULL)
return(MagickFalse);
@@ -2116,17 +2120,21 @@ static MagickBooleanType ReadBC5Pixels(Image *image,
colors.g[7]=255;
}
- /* Write the pixels */
- area=(size_t) (MagickMin(MagickMin(4,(ssize_t) image->columns-x)*
- MagickMin(4,(ssize_t) image->rows-y),16));
+ /* Write the pixels (index layout is always 4x4) */
start_bit_r=16;
start_bit_g=80;
- for (i = 0; i < (ssize_t) area; i++)
+ for (i = 0; i < 16; i++)
{
- SetPixelRed(image,ScaleCharToQuantum(colors.r[GetBits(block,
- &start_bit_r,3)]),q);
- SetPixelGreen(image,ScaleCharToQuantum(colors.g[GetBits(block,
- &start_bit_g,3)]),q);
+ unsigned char
+ index_g,
+ index_r;
+
+ index_r=GetBits(block,&start_bit_r,3);
+ index_g=GetBits(block,&start_bit_g,3);
+ if (((i & 3) >= (ssize_t) columns) || ((i >> 2) >= (ssize_t) rows))
+ continue;
+ SetPixelRed(image,ScaleCharToQuantum(colors.r[index_r]),q);
+ SetPixelGreen(image,ScaleCharToQuantum(colors.g[index_g]),q);
SetPixelBlue(image,(Quantum) 0,q);
q+=(ptrdiff_t) GetPixelChannels(image);
@@ -2204,12 +2212,14 @@ static MagickBooleanType ReadBC7Pixels(Image *image,
for (x = 0; x < (ssize_t) image->columns; x += 4)
{
size_t
- area;
+ columns,
+ rows;
+
+ columns=(size_t) MagickMin(4,(ssize_t) image->columns-x);
+ rows=(size_t) MagickMin(4,(ssize_t) image->rows-y);
/* Get 4x4 patch of pixels to write on */
- q=QueueAuthenticPixels(image,x,y,(size_t)
- MagickMin(4,(ssize_t) image->columns-x),(size_t)
- MagickMin(4,(ssize_t) image->rows-y),exception);
+ q=QueueAuthenticPixels(image,x,y,columns,rows,exception);
if (q == (Quantum *) NULL)
return(MagickFalse);
@@ -2223,13 +2233,32 @@ static MagickBooleanType ReadBC7Pixels(Image *image,
if (EOFBlob(image) != MagickFalse)
return(MagickFalse);
- /* Get the mode of the block */
- start_bit=0;
- while (start_bit <= 8 && !GetBit(block, &start_bit)) {}
- mode=start_bit-1;
-
+ /* Get the mode of the block (position of the first set bit) */
+ mode=0;
+ while ((mode < 8) && (((block[0] >> mode) & 1) == 0))
+ mode++;
if (mode > 7)
- return(MagickFalse);
+ {
+ /* Reserved mode: the specification says to return zeros */
+ ssize_t
+ j;
+
+ for (j=0; j < (ssize_t) rows; j++)
+ {
+ for (i=0; i < (ssize_t) columns; i++)
+ {
+ SetPixelRed(image,(Quantum) 0,q);
+ SetPixelGreen(image,(Quantum) 0,q);
+ SetPixelBlue(image,(Quantum) 0,q);
+ SetPixelAlpha(image,(Quantum) 0,q);
+ q+=(ptrdiff_t) GetPixelChannels(image);
+ }
+ }
+ if (SyncAuthenticPixels(image,exception) == MagickFalse)
+ return(MagickFalse);
+ continue;
+ }
+ start_bit=mode+1;
num_subsets=BC7_mode_info[mode].num_subsets;
partition_id=0;
@@ -2281,14 +2310,15 @@ static MagickBooleanType ReadBC7Pixels(Image *image,
alpha_indices[i]=GetBits(block,&start_bit,index2_prec);
}
- /* Write the pixels */
- area=(size_t) (MagickMin(MagickMin(4,(ssize_t) image->columns-x)*
- MagickMin(4,(ssize_t) image->rows-y),16));
- for (i=0; i < (ssize_t) area; i++)
+ /* Write the pixels (index layout is always 4x4) */
+ for (i=0; i < 16; i++)
{
unsigned char
c2;
+ if (((i & 3) >= (ssize_t) columns) || ((i >> 2) >= (ssize_t) rows))
+ continue;
+
c0=2 * subset_indices[i];
c1=(2 * subset_indices[i]) + 1;
c2=color_indices[i];
@@ -2421,6 +2451,9 @@ static MagickBooleanType ReadUncompressedRGBPixels(Image *image,
unsigned short
color;
+ if ((dds_info->pixelformat.rgb_bitcount == 8) ||
+ (dds_info->extFormat == DXGI_FORMAT_R8_UNORM))
+ (void) SetImageType(image,GrayscaleType,exception);
r_mask=dds_info->pixelformat.r_bitmask;
g_mask=dds_info->pixelformat.g_bitmask;
b_mask=dds_info->pixelformat.b_bitmask;
@@ -2451,7 +2484,7 @@ static MagickBooleanType ReadUncompressedRGBPixels(Image *image,
else if ((dds_info->pixelformat.rgb_bitcount == 16) ||
(dds_info->extFormat == DXGI_FORMAT_B5G6R5_UNORM))
{
- color=ReadBlobShort(image);
+ color=ReadBlobLSBShort(image);
SetPixelRed(image,ScaleCharToQuantum(ExtractChannelFromMask(
color,r_mask,r_shift,r_max)),q);
SetPixelGreen(image,ScaleCharToQuantum(ExtractChannelFromMask(
@@ -2565,15 +2598,37 @@ static MagickBooleanType SkipRGBMipmaps(Image *image,const DDSInfo *dds_info,
offset=(MagickOffsetType) (w*h*(size_t) pixel_size);
if (SeekBlob(image,offset,SEEK_CUR) < 0)
break;
- w=DIV2(w);
- h=DIV2(h);
if ((w == 1) && (h == 1))
break;
+ w=DIV2(w);
+ h=DIV2(h);
}
}
return(MagickTrue);
}
+static inline int GetUncompressedRGBPixelSize(const DDSInfo *dds_info)
+{
+ if ((dds_info->pixelformat.rgb_bitcount == 8) ||
+ (dds_info->extFormat == DXGI_FORMAT_R8_UNORM))
+ return(1);
+ if ((dds_info->pixelformat.rgb_bitcount == 16) ||
+ (dds_info->extFormat == DXGI_FORMAT_B5G6R5_UNORM))
+ return(2);
+ if ((dds_info->pixelformat.rgb_bitcount == 32) ||
+ (dds_info->extFormat == DXGI_FORMAT_B8G8R8X8_UNORM))
+ return(4);
+ return(3);
+}
+
+static inline int GetUncompressedRGBAPixelSize(const DDSInfo *dds_info)
+{
+ if ((dds_info->pixelformat.rgb_bitcount == 16) ||
+ (dds_info->extFormat == DXGI_FORMAT_B5G5R5A1_UNORM))
+ return(2);
+ return(4);
+}
+
static MagickBooleanType ReadUncompressedRGB(const ImageInfo *image_info,
Image *image,const DDSInfo *dds_info,const MagickBooleanType read_mipmaps,
ExceptionInfo *exception)
@@ -2593,7 +2648,8 @@ static MagickBooleanType ReadUncompressedRGB(const ImageInfo *image_info,
return(ReadMipmaps(image_info,image,dds_info,ReadUncompressedRGBPixels,
exception));
else
- return(SkipRGBMipmaps(image,dds_info,3,exception));
+ return(SkipRGBMipmaps(image,dds_info,GetUncompressedRGBPixelSize(dds_info),
+ exception));
}
static MagickBooleanType ReadUncompressedAlpha(const ImageInfo *image_info,
@@ -2687,7 +2743,7 @@ static MagickBooleanType ReadUncompressedRGBAPixels(Image *image,
if ((dds_info->pixelformat.rgb_bitcount == 16) ||
(dds_info->extFormat == DXGI_FORMAT_B5G5R5A1_UNORM))
{
- color=ReadBlobShort(image);
+ color=ReadBlobLSBShort(image);
SetPixelAlpha(image,ScaleCharToQuantum(ExtractChannelFromMask(
color,a_mask,a_shift,a_max)),q);
if (is_grayscale != MagickFalse)
@@ -2708,13 +2764,13 @@ static MagickBooleanType ReadUncompressedRGBAPixels(Image *image,
pixel=ReadBlobLSBLong(image);
SetPixelRed(image,ScaleShortToQuantum((unsigned short)
- ((pixel & 0x3ff)/1023.0)*65535),q);
- SetPixelBlue(image,ScaleShortToQuantum((unsigned short)
- (((pixel >> 10) & 0x3ff)/1023.0)*65535),q);
+ ((((pixel & 0x3ff)/1023.0)*65535.0)+0.5)),q);
SetPixelGreen(image,ScaleShortToQuantum((unsigned short)
- (((pixel >> 20) & 0x3ff)/1023.0)*65535),q);
+ (((((pixel >> 10) & 0x3ff)/1023.0)*65535.0)+0.5)),q);
+ SetPixelBlue(image,ScaleShortToQuantum((unsigned short)
+ (((((pixel >> 20) & 0x3ff)/1023.0)*65535.0)+0.5)),q);
SetPixelAlpha(image,ScaleShortToQuantum((unsigned short)
- (((pixel >> 30) & 3)/3.0)*65535),q);
+ (((((pixel >> 30) & 3)/3.0)*65535.0)+0.5)),q);
}
else if ((dds_info->extFormat == DXGI_FORMAT_R8G8B8A8_UNORM) ||
(is_rgba != MagickFalse))
@@ -2760,7 +2816,8 @@ static MagickBooleanType ReadUncompressedRGBA(const ImageInfo *image_info,
return(ReadMipmaps(image_info,image,dds_info,ReadUncompressedRGBAPixels,
exception));
else
- return(SkipRGBMipmaps(image,dds_info,4,exception));
+ return(SkipRGBMipmaps(image,dds_info,GetUncompressedRGBAPixelSize(dds_info),
+ exception));
}
/*
@@ -2929,6 +2986,31 @@ static Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
ThrowReaderException(ImageError,"ImageTypeNotSupported");
}
+ /* sRGB variants decode identically to their UNORM counterparts */
+ switch (dds_info.extFormat)
+ {
+ case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
+ dds_info.extFormat=DXGI_FORMAT_R8G8B8A8_UNORM;
+ break;
+ case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
+ dds_info.extFormat=DXGI_FORMAT_B8G8R8A8_UNORM;
+ break;
+ case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
+ dds_info.extFormat=DXGI_FORMAT_B8G8R8X8_UNORM;
+ break;
+ case DXGI_FORMAT_BC1_UNORM_SRGB:
+ dds_info.extFormat=DXGI_FORMAT_BC1_UNORM;
+ break;
+ case DXGI_FORMAT_BC2_UNORM_SRGB:
+ dds_info.extFormat=DXGI_FORMAT_BC2_UNORM;
+ break;
+ case DXGI_FORMAT_BC3_UNORM_SRGB:
+ dds_info.extFormat=DXGI_FORMAT_BC3_UNORM;
+ break;
+ default:
+ break;
+ }
+
switch (dds_info.extFormat)
{
case DXGI_FORMAT_R8_UNORM:
@@ -3025,8 +3107,6 @@ static Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
if (dds_info.extFlags & DDSEXTFLAGS_CUBEMAP)
cubemap=MagickTrue;
-
- num_images=dds_info.extArraySize;
break;
}
default:
@@ -3058,7 +3138,17 @@ static Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
}
if (volume)
- num_images = dds_info.depth;
+ {
+ /*
+ Volume mipmaps store all (halved) slices per level, so they cannot be
+ handled per slice: only read the slices of the top level.
+ */
+ num_images = dds_info.depth;
+ dds_info.ddscaps1 &= ~((size_t) DDSCAPS_MIPMAP);
+ }
+
+ if ((dds_info.width == 0) || (dds_info.height == 0))
+ ThrowReaderException(CorruptImageError,"ImproperImageHeader");
if ((num_images == 0) || (num_images > GetBlobSize(image)))
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
@@ -3077,9 +3167,14 @@ static Image *ReadDDSImage(const ImageInfo *image_info,ExceptionInfo *exception)
/* Start a new image */
if (EOFBlob(image) != MagickFalse)
ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
+ /* Mipmaps may already have been appended: go to the end of the list */
+ image=GetLastImageInList(image);
AcquireNextImage(image_info,image,exception);
if (GetNextImageInList(image) == (Image *) NULL)
- return(DestroyImageList(image));
+ {
+ (void) CloseBlob(image);
+ return(DestroyImageList(image));
+ }
image=SyncNextImageInList(image);
}
@@ -3266,11 +3361,14 @@ static size_t CompressAlpha(const size_t min, const size_t max,
index = 0;
for (j=0; j<8; j++)
{
+ ssize_t
+ diff;
+
size_t
dist;
- dist = value - (size_t)codes[j];
- dist *= dist;
+ diff = (ssize_t) value - (ssize_t) codes[j];
+ dist = (size_t) (diff*diff);
if (dist < least)
{
@@ -4225,7 +4323,10 @@ static MagickBooleanType WriteUncompressed(Image *image,
{
p=GetVirtualPixels(image,0,y,image->columns,1,exception);
if (p == (const Quantum *) NULL)
- break;
+ {
+ status=MagickFalse;
+ break;
+ }
q=pixels;
for (x=0; x < (ssize_t) image->columns; x++)
@@ -4311,8 +4412,8 @@ static MagickBooleanType WriteMipmaps(Image *image,const ImageInfo *image_info,
DestroyBlob(mipmap_image);
mipmap_image->blob=ReferenceBlob(image->blob);
- status=WriteImageData(mipmap_image,pixelFormat,compression,weightByAlpha,
- clusterFit,exception);
+ status=WriteImageData(mipmap_image,pixelFormat,compression,clusterFit,
+ weightByAlpha,exception);
if (fromlist == MagickFalse)
{
@@ -4471,8 +4572,9 @@ static MagickBooleanType WriteDDSImage(const ImageInfo *image_info,
(void) TransformImageColorspace(image,sRGBColorspace,exception);
pixelFormat=DDPF_FOURCC;
compression=FOURCC_DXT5;
- if (((image->alpha_trait & BlendPixelTrait) == 0) ||
- (LocaleCompare(image_info->magick,"dxt1") == 0) ||
+ if ((image->alpha_trait & BlendPixelTrait) == 0)
+ compression=FOURCC_DXT1;
+ if ((LocaleCompare(image_info->magick,"dxt1") == 0) ||
(image_info->compression == DXT1Compression))
compression=FOURCC_DXT1;
else if (image_info->compression == NoCompression)