-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathRGBImage.cpp
312 lines (257 loc) · 7.82 KB
/
RGBImage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*This file is part of the FEBio Studio source code and is licensed under the MIT license
listed below.
See Copyright-FEBio-Studio.txt for details.
Copyright (c) 2021 University of Utah, The Trustees of Columbia University in
the City of New York, and others.
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.*/
#include "stdafx.h"
#include "RGBImage.h"
#include <stdio.h>
#ifdef WIN32
#include <memory>
#else
#include <memory.h>
#endif
//////////////////////////////////////////////////////////////////////
// CRGBImage
//////////////////////////////////////////////////////////////////////
CRGBImage::CRGBImage()
{
m_pb = 0;
m_cx = m_cy = 0;
}
CRGBImage::~CRGBImage()
{
delete[] m_pb;
}
CRGBImage::CRGBImage(int nx, int ny)
{
m_pb = 0;
m_cx = m_cy = 0;
Create(nx, ny);
}
CRGBImage::CRGBImage(const CRGBImage& im)
{
m_pb = 0;
int nx = im.m_cx;
int ny = im.m_cy;
Create(nx, ny);
memcpy(m_pb, im.m_pb, 3 * nx*ny);
}
void CRGBImage::Create(int nx, int ny)
{
if (m_pb) delete[] m_pb;
m_cx = nx;
m_cy = ny;
m_pb = new uint8_t[nx*ny][3];
}
CRGBImage& CRGBImage::operator =(const CRGBImage& im)
{
int nx = im.m_cx;
int ny = im.m_cy;
Create(nx, ny);
memcpy(m_pb, im.m_pb, 3 * nx*ny);
return (*this);
}
void CRGBImage::SwapRB()
{
uint8_t* pb = *m_pb;
uint8_t t;
for (int i = 0; i<m_cx*m_cy; i++, pb += 3)
{
t = pb[0];
pb[0] = pb[2];
pb[2] = t;
}
}
void CRGBImage::FlipY()
{
uint8_t t;
for (int i = 0; i<m_cy / 2; ++i)
{
int i0 = i;
int i1 = m_cy - 1 - i;
if (i0 != i1)
{
uint8_t(*psrc)[3] = m_pb + i0*m_cx;
uint8_t(*pdst)[3] = m_pb + i1*m_cx;
for (int j = 0; j<m_cx; ++j)
{
t = psrc[j][0]; psrc[j][0] = pdst[j][0]; pdst[j][0] = t;
t = psrc[j][1]; psrc[j][1] = pdst[j][1]; pdst[j][1] = t;
t = psrc[j][2]; psrc[j][2] = pdst[j][2]; pdst[j][2] = t;
}
}
}
}
bool CRGBImage::SaveBMP(const char* szfile)
{
SwapRB();
// create the file
FILE* fp = fopen(szfile, "wb");
if (fp == 0) return false;
// save the fileheader
uint16_t bfType = 0x4d42; // file type, must be "BM"
uint32_t bfSize = 54 + 3 * m_cx*m_cy; // size in bytes of file
uint16_t bfReserved1 = 0; // reserved, must be zero
uint16_t bfReserved2 = 0; // reserved, must be zero
uint32_t bfOffBits = 54; // offset in bytes from beginning of BMPHEADER to bitmap bits.
// info header
uint32_t biSize = 40; // size of structure (should be sizeof(BMPINFOHEADER)=40)
uint32_t biWidth = m_cx; // width of bitmap in pixels
uint32_t biHeight = m_cy; // height of bitmap in pixels
uint16_t biPlanes = 1; // number of planes (must be 1)
uint16_t biBitCount = 24; // number of bits per pixel (here 24)
uint32_t biCompression = 0; // type of compression (here 0, no compression)
uint32_t biSizeImage = 0; // size of image in bytes (here can be 0)
uint32_t biXPelsPerMeter = 0;// horizontal resolution (here not used)
uint32_t biYPelsPerMeter = 0;// vertical resolution (here not used)
uint32_t biClrUsed = 0; // number of color indices (here 0)
uint32_t biClrImportant = 0; // number of colors required to display image (here 0)
fwrite(&bfType, sizeof(bfType), 1, fp);
fwrite(&bfSize, sizeof(bfSize), 1, fp);
fwrite(&bfReserved1, sizeof(bfReserved1), 1, fp);
fwrite(&bfReserved2, sizeof(bfReserved2), 1, fp);
fwrite(&bfOffBits, sizeof(bfOffBits), 1, fp);
fwrite(&biSize, sizeof(biSize), 1, fp);
fwrite(&biWidth, sizeof(biWidth), 1, fp);
fwrite(&biHeight, sizeof(biHeight), 1, fp);
fwrite(&biPlanes, sizeof(biPlanes), 1, fp);
fwrite(&biBitCount, sizeof(biBitCount), 1, fp);
fwrite(&biCompression, sizeof(biCompression), 1, fp);
fwrite(&biSizeImage, sizeof(biSizeImage), 1, fp);
fwrite(&biXPelsPerMeter, sizeof(biXPelsPerMeter), 1, fp);
fwrite(&biYPelsPerMeter, sizeof(biYPelsPerMeter), 1, fp);
fwrite(&biClrUsed, sizeof(biClrUsed), 1, fp);
fwrite(&biClrImportant, sizeof(biClrImportant), 1, fp);
// save the pixels
// we need to make sure that the image is 32-bit aligned
int wb = 4 * ((3 * m_cx - 1) / 4 + 1);
uint8_t* pb = new uint8_t[wb];
for (int i = 0; i<m_cy; i++)
{
memcpy(pb, m_pb + i*m_cx, 3 * m_cx);
fwrite(pb, sizeof(uint8_t), wb, fp);
}
delete[] pb;
fclose(fp);
SwapRB();
return true;
}
union FIELD_VALUE {
uint16_t sval;
uint32_t lval;
uint8_t bval[4];
};
bool CRGBImage::SaveTIF(const char* szfile)
{
// determine the endianess
uint16_t wrd = 0x4D49;
uint8_t bt = ((uint8_t*)&wrd)[0];
uint16_t order = ((uint16_t)bt << 8) | bt;
// create the file
FILE* fp = fopen(szfile, "wb");
if (fp == 0) return false;
// write the order
fwrite(&order, sizeof(uint16_t), 1, fp);
// write the type
uint16_t type = 0x2A;
fwrite(&type, sizeof(uint16_t), 1, fp);
// write the offset to the IFD
uint32_t offset = 8;
fwrite(&offset, sizeof(uint32_t), 1, fp);
// write the IFD size
uint16_t ifd_size = 7;
fwrite(&ifd_size, sizeof(uint16_t), 1, fp);
// write the fields
uint16_t nTag;
uint16_t nType;
uint32_t nLength;
FIELD_VALUE val;
// FIELD : SubfileType
nTag = 255;
nType = 3; // short
nLength = 1;
val.sval = 1;
fwrite(&nTag, sizeof(nTag), 1, fp);
fwrite(&nType, sizeof(nType), 1, fp);
fwrite(&nLength, sizeof(nLength), 1, fp);
fwrite(&val.lval, sizeof(val.lval), 1, fp);
// FIELD : ImageWidth
nTag = 256;
nType = 3; // short
nLength = 1;
val.sval = m_cx;
fwrite(&nTag, sizeof(nTag), 1, fp);
fwrite(&nType, sizeof(nType), 1, fp);
fwrite(&nLength, sizeof(nLength), 1, fp);
fwrite(&val.lval, sizeof(val.lval), 1, fp);
// FIELD : ImageHeight
nTag = 257;
nType = 3; // short
nLength = 1;
val.sval = m_cy;
fwrite(&nTag, sizeof(nTag), 1, fp);
fwrite(&nType, sizeof(nType), 1, fp);
fwrite(&nLength, sizeof(nLength), 1, fp);
fwrite(&val.lval, sizeof(val.lval), 1, fp);
// FIELD : BitsPerSample
nTag = 258;
nType = 3; // short
nLength = 1;
val.sval = 8;
fwrite(&nTag, sizeof(nTag), 1, fp);
fwrite(&nType, sizeof(nType), 1, fp);
fwrite(&nLength, sizeof(nLength), 1, fp);
fwrite(&val.lval, sizeof(val.lval), 1, fp);
// FIELD : PhotometricInterpretation
nTag = 262;
nType = 3; // short
nLength = 1;
val.sval = 2;
fwrite(&nTag, sizeof(nTag), 1, fp);
fwrite(&nType, sizeof(nType), 1, fp);
fwrite(&nLength, sizeof(nLength), 1, fp);
fwrite(&val.lval, sizeof(val.lval), 1, fp);
// FIELD : StripOffset
nTag = 273;
nType = 4; // short
nLength = 1;
val.lval = 8 + 2 + ifd_size * 12 + 4;
fwrite(&nTag, sizeof(nTag), 1, fp);
fwrite(&nType, sizeof(nType), 1, fp);
fwrite(&nLength, sizeof(nLength), 1, fp);
fwrite(&val.lval, sizeof(val.lval), 1, fp);
// FIELD : SamplesPerPixel
nTag = 277;
nType = 3; // short
nLength = 1;
val.sval = 3;
fwrite(&nTag, sizeof(nTag), 1, fp);
fwrite(&nType, sizeof(nType), 1, fp);
fwrite(&nLength, sizeof(nLength), 1, fp);
fwrite(&val.lval, sizeof(val.lval), 1, fp);
// next IFD
uint32_t next = 0;
fwrite(&next, sizeof(next), 1, fp);
// write the image data
for (int y = m_cy - 1; y >= 0; y--)
fwrite(m_pb + y*m_cx, sizeof(uint8_t) * 3, m_cx, fp);
// and we're done !
fclose(fp);
return true;
}