FaceGen 3 SDKs Reference
Loading...
Searching...
No Matches
fimBmpIo.cpp
1//
2// Copyright (c) Singular Inversions Inc. 2010
3//
4// Authors: Andrew Beatty
5// Created: July 7, 2010
6//
7
8#include "stdafx.h"
9
10#include "fimBmpIo.hpp"
11#include "FgSerial.hpp"
12#include "fimImgOps.hpp"
13#include "FgFile.hpp"
14
15using namespace std;
16
17namespace Fg {
18
19struct DibHeader
20{
21 uint32 header_sz;
22 uint32 width;
23 uint32 height;
24 uint16 nplanes;
25 uint16 bitspp;
26 uint32 compress_type;
27 uint32 bmp_bytesz;
28 uint32 hres;
29 uint32 vres;
30 uint32 ncolors;
31 uint32 nimpcolors;
32};
33
34bool
35fg3ReadBmpSimple(
36 String8 const & fname,
37 FimImgRgbaUbC & img)
38{
39 Ifstream ifs(fname,false);
40 if (!ifs)
41 return false;
42 string magic = " ";
43 ifs.read(&magic[0],2);
44 if (magic != "BM")
45 fgThrow("File is not a BMP file",fname);
46 uint32 bmpSize,
47 dummy,
48 offset;
49 ifs.read((char*)&bmpSize,4);
50 ifs.read((char*)&dummy,4);
51 ifs.read((char*)&offset,4);
52 DibHeader header;
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)
65 {
66 for (uint col=0; col<img.width(); ++col)
67 {
68 ifs.read((char*)&(img[nr-row][col].c[2]),1);
69 if (header.bitspp == 32)
70 {
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);
74 }
75 else if (header.bitspp == 24)
76 {
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;
80 }
81 else
82 {
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;
86 }
87 }
88 }
89 return true;
90}
91
92bool
93fg3ReadBmpSimple(
94 String8 const & fname,
95 FimImgUbC & img)
96{
97 FimImgRgbaUbC tmp;
98 if (!fg3ReadBmpSimple(fname,tmp))
99 return false;
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];
104 return true;
105}
106
107bool
108fg3WriteBmp(
109 String8 const & fname,
110 const FimImgRgbaUbC & img)
111{
112 Ofstream ofs(fname,false);
113 if (!ofs)
114 return false;
115 ofs.write("BM",2);
116 uint32 bmpSize = 3 * img.width() * img.height() + 54,
117 dummy = 0,
118 offset = 54;
119 ofs.write((char*)&bmpSize,4);
120 ofs.write((char*)&dummy,4);
121 ofs.write((char*)&offset,4);
122 DibHeader header;
123 header.header_sz = 40;
124 header.width = img.width();
125 header.height = img.height();
126 header.nplanes = 1;
127 header.bitspp = 24;
128 header.compress_type = 0;
129 header.bmp_bytesz = 3 * img.width() * img.height();
130 header.hres = 0;
131 header.vres = 0;
132 header.ncolors = 0;
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)
137 {
138 for (uint col=0; col<img.width(); ++col)
139 {
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);
143 }
144 }
145 return true;
146}
147
148}