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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* libcdr
* Version: MPL 1.1 / GPLv2+ / LGPLv2+
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License or as specified alternatively below. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Major Contributor(s):
* Copyright (C) 2012 Fridrich Strba <fridrich.strba@bluewin.ch>
*
*
* All Rights Reserved.
*
* For minor contributions see the git repository.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPLv2+"), or
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
* in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
* instead of those above.
*/
#include <math.h>
#include "CDRStylesCollector.h"
#include "CDRInternalStream.h"
#include "libcdr_utils.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef DUMP_IMAGE
#define DUMP_IMAGE 0
#endif
libcdr::CDRStylesCollector::CDRStylesCollector(libcdr::CDRParserState &ps) :
m_ps(ps), m_page(8.5, 11.0, -4.25, -5.5)
{
}
libcdr::CDRStylesCollector::~CDRStylesCollector()
{
}
void libcdr::CDRStylesCollector::collectFild(unsigned id, unsigned short fillType, const libcdr::CDRColor &color1, const libcdr::CDRColor &color2,
const libcdr::CDRGradient &gradient, const libcdr::CDRImageFill &imageFill)
{
m_ps.m_fillStyles[id] = CDRFillStyle(fillType, color1, color2, gradient, imageFill);
}
void libcdr::CDRStylesCollector::collectOutl(unsigned id, unsigned short lineType, unsigned short capsType, unsigned short joinType, double lineWidth,
double stretch, double angle, const CDRColor &color, const std::vector<unsigned short> &dashArray,
unsigned startMarkerId, unsigned endMarkerId)
{
m_ps.m_lineStyles[id] = CDRLineStyle(lineType, capsType, joinType, lineWidth, stretch, angle, color, dashArray, startMarkerId, endMarkerId);
}
void libcdr::CDRStylesCollector::collectBmp(unsigned imageId, unsigned colorModel, unsigned width, unsigned height, unsigned bpp, const std::vector<unsigned> &palette, const std::vector<unsigned char> &bitmap)
{
libcdr::CDRInternalStream stream(bitmap);
WPXBinaryData image;
unsigned tmpPixelSize = (unsigned)(height * width);
if (tmpPixelSize < (unsigned)height) // overflow
return;
unsigned tmpDIBImageSize = tmpPixelSize * 4;
if (tmpPixelSize > tmpDIBImageSize) // overflow !!!
return;
unsigned tmpDIBOffsetBits = 14 + 40;
unsigned tmpDIBFileSize = tmpDIBOffsetBits + tmpDIBImageSize;
if (tmpDIBImageSize > tmpDIBFileSize) // overflow !!!
return;
// Create DIB file header
writeU16(image, 0x4D42); // Type
writeU32(image, tmpDIBFileSize); // Size
writeU16(image, 0); // Reserved1
writeU16(image, 0); // Reserved2
writeU32(image, tmpDIBOffsetBits); // OffsetBits
// Create DIB Info header
writeU32(image, 40); // Size
writeU32(image, width); // Width
writeU32(image, height); // Height
writeU16(image, 1); // Planes
writeU16(image, 32); // BitCount
writeU32(image, 0); // Compression
writeU32(image, tmpDIBImageSize); // SizeImage
writeU32(image, 0); // XPelsPerMeter
writeU32(image, 0); // YPelsPerMeter
writeU32(image, 0); // ColorsUsed
writeU32(image, 0); // ColorsImportant
// Cater for eventual padding
unsigned lineWidth = bitmap.size() / height;
bool storeBMP = true;
for (unsigned j = 0; j < height; ++j)
{
unsigned i = 0;
unsigned k = 0;
if (colorModel == 6)
{
while (i <lineWidth && k < width)
{
unsigned l = 0;
unsigned char c = bitmap[j*lineWidth+i];
i++;
while (k < width && l < 8)
{
if (c & 0x80)
writeU32(image, 0xffffff);
else
writeU32(image, 0);
c <<= 1;
l++;
k++;
}
}
}
else if (colorModel == 5)
{
while (i <lineWidth && i < width)
{
unsigned char c = bitmap[j*lineWidth+i];
i++;
writeU32(image, m_ps.getBMPColor(libcdr::CDRColor(colorModel, c)));
}
}
else if (!palette.empty())
{
while (i < lineWidth && i < width)
{
unsigned char c = bitmap[j*lineWidth+i];
i++;
writeU32(image, m_ps.getBMPColor(libcdr::CDRColor(colorModel, palette[c])));
}
}
else if (bpp == 24)
{
while (i < lineWidth && k < width)
{
unsigned c = ((unsigned)bitmap[j*lineWidth+i+2] << 16) | ((unsigned)bitmap[j*lineWidth+i+1] << 8) | ((unsigned)bitmap[j*lineWidth+i]);
i += 3;
writeU32(image, m_ps.getBMPColor(libcdr::CDRColor(colorModel, c)));
k++;
}
}
else if (bpp == 32)
{
while (i < lineWidth && k < width)
{
unsigned c = (bitmap[j*lineWidth+i+3] << 24) | (bitmap[j*lineWidth+i+2] << 16) | (bitmap[j*lineWidth+i+1] << 8) | (bitmap[j*lineWidth+i]);
i += 4;
writeU32(image, m_ps.getBMPColor(libcdr::CDRColor(colorModel, c)));
k++;
}
}
else
storeBMP = false;
}
if (storeBMP)
{
#if DUMP_IMAGE
WPXString filename;
filename.sprintf("bitmap%.8x.bmp", imageId);
FILE *f = fopen(filename.cstr(), "wb");
if (f)
{
const unsigned char *tmpBuffer = image.getDataBuffer();
for (unsigned long k = 0; k < image.size(); k++)
fprintf(f, "%c",tmpBuffer[k]);
fclose(f);
}
#endif
m_ps.m_bmps[imageId] = image;
}
}
void libcdr::CDRStylesCollector::collectBmp(unsigned imageId, const std::vector<unsigned char> &bitmap)
{
WPXBinaryData image(&bitmap[0], bitmap.size());
#if DUMP_IMAGE
WPXString filename;
filename.sprintf("bitmap%.8x.bmp", imageId);
FILE *f = fopen(filename.cstr(), "wb");
if (f)
{
const unsigned char *tmpBuffer = image.getDataBuffer();
for (unsigned long k = 0; k < image.size(); k++)
fprintf(f, "%c",tmpBuffer[k]);
fclose(f);
}
#endif
m_ps.m_bmps[imageId] = image;
}
void libcdr::CDRStylesCollector::collectPageSize(double width, double height, double offsetX, double offsetY)
{
if (m_ps.m_pages.empty())
m_page = CDRPage(width, height, offsetX, offsetY);
else
m_ps.m_pages.back() = CDRPage(width, height, offsetX, offsetY);
}
void libcdr::CDRStylesCollector::collectPage(unsigned /* level */)
{
m_ps.m_pages.push_back(m_page);
}
void libcdr::CDRStylesCollector::collectBmpf(unsigned patternId, unsigned width, unsigned height, const std::vector<unsigned char> &pattern)
{
m_ps.m_patterns[patternId] = CDRPattern(width, height, pattern);
}
void libcdr::CDRStylesCollector::collectColorProfile(const std::vector<unsigned char> &profile)
{
if (!profile.empty())
m_ps.setColorTransform(profile);
}
void libcdr::CDRStylesCollector::collectPaletteEntry(unsigned colorId, unsigned /* userId */, const libcdr::CDRColor &color)
{
m_ps.m_documentPalette[colorId] = color;
}
void libcdr::CDRStylesCollector::collectFont(unsigned fontId, const WPXString &font)
{
m_ps.m_fonts[fontId] = font;
}
void libcdr::CDRStylesCollector::collectText(unsigned textId, const WPXString &text)
{
m_ps.m_texts[textId] = text;
}
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|