2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* Windows BMP codec
|
2006-04-24 01:14:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "lib/byte_order.h"
|
|
|
|
#include "tex_codec.h"
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
|
|
|
struct BmpHeader
|
|
|
|
{
|
|
|
|
// BITMAPFILEHEADER
|
|
|
|
u16 bfType; // "BM"
|
|
|
|
u32 bfSize; // of file
|
|
|
|
u16 bfReserved1;
|
|
|
|
u16 bfReserved2;
|
|
|
|
u32 bfOffBits; // offset to image data
|
|
|
|
|
|
|
|
// BITMAPINFOHEADER
|
|
|
|
u32 biSize;
|
2010-05-01 11:45:58 +02:00
|
|
|
i32 biWidth;
|
|
|
|
i32 biHeight;
|
2006-04-24 01:14:18 +02:00
|
|
|
u16 biPlanes;
|
|
|
|
u16 biBitCount;
|
|
|
|
u32 biCompression;
|
|
|
|
u32 biSizeImage;
|
|
|
|
// the following are unused and zeroed when writing:
|
2010-05-01 11:45:58 +02:00
|
|
|
i32 biXPelsPerMeter;
|
|
|
|
i32 biYPelsPerMeter;
|
2006-04-24 01:14:18 +02:00
|
|
|
u32 biClrUsed;
|
|
|
|
u32 biClrImportant;
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
#define BI_RGB 0 // biCompression
|
|
|
|
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
static LibError bmp_transform(Tex* UNUSED(t), size_t UNUSED(transforms))
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::TEX_CODEC_CANNOT_HANDLE;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool bmp_is_hdr(const u8* file)
|
|
|
|
{
|
|
|
|
// check header signature (bfType == "BM"?).
|
|
|
|
// we compare single bytes to be endian-safe.
|
|
|
|
return (file[0] == 'B' && file[1] == 'M');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
static bool bmp_is_ext(const OsPath& extension)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
return extension == L".bmp";
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t bmp_hdr_size(const u8* file)
|
|
|
|
{
|
|
|
|
const size_t hdr_size = sizeof(BmpHeader);
|
|
|
|
if(file)
|
|
|
|
{
|
|
|
|
BmpHeader* hdr = (BmpHeader*)file;
|
|
|
|
const u32 ofs = read_le32(&hdr->bfOffBits);
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(ofs >= hdr_size && "bmp_hdr_size invalid");
|
2006-04-24 01:14:18 +02:00
|
|
|
return ofs;
|
|
|
|
}
|
|
|
|
return hdr_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// requirements: uncompressed, direct colour, bottom up
|
2007-01-06 02:33:13 +01:00
|
|
|
static LibError bmp_decode(DynArray* RESTRICT da, Tex* RESTRICT t)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2008-01-13 12:25:19 +01:00
|
|
|
u8* file = da->base;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
const BmpHeader* hdr = (const BmpHeader*)file;
|
|
|
|
const long w = (long)read_le32(&hdr->biWidth);
|
|
|
|
const long h_ = (long)read_le32(&hdr->biHeight);
|
|
|
|
const u16 bpp = read_le16(&hdr->biBitCount);
|
|
|
|
const u32 compress = read_le32(&hdr->biCompression);
|
|
|
|
|
|
|
|
const long h = abs(h_);
|
|
|
|
|
2008-09-18 13:27:55 +02:00
|
|
|
size_t flags = 0;
|
2006-04-24 01:14:18 +02:00
|
|
|
flags |= (h_ < 0)? TEX_TOP_DOWN : TEX_BOTTOM_UP;
|
|
|
|
if(bpp > 16)
|
|
|
|
flags |= TEX_BGR;
|
|
|
|
if(bpp == 32)
|
|
|
|
flags |= TEX_ALPHA;
|
|
|
|
|
|
|
|
// sanity checks
|
|
|
|
if(compress != BI_RGB)
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::TEX_COMPRESSED);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
t->w = w;
|
|
|
|
t->h = h;
|
|
|
|
t->bpp = bpp;
|
|
|
|
t->flags = flags;
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-06 02:33:13 +01:00
|
|
|
static LibError bmp_encode(Tex* RESTRICT t, DynArray* RESTRICT da)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
const size_t hdr_size = sizeof(BmpHeader); // needed for BITMAPFILEHEADER
|
|
|
|
const size_t img_size = tex_img_size(t);
|
|
|
|
const size_t file_size = hdr_size + img_size;
|
2010-07-12 00:40:17 +02:00
|
|
|
const i32 h = (t->flags & TEX_TOP_DOWN)? -(i32)t->h : (i32)t->h;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2008-09-18 13:27:55 +02:00
|
|
|
size_t transforms = t->flags;
|
2006-04-24 01:14:18 +02:00
|
|
|
transforms &= ~TEX_ORIENTATION; // no flip needed - we can set top-down bit.
|
|
|
|
transforms ^= TEX_BGR; // BMP is native BGR.
|
|
|
|
|
|
|
|
const BmpHeader hdr =
|
|
|
|
{
|
|
|
|
// BITMAPFILEHEADER
|
2008-01-13 12:25:19 +01:00
|
|
|
0x4D42, // bfType = 'B','M'
|
2006-04-24 01:14:18 +02:00
|
|
|
(u32)file_size, // bfSize
|
|
|
|
0, 0, // bfReserved1,2
|
|
|
|
hdr_size, // bfOffBits
|
|
|
|
|
|
|
|
// BITMAPINFOHEADER
|
|
|
|
40, // biSize = sizeof(BITMAPINFOHEADER)
|
2010-07-12 00:40:17 +02:00
|
|
|
(i32)t->w,
|
2006-04-24 01:14:18 +02:00
|
|
|
h,
|
|
|
|
1, // biPlanes
|
2008-09-18 13:27:55 +02:00
|
|
|
(u16)t->bpp,
|
2006-04-24 01:14:18 +02:00
|
|
|
BI_RGB, // biCompression
|
|
|
|
(u32)img_size, // biSizeImage
|
|
|
|
0, 0, 0, 0 // unused (bi?PelsPerMeter, biClr*)
|
|
|
|
};
|
|
|
|
return tex_codec_write(t, transforms, &hdr, hdr_size, da);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEX_CODEC_REGISTER(bmp);
|