10#include "fimBmpIo.hpp"
11#include "FgSerial.hpp"
12#include "fimImgOps.hpp"
36 String8
const & fname,
39 Ifstream ifs(fname,
false);
43 ifs.read(&magic[0],2);
45 fgThrow(
"File is not a BMP file",fname);
49 ifs.read((
char*)&bmpSize,4);
50 ifs.read((
char*)&dummy,4);
51 ifs.read((
char*)&offset,4);
53 ifs.read((
char*)&header,
sizeof(header));
54 if ((header.header_sz != 40) ||
55 (header.nplanes != 1) ||
56 (!((header.bitspp == 32) || (header.bitspp == 24) || (header.bitspp == 8))) ||
57 (header.compress_type != 0))
58 fgThrow(
"Unsupported DIB type",fname);
59 img.resize(header.width,header.height);
60 uint skip = offset-54;
61 for (uint ii=0; ii<skip; ++ii)
62 ifs.read((
char*)&dummy,1);
63 uint nr = img.height() - 1;
64 for (uint row=0; row<img.height(); ++row)
66 for (uint col=0; col<img.width(); ++col)
68 ifs.read((
char*)&(img[nr-row][col].c[2]),1);
69 if (header.bitspp == 32)
71 ifs.read((
char*)&(img[nr-row][col].c[1]),1);
72 ifs.read((
char*)&(img[nr-row][col].c[0]),1);
73 ifs.read((
char*)&(img[nr-row][col].c[3]),1);
75 else if (header.bitspp == 24)
77 ifs.read((
char*)&(img[nr-row][col].c[1]),1);
78 ifs.read((
char*)&(img[nr-row][col].c[0]),1);
79 img[nr-row][col].c[3] = 255;
83 img[nr-row][col].c[1] = img[nr-row][col].c[2];
84 img[nr-row][col].c[0] = img[nr-row][col].c[2];
85 img[nr-row][col].c[3] = 255;
94 String8
const & fname,
98 if (!fg3ReadBmpSimple(fname,tmp))
100 img.resize(tmp.width(),tmp.height());
101 for (uint row=0; row<img.height(); ++row)
102 for (uint col=0; col<img.width(); ++col)
103 img[row][col] = tmp[row][col].c[0];
109 String8
const & fname,
110 const FimImgRgbaUbC & img)
112 Ofstream ofs(fname,
false);
116 uint32 bmpSize = 3 * img.width() * img.height() + 54,
119 ofs.write((
char*)&bmpSize,4);
120 ofs.write((
char*)&dummy,4);
121 ofs.write((
char*)&offset,4);
123 header.header_sz = 40;
124 header.width = img.width();
125 header.height = img.height();
128 header.compress_type = 0;
129 header.bmp_bytesz = 3 * img.width() * img.height();
133 header.nimpcolors = 0;
134 ofs.write((
char*)&header,40);
135 uint nr = img.height() - 1;
136 for (uint row=0; row<img.height(); ++row)
138 for (uint col=0; col<img.width(); ++col)
140 ofs.write((
char*)&(img[nr-row][col].c[2]),1);
141 ofs.write((
char*)&(img[nr-row][col].c[1]),1);
142 ofs.write((
char*)&(img[nr-row][col].c[0]),1);