Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tex_bmp.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * Windows BMP codec
25  */
26 
27 #include "precompiled.h"
28 
29 #include "lib/byte_order.h"
30 #include "tex_codec.h"
31 
32 #pragma pack(push, 1)
33 
34 struct BmpHeader
35 {
36  // BITMAPFILEHEADER
37  u16 bfType; // "BM"
38  u32 bfSize; // of file
41  u32 bfOffBits; // offset to image data
42 
43  // BITMAPINFOHEADER
51  // the following are unused and zeroed when writing:
56 };
57 
58 #pragma pack(pop)
59 
60 #define BI_RGB 0 // biCompression
61 
62 
63 static Status bmp_transform(Tex* UNUSED(t), size_t UNUSED(transforms))
64 {
66 }
67 
68 
69 static bool bmp_is_hdr(const u8* file)
70 {
71  // check header signature (bfType == "BM"?).
72  // we compare single bytes to be endian-safe.
73  return (file[0] == 'B' && file[1] == 'M');
74 }
75 
76 
77 static bool bmp_is_ext(const OsPath& extension)
78 {
79  return extension == L".bmp";
80 }
81 
82 
83 static size_t bmp_hdr_size(const u8* file)
84 {
85  const size_t hdr_size = sizeof(BmpHeader);
86  if(file)
87  {
88  BmpHeader* hdr = (BmpHeader*)file;
89  const u32 ofs = read_le32(&hdr->bfOffBits);
90  ENSURE(ofs >= hdr_size && "bmp_hdr_size invalid");
91  return ofs;
92  }
93  return hdr_size;
94 }
95 
96 
97 // requirements: uncompressed, direct colour, bottom up
98 static Status bmp_decode(rpU8 data, size_t UNUSED(size), Tex* RESTRICT t)
99 {
100  const BmpHeader* hdr = (const BmpHeader*)data;
101  const long w = (long)read_le32(&hdr->biWidth);
102  const long h_ = (long)read_le32(&hdr->biHeight);
103  const u16 bpp = read_le16(&hdr->biBitCount);
104  const u32 compress = read_le32(&hdr->biCompression);
105 
106  const long h = abs(h_);
107 
108  size_t flags = 0;
109  flags |= (h_ < 0)? TEX_TOP_DOWN : TEX_BOTTOM_UP;
110  if(bpp > 16)
111  flags |= TEX_BGR;
112  if(bpp == 32)
113  flags |= TEX_ALPHA;
114 
115  // sanity checks
116  if(compress != BI_RGB)
118 
119  t->w = w;
120  t->h = h;
121  t->bpp = bpp;
122  t->flags = flags;
123  return INFO::OK;
124 }
125 
126 
128 {
129  const size_t hdr_size = sizeof(BmpHeader); // needed for BITMAPFILEHEADER
130  const size_t img_size = tex_img_size(t);
131  const size_t file_size = hdr_size + img_size;
132  const i32 h = (t->flags & TEX_TOP_DOWN)? -(i32)t->h : (i32)t->h;
133 
134  size_t transforms = t->flags;
135  transforms &= ~TEX_ORIENTATION; // no flip needed - we can set top-down bit.
136  transforms ^= TEX_BGR; // BMP is native BGR.
137 
138  const BmpHeader hdr =
139  {
140  // BITMAPFILEHEADER
141  0x4D42, // bfType = 'B','M'
142  (u32)file_size, // bfSize
143  0, 0, // bfReserved1,2
144  hdr_size, // bfOffBits
145 
146  // BITMAPINFOHEADER
147  40, // biSize = sizeof(BITMAPINFOHEADER)
148  (i32)t->w,
149  h,
150  1, // biPlanes
151  (u16)t->bpp,
152  BI_RGB, // biCompression
153  (u32)img_size, // biSizeImage
154  0, 0, 0, 0 // unused (bi?PelsPerMeter, biClr*)
155  };
156  return tex_codec_write(t, transforms, &hdr, hdr_size, da);
157 }
158 
159 TEX_CODEC_REGISTER(bmp);
#define u8
Definition: types.h:39
Status tex_codec_write(Tex *t, size_t transforms, const void *hdr, size_t hdr_size, DynArray *da)
apply transforms and then copy header and image into output buffer.
Definition: tex_codec.cpp:176
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
u16 bfReserved2
Definition: tex_bmp.cpp:40
u32 biCompression
Definition: tex_bmp.cpp:49
i32 biHeight
Definition: tex_bmp.cpp:46
u32 read_le32(const void *p)
Definition: byte_order.cpp:73
const Status TEX_CODEC_CANNOT_HANDLE
Definition: tex.h:132
const Status OK
Definition: status.h:386
static Status bmp_transform(Tex *t, size_t transforms)
Definition: tex_bmp.cpp:63
const Status TEX_COMPRESSED
Definition: tex.h:121
i32 biXPelsPerMeter
Definition: tex_bmp.cpp:52
u32 biSize
Definition: tex_bmp.cpp:44
#define i32
Definition: types.h:36
u16 biPlanes
Definition: tex_bmp.cpp:47
indicates the image contains an alpha channel.
Definition: tex.h:171
provides a memory range that can be expanded but doesn&#39;t waste physical memory or relocate itself...
Definition: dynarray.h:39
u32 bfOffBits
Definition: tex_bmp.cpp:41
static Status bmp_encode(Tex *RESTRICT t, DynArray *RESTRICT da)
Definition: tex_bmp.cpp:127
indicates B and R pixel components are exchanged.
Definition: tex.h:163
static Status bmp_decode(rpU8 data, size_t size, Tex *RESTRICT t)
Definition: tex_bmp.cpp:98
flags &amp; TEX_ORIENTATION is a field indicating orientation, i.e.
Definition: tex.h:190
uint8_t *__restrict rpU8
u16 bfReserved1
Definition: tex_bmp.cpp:39
i32 biWidth
Definition: tex_bmp.cpp:45
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
#define BI_RGB
Definition: tex_bmp.cpp:60
u32 biClrImportant
Definition: tex_bmp.cpp:55
Definition: path.h:75
u32 biSizeImage
Definition: tex_bmp.cpp:50
i64 Status
Error handling system.
Definition: status.h:171
u32 bfSize
Definition: tex_bmp.cpp:38
stores all data describing an image.
Definition: tex.h:210
u32 biClrUsed
Definition: tex_bmp.cpp:54
#define u16
Definition: types.h:40
const char * extension
Definition: mongoose.cpp:1736
#define u32
Definition: types.h:41
u16 read_le16(const void *p)
read a little-endian number from memory into native byte order.
Definition: byte_order.cpp:66
#define RESTRICT
u16 biBitCount
Definition: tex_bmp.cpp:48
#define TEX_CODEC_REGISTER(name)
build codec vtbl and register it.
Definition: tex_codec.h:137
#define WARN_RETURN(status)
Definition: status.h:255
static bool bmp_is_ext(const OsPath &extension)
Definition: tex_bmp.cpp:77
size_t tex_img_size(const Tex *t)
return total byte size of the image pixels.
Definition: tex.cpp:683
static size_t bmp_hdr_size(const u8 *file)
Definition: tex_bmp.cpp:83
static bool bmp_is_hdr(const u8 *file)
Definition: tex_bmp.cpp:69
i32 biYPelsPerMeter
Definition: tex_bmp.cpp:53
u16 bfType
Definition: tex_bmp.cpp:37