/// <summary> /// Computes pitch information about an image given its DXGI format and uncompressed dimensions. /// </summary> /// <param name="format">Format of the image data.</param> /// <param name="width">Uncompressed width, in texels.</param> /// <param name="height">Uncompressed height, in texels.</param> /// <param name="rowPitch">Total # of bytes per scanline.</param> /// <param name="slicePitch">Total # of bytes per slice (if 3D texture).</param> /// <param name="widthCount">Compressed width, if the format is a compressed image format, otherwise the given width.</param> /// <param name="heightCount">Compressed height, if the format is a compressed image format, otherwise the given height.</param> /// <param name="bytesPerPixel">Gets the size of the format.</param> /// <param name="legacyDword">True if need to use workaround computation for some incorrectly created DDS files based on legacy DirectDraw assumptions about pitch alignment.</param> public static void ComputePitch(DDS.DXGIFormat format, int width, int height, out int rowPitch, out int slicePitch, out int widthCount, out int heightCount, out int bytesPerPixel, bool legacyDword = false) { widthCount = width; heightCount = height; if (DDS.FormatConverter.IsCompressed(format)) { int blockSize = DDS.FormatConverter.GetCompressedBlockSize(format); widthCount = Math.Max(1, (width + 3) / 4); heightCount = Math.Max(1, (height + 3) / 4); rowPitch = widthCount * blockSize; slicePitch = rowPitch * heightCount; bytesPerPixel = blockSize; } else if (DDS.FormatConverter.IsPacked(format)) { rowPitch = ((width + 1) >> 1) * 4; slicePitch = rowPitch * height; int bitsPerPixel = DDS.FormatConverter.GetBitsPerPixel(format); bytesPerPixel = Math.Max(1, bitsPerPixel / 8); } else { int bitsPerPixel = DDS.FormatConverter.GetBitsPerPixel(format); bytesPerPixel = Math.Max(1, bitsPerPixel / 8); if (legacyDword) { //Allow for old DDS files that based pitch on certain assumptions rowPitch = ((width * bitsPerPixel + 31) / 32) * sizeof(int); slicePitch = rowPitch * height; } else { rowPitch = (width * bitsPerPixel + 7) / 8; slicePitch = rowPitch * height; } } }
/// <summary> /// Computes pitch information about an image given its DXGI format and uncompressed dimensions. /// </summary> /// <param name="format">Format of the image data.</param> /// <param name="width">Uncompressed width, in texels.</param> /// <param name="height">Uncompressed height, in texels.</param> /// <param name="rowPitch">Total # of bytes per scanline.</param> /// <param name="slicePitch">Total # of bytes per slice (if 3D texture).</param> /// <param name="widthCount">Compressed width, if the format is a compressed image format, otherwise the given width.</param> /// <param name="heightCount">Compressed height, if the format is a compressed image format, otherwise the given height.</param> /// <param name="legacyDword">True if need to use workaround computation for some incorrectly created DDS files based on legacy DirectDraw assumptions about pitch alignment.</param> public static void ComputePitch(DDS.DXGIFormat format, int width, int height, out int rowPitch, out int slicePitch, out int widthCount, out int heightCount, bool legacyDword = false) { int bytesPerPixel; ComputePitch(format, width, height, out rowPitch, out slicePitch, out widthCount, out heightCount, out bytesPerPixel, legacyDword); }