public ImageMipMapHandler(string imageWithMipMaps, byte[] data)
{
imageWithMipMaps = (imageWithMipMaps == "") ? ".dds" : imageWithMipMaps;
imageList = new List<ImageFile>();
string fileFormat = Path.GetExtension(imageWithMipMaps).ToLowerInvariant();
string fileName = Path.GetFileNameWithoutExtension(imageWithMipMaps);
ImageFile imageMipMap;
int headerSize;
if (data != null)
{
headerSize = 128;
imageMipMap = new DDS("", data);
}
else
{
switch (fileFormat)
{
case ".dds":
headerSize = 128;
imageMipMap = new DDS(imageWithMipMaps, null);
break;
case ".tga":
headerSize = 18;
imageMipMap = new TGA(imageWithMipMaps, null);
break;
default: throw new FormatException("Invalid image format");
}
}
//Console.WriteLine("Image Format: {0}", imageMipMap.format);
//check if image has mipmaps
/* using (FileStream imageStream = File.OpenRead(imageWithMipMaps))
{
long size = ImageMipMapDataSize(imageMipMap.imgSize, CprFormat(imageMipMap.format), imageMipMap.BPP);
if (imageStream.Length - headerSize != ImageMipMapDataSize(imageMipMap.imgSize, CprFormat(imageMipMap.format), imageMipMap.BPP))
{
//MessageBox.Show("bytes in file: " + (imageStream.Length - headerSize) + ", bytes calulated: " + ImageMipMapDataSize(imageMipMap.imgSize, imageMipMap.format, imageMipMap.BPP) + ", BPP: " + imageMipMap.BPP + ", format: " + imageMipMap.format);
//Console.WriteLine("bytes in file: {0}, bytes calulated: {1}, BPP: {2}", imageStream.Length - headerSize, ImageMipMapDataSize(imageMipMap.imgSize, imageMipMap.format, imageMipMap.BPP), imageMipMap.BPP);
throw new FormatException("The image doesn't have any mipmaps");
}
} */
// KFreon: This can be wrong too much. Single mip images etc.
/*
var size = ImageMipMapDataSize(imageMipMap.imgSize, CprFormat(imageMipMap.format), imageMipMap.BPP);
if (imageMipMap.imgData.Length != size)
throw new FormatException("The image doesn't have any mipmaps");*/
byte[] buffer = null;
// add the first tga image
if (fileFormat == ".tga")
{
//buffer = new byte[imageMipMap.imgData.Length];
buffer = imageMipMap.imgData;
//imageList.Add(imageMipMap);
}
// Heff: Use Max to support 1x1 mips for 1:2 ratio images.
int maxCount = (int)Math.Max(imageMipMap.imgSize.width, imageMipMap.imgSize.height);
int count = 1;
int imgDataPos = 0;
while (count <= maxCount)
{
ImageFile newImageFile;
ImageSize newImageSize = imageMipMap.imgSize / count;
//if (newImageSize.width < 4 || newImageSize.height < 4)
// break;
int imgDataSize = (int)ImageDataSize(newImageSize, imageMipMap.format, imageMipMap.BPP);
if (fileFormat == ".dds")
{
buffer = new byte[imgDataSize];
Buffer.BlockCopy(imageMipMap.imgData, imgDataPos, buffer, 0, imgDataSize);
imgDataPos += imgDataSize;
if (imageMipMap.format == "R8G8B8") // Automatic conversion to 32-bit
{
buffer = ConvertTo32bit(buffer, (int)newImageSize.width, (int)newImageSize.height);
newImageFile = new DDS(fileName + "_" + newImageSize + fileFormat, newImageSize, "A8R8G8B8", buffer);
}
else
newImageFile = new DDS(fileName + "_" + newImageSize + fileFormat, newImageSize, imageMipMap.format, buffer);
}
else if (fileFormat == ".tga")
{
newImageFile = new TGA(fileName + "_" + newImageSize + fileFormat, newImageSize, imageMipMap.format, buffer);
if (newImageSize != new ImageSize(1, 1))
buffer = ShrinkImage(buffer, newImageSize, imageMipMap.BPP);
}
else
throw new FormatException("Invalid image format");
imageList.Add(newImageFile);
count *= 2;
}
}