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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* This file is part of the libpict project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "PictBitmap.h"
#include "libpict_utils.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define DUMP_BITMAP 0
#if DUMP_BITMAP
static unsigned bitmapId = 0;
#include <sstream>
#endif
namespace
{
void writeU16(unsigned char *buffer, unsigned &position, const int value)
{
buffer[position++] = (unsigned char)(value & 0xFF);
buffer[position++] = (unsigned char)((value >> 8) & 0xFF);
}
void writeU32(unsigned char *buffer, unsigned &position, const int value)
{
buffer[position++] = (unsigned char)(value & 0xFF);
buffer[position++] = (unsigned char)((value >> 8) & 0xFF);
buffer[position++] = (unsigned char)((value >> 16) & 0xFF);
buffer[position++] = (unsigned char)((value >> 24) & 0xFF);
}
void writeU8(unsigned char *buffer, unsigned &position, const int value)
{
buffer[position++] = (unsigned char)(value & 0xFF);
}
}
class libpict::PictBitmap::Private
{
// disable copying
Private(const Private &other);
Private &operator=(const Private &other);
public:
int width;
int height;
int vRes;
int hRes;
bool vFlip;
bool hFlip;
PictColor *pixels;
librevenge::RVNGBinaryData dib;
Private(int w, int h): width(w), height(h), vRes(72), hRes(72), vFlip(false), hFlip(false), pixels(0), dib() {}
};
libpict::PictBitmap::PictBitmap(int w, int h, int verticalResolution, int horizontalResolution, bool verticalFlip, bool horizontalFlip) :
d(new Private(w, h))
{
d->vRes = verticalResolution;
d->hRes = horizontalResolution;
d->vFlip = verticalFlip;
d->hFlip = horizontalFlip;
d->pixels = new PictColor[w*h];
}
libpict::PictBitmap::~PictBitmap()
{
if (d)
{
if (d->pixels)
delete [] d->pixels;
delete d;
}
}
libpict::PictBitmap::PictBitmap(const PictBitmap &bitmap): d(new Private(0,0))
{
copyFrom(bitmap);
}
libpict::PictBitmap &libpict::PictBitmap::operator=(const PictBitmap &bitmap)
{
copyFrom(bitmap);
return *this;
}
void libpict::PictBitmap::copyFrom(const PictBitmap &bitmap)
{
d->width = bitmap.d->width;
d->height = bitmap.d->height;
delete [] d->pixels;
d->pixels = new PictColor[d->width*d->height];
for (int i=0; i < d->width*d->height; i++)
d->pixels[i] = bitmap.d->pixels[i];
}
int libpict::PictBitmap::width() const
{
return d->width;
}
int libpict::PictBitmap::height() const
{
return d->height;
}
int libpict::PictBitmap::vres() const
{
return d->vRes;
}
int libpict::PictBitmap::hres() const
{
return d->hRes;
}
void libpict::PictBitmap::setPixel(int x, int y, const libpict::PictColor &color)
{
if ((x < 0) || (y <0) || (x >= d->width) || (y >= d->height))
return;
d->pixels[y*d->width + x] = color;
}
const librevenge::RVNGBinaryData &libpict::PictBitmap::getDIB() const
{
if (d->dib.size() || d->height <= 0 || d->width <= 0)
return d->dib;
#ifndef OUTPUT_DUMMY_BITMAPS
unsigned tmpPixelSize = (unsigned)(d->height * d->width);
if (tmpPixelSize < (unsigned)d->height) // overflow
return d->dib;
#else
unsigned tmpPixelSize = 1;
#endif
unsigned tmpBufferPosition = 0;
unsigned tmpDIBImageSize = tmpPixelSize * 4;
if (tmpPixelSize > tmpDIBImageSize) // overflow !!!
return d->dib;
unsigned tmpDIBOffsetBits = 14 + 40;
unsigned tmpDIBFileSize = tmpDIBOffsetBits + tmpDIBImageSize;
if (tmpDIBImageSize > tmpDIBFileSize) // overflow !!!
return d->dib;
unsigned char *tmpDIBBuffer = new unsigned char[tmpDIBFileSize];
// Create DIB file header
writeU16(tmpDIBBuffer, tmpBufferPosition, 0x4D42); // Type
writeU32(tmpDIBBuffer, tmpBufferPosition, tmpDIBFileSize); // Size
writeU16(tmpDIBBuffer, tmpBufferPosition, 0); // Reserved1
writeU16(tmpDIBBuffer, tmpBufferPosition, 0); // Reserved2
writeU32(tmpDIBBuffer, tmpBufferPosition, tmpDIBOffsetBits); // OffsetBits
PICT_DEBUG_MSG(("PictBitmap: DIB file header end = %i\n", tmpBufferPosition - 1));
// Create DIB Info header
writeU32(tmpDIBBuffer, tmpBufferPosition, 40); // Size
#ifndef OUTPUT_DUMMY_BITMAPS
writeU32(tmpDIBBuffer, tmpBufferPosition, width()); // Width
writeU32(tmpDIBBuffer, tmpBufferPosition, height()); // Height
#else
writeU32(tmpDIBBuffer, tmpBufferPosition, 1); // Width
writeU32(tmpDIBBuffer, tmpBufferPosition, 1); // Height
#endif
writeU16(tmpDIBBuffer, tmpBufferPosition, 1); // Planes
writeU16(tmpDIBBuffer, tmpBufferPosition, 32); // BitCount
writeU32(tmpDIBBuffer, tmpBufferPosition, 0); // Compression
writeU32(tmpDIBBuffer, tmpBufferPosition, tmpDIBImageSize); // SizeImage
writeU32(tmpDIBBuffer, tmpBufferPosition, (int)(hres()*100.0/2.54)); // XPelsPerMeter
writeU32(tmpDIBBuffer, tmpBufferPosition, (int)(vres()*100.0/2.54)); // YPelsPerMeter
writeU32(tmpDIBBuffer, tmpBufferPosition, 0); // ColorsUsed
writeU32(tmpDIBBuffer, tmpBufferPosition, 0); // ColorsImportant
PICT_DEBUG_MSG(("PictBitmap: DIB info header end = %i\n", tmpBufferPosition - 1));
// Write DIB Image data
#ifndef OUTPUT_DUMMY_BITMAPS
int i = 0;
int j = 0;
if (d->vFlip)
for (i = 0; i < d->height && tmpBufferPosition < tmpDIBFileSize; i++)
{
if (d->hFlip)
for (j = d->width - 1; j >= 0 && tmpBufferPosition < tmpDIBFileSize; j--)
{
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].blue);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].green);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].red);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].alpha);
}
else
for (j = 0; j < d->width && tmpBufferPosition < tmpDIBFileSize; j++)
{
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].blue);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].green);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].red);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].alpha);
}
}
else
for (i = d->height - 1; i >= 0 && tmpBufferPosition < tmpDIBFileSize; i--)
{
if (d->hFlip)
for (j = d->width - 1; j >= 0 && tmpBufferPosition < tmpDIBFileSize; j--)
{
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].blue);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].green);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].red);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].alpha);
}
else
for (j = 0; j < d->width && tmpBufferPosition < tmpDIBFileSize; j++)
{
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].blue);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].green);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].red);
writeU8(tmpDIBBuffer, tmpBufferPosition, d->pixels[i*d->width + j].alpha);
}
}
PICT_DEBUG_MSG(("PictBitmap: DIB file size = %i\n", tmpBufferPosition - 1));
#else
writeU8(tmpDIBBuffer, tmpBufferPosition, 0x7F);
writeU8(tmpDIBBuffer, tmpBufferPosition, 0x7F);
writeU8(tmpDIBBuffer, tmpBufferPosition, 0x7F);
writeU8(tmpDIBBuffer, tmpBufferPosition, 0x7F);
#endif
d->dib.append(tmpDIBBuffer, tmpDIBFileSize);
// temporary for debug - dump the binary bmp (need to have write access in the current directory
#if DUMP_BITMAP
std::ostringstream filename;
filename << "binarydump" << bitmapId++ << ".bmp";
FILE *f = fopen(filename.str().c_str(), "wb");
if (f)
{
for (unsigned k = 0; k < tmpDIBFileSize; k++)
fprintf(f, "%c",tmpDIBBuffer[k]);
fclose(f);
}
#endif
// Cleanup things before returning
delete [] tmpDIBBuffer;
return d->dib;
}
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|