Main Page
Namespaces
Classes
Files
File List
File Members
MWAWPictBitmap.hxx
Go to the documentation of this file.
1
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3
/* libmwaw
4
* Version: MPL 2.0 / LGPLv2+
5
*
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 2.0 (the "License"); you may not use this file except in compliance with
8
* the License or as specified alternatively below. You may obtain a copy of
9
* the License at http://www.mozilla.org/MPL/
10
*
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
14
* License.
15
*
16
* Major Contributor(s):
17
* Copyright (C) 2002 William Lachance (wrlach@gmail.com)
18
* Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
19
* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
20
* Copyright (C) 2006, 2007 Andrew Ziem
21
* Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
22
*
23
*
24
* All Rights Reserved.
25
*
26
* For minor contributions see the git repository.
27
*
28
* Alternatively, the contents of this file may be used under the terms of
29
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30
* in which case the provisions of the LGPLv2+ are applicable
31
* instead of those above.
32
*/
33
34
/* This header contains code specific to some bitmap
35
*/
36
37
#ifndef MWAW_PICT_BITMAP
38
# define MWAW_PICT_BITMAP
39
40
#include <assert.h>
41
42
#include <vector>
43
44
#include "
libmwaw_internal.hxx
"
45
#include "
MWAWPict.hxx
"
46
47
class
WPXBinaryData;
48
50
//
51
// Some container
52
//
54
56
template
<
class
T>
class
MWAWPictBitmapContainer
57
{
58
public
:
60
MWAWPictBitmapContainer
(
Vec2i
const
&sz) :
m_size
(sz),
m_data
(0L) {
61
if
(
m_size
[0]*
m_size
[1] != 0)
m_data
=
new
T[size_t(
m_size
[0]*
m_size
[1])];
62
}
64
virtual
~MWAWPictBitmapContainer
() {
65
if
(
m_data
)
delete
[]
m_data
;
66
}
67
69
bool
ok
()
const
{
70
return
(
m_data
!= 0L);
71
}
72
74
int
cmp
(
MWAWPictBitmapContainer<T>
const
&orig)
const
{
75
int
diff =
m_size
.
cmpY
(orig.
m_size
);
76
if
(diff)
return
diff;
77
if
(!
m_data
)
return
orig.
m_data
? 1 : 0;
78
if
(!orig.
m_data
)
return
-1;
79
for
(
int
i=0; i <
m_size
[0]*
m_size
[1]; i++) {
80
if
(
m_data
[i] < orig.
m_data
[i])
return
-1;
81
if
(
m_data
[i] > orig.
m_data
[i])
return
1;
82
}
83
return
0;
84
}
86
Vec2i
const
&
size
()
const
{
87
return
m_size
;
88
}
90
int
numRows
()
const
{
91
return
m_size
[0];
92
}
94
int
numColumns
()
const
{
95
return
m_size
[1];
96
}
97
99
T
const
&
get
(
int
i,
int
j)
const
{
100
assert(
m_data
!= 0L && i>=0 && i <
m_size
[0] && j>=0 && j <
m_size
[1]);
101
return
m_data
[i+
m_size
[0]*j];
102
}
104
T
const
*
getRow
(
int
j)
const
{
105
assert(
m_data
!= 0L && j>=0 && j <
m_size
[1]);
106
return
m_data
+
m_size
[0]*j;
107
}
108
110
void
set
(
int
i,
int
j, T
const
&v) {
111
assert(
m_data
!= 0L && i>=0 && i <
m_size
[0] && j>=0 && j <
m_size
[1]);
112
m_data
[i+j*
m_size
[0]] = v;
113
}
114
116
template
<
class
U>
117
void
setRow
(
int
j, U
const
*val) {
118
assert(
m_data
!= 0L && j>=0 && j <
m_size
[1]);
119
for
(
int
i = 0, ind=j*
m_size
[0]; i <
m_size
[0]; i++, ind++)
m_data
[ind] = T(val[i]);
120
}
121
123
template
<
class
U>
124
void
setColumn
(
int
i, U
const
*val) {
125
assert(
m_data
!= 0L && i>=0 && i <
m_size
[0]);
126
for
(
int
j = 0, ind=i; j <
m_size
[1]; j++, ind+=m_size[0])
m_data
[ind] = T(val[i]);
127
}
128
129
private
:
130
MWAWPictBitmapContainer
(
MWAWPictBitmapContainer
const
&orig);
131
MWAWPictBitmapContainer
&
operator=
(
MWAWPictBitmapContainer
const
&orig);
132
protected
:
134
Vec2i
m_size
;
136
T *
m_data
;
137
};
138
140
class
MWAWPictBitmapContainerBool
:
public
MWAWPictBitmapContainer
<bool>
141
{
142
public
:
144
MWAWPictBitmapContainerBool
(
Vec2i
const
&sz) :
MWAWPictBitmapContainer
<bool>(sz) {}
145
147
int
cmp
(
MWAWPictBitmapContainerBool
const
&orig)
const
{
148
int
diff =
m_size
.
cmpY
(orig.
m_size
);
149
if
(diff)
return
diff;
150
if
(!
m_data
)
return
orig.
m_data
? 1 : 0;
151
if
(!orig.
m_data
)
return
-1;
152
for
(
int
i=0; i <
m_size
[0]*
m_size
[1]; i++) {
153
if
(
m_data
[i] == orig.
m_data
[i])
continue
;
154
return
m_data
[i] ? 1 : -1;
155
}
156
return
0;
157
}
158
160
void
setRowPacked
(
int
j,
unsigned
char
const
*val) {
161
assert(
m_data
!= 0L && j>=0 && j <
m_size
[1]);
162
for
(
int
i = 0, ind = j*
m_size
[0]; i <
m_size
[0]; ) {
163
unsigned
char
v = *(val++);
164
unsigned
char
mask = 0x80;
165
for
(
int
p = 0; p < 8 && i < m_size[0]; i++, p++, ind++) {
166
m_data
[ind] = ((v&mask) != 0);
167
mask = (
unsigned
char) (mask >> 1);
168
}
169
}
170
}
171
};
172
174
class
MWAWPictBitmap
:
public
MWAWPict
175
{
176
public
:
178
enum
SubType
{
BW
,
Indexed
,
Color
};
180
virtual
Type
getType
()
const
{
181
return
MWAWPict::Bitmap
;
182
}
184
virtual
SubType
getSubType
()
const
= 0;
185
187
virtual
bool
getBinary
(WPXBinaryData &res, std::string &s)
const
{
188
if
(!
valid
())
return
false
;
189
190
s =
"image/pict"
;
191
createFileData
(res);
192
return
true
;
193
}
194
196
virtual
bool
valid
()
const
{
197
return
false
;
198
}
199
202
virtual
int
cmp
(
MWAWPict
const
&a)
const
{
203
int
diff =
MWAWPict::cmp
(a);
204
if
(diff)
return
diff;
205
MWAWPictBitmap
const
&aPict =
static_cast<
MWAWPictBitmap
const
&
>
(a);
206
207
// the type
208
diff =
getSubType
() - aPict.
getSubType
();
209
if
(diff)
return
(diff < 0) ? -1 : 1;
210
211
return
0;
212
}
213
214
protected
:
216
virtual
bool
createFileData
(WPXBinaryData &result)
const
= 0;
217
219
MWAWPictBitmap
(
Vec2i
const
&sz) {
220
setBdBox
(
Box2f
(
Vec2f
(0,0), sz));
221
}
222
};
223
225
class
MWAWPictBitmapBW
:
public
MWAWPictBitmap
226
{
227
public
:
229
virtual
SubType
getSubType
()
const
{
230
return
BW
;
231
}
232
235
virtual
int
cmp
(
MWAWPict
const
&a)
const
{
236
int
diff =
MWAWPictBitmap::cmp
(a);
237
if
(diff)
return
diff;
238
MWAWPictBitmapBW
const
&aPict =
static_cast<
MWAWPictBitmapBW
const
&
>
(a);
239
240
return
m_data
.
cmp
(aPict.
m_data
);
241
}
242
244
virtual
bool
valid
()
const
{
245
return
m_data
.
ok
();
246
}
247
249
MWAWPictBitmapBW
(
Vec2i
const
&sz) :
MWAWPictBitmap
(sz),
m_data
(sz) { }
250
252
Vec2i
const
&
size
()
const
{
253
return
m_data
.
size
();
254
}
256
int
numRows
()
const
{
257
return
m_data
.
numRows
();
258
}
260
int
numColumns
()
const
{
261
return
m_data
.
numColumns
();
262
}
264
bool
get
(
int
i,
int
j)
const
{
265
return
m_data
.
get
(i,j);
266
}
268
bool
const
*
getRow
(
int
j)
const
{
269
return
m_data
.
getRow
(j);
270
}
272
void
set
(
int
i,
int
j,
bool
v) {
273
m_data
.
set
(i,j, v);
274
}
276
void
setRow
(
int
j,
bool
const
*val) {
277
m_data
.
setRow
(j, val);
278
}
280
void
setRowPacked
(
int
j,
unsigned
char
const
*val) {
281
m_data
.
setRowPacked
(j, val);
282
}
284
void
setColumn
(
int
i,
bool
const
*val) {
285
m_data
.
setColumn
(i, val);
286
}
287
288
protected
:
290
virtual
bool
createFileData
(WPXBinaryData &result)
const
;
291
293
MWAWPictBitmapContainerBool
m_data
;
294
};
295
297
class
MWAWPictBitmapIndexed
:
public
MWAWPictBitmap
298
{
299
public
:
301
virtual
SubType
getSubType
()
const
{
302
return
Indexed
;
303
}
304
307
virtual
int
cmp
(
MWAWPict
const
&a)
const
{
308
int
diff =
MWAWPictBitmap::cmp
(a);
309
if
(diff)
return
diff;
310
MWAWPictBitmapIndexed
const
&aPict =
static_cast<
MWAWPictBitmapIndexed
const
&
>
(a);
311
312
diff=int(
m_colors
.size())-
int
(aPict.
m_colors
.size());
313
if
(diff)
return
(diff < 0) ? -1 : 1;
314
for
(
size_t
c=0; c <
m_colors
.size(); c++) {
315
if
(
m_colors
[c] < aPict.
m_colors
[c])
316
return
1;
317
if
(
m_colors
[c] > aPict.
m_colors
[c])
318
return
-1;
319
}
320
return
m_data
.
cmp
(aPict.
m_data
);
321
}
322
324
virtual
bool
valid
()
const
{
325
return
m_data
.
ok
();
326
}
327
329
MWAWPictBitmapIndexed
(
Vec2i
const
&sz) :
MWAWPictBitmap
(sz),
m_data
(sz),
m_colors
() { }
330
332
Vec2i
const
&
size
()
const
{
333
return
m_data
.
size
();
334
}
336
int
numRows
()
const
{
337
return
m_data
.
numRows
();
338
}
340
int
numColumns
()
const
{
341
return
m_data
.
numColumns
();
342
}
344
int
get
(
int
i,
int
j)
const
{
345
return
m_data
.
get
(i,j);
346
}
348
int
const
*
getRow
(
int
j)
const
{
349
return
m_data
.
getRow
(j);
350
}
351
353
void
set
(
int
i,
int
j,
int
v) {
354
m_data
.
set
(i,j, v);
355
}
357
template
<
class
U>
void
setRow
(
int
j, U
const
*val) {
358
m_data
.
setRow
(j, val);
359
}
361
template
<
class
U>
void
setColumn
(
int
i, U
const
*val) {
362
m_data
.
setColumn
(i, val);
363
}
364
366
std::vector<MWAWColor>
const
&
getColors
()
const
{
367
return
m_colors
;
368
}
370
void
setColors
(std::vector<MWAWColor>
const
&cols) {
371
m_colors
= cols;
372
}
373
374
protected
:
376
virtual
bool
createFileData
(WPXBinaryData &result)
const
;
377
379
MWAWPictBitmapContainer<int>
m_data
;
381
std::vector<MWAWColor>
m_colors
;
382
};
383
385
class
MWAWPictBitmapColor
:
public
MWAWPictBitmap
386
{
387
public
:
389
virtual
SubType
getSubType
()
const
{
390
return
Indexed
;
391
}
392
395
virtual
int
cmp
(
MWAWPict
const
&a)
const
{
396
int
diff =
MWAWPictBitmap::cmp
(a);
397
if
(diff)
return
diff;
398
MWAWPictBitmapColor
const
&aPict =
static_cast<
MWAWPictBitmapColor
const
&
>
(a);
399
400
return
m_data
.
cmp
(aPict.
m_data
);
401
}
402
404
virtual
bool
valid
()
const
{
405
return
m_data
.
ok
();
406
}
407
409
MWAWPictBitmapColor
(
Vec2i
const
&sz) :
MWAWPictBitmap
(sz),
m_data
(sz) { }
410
412
Vec2i
const
&
size
()
const
{
413
return
m_data
.
size
();
414
}
416
int
numRows
()
const
{
417
return
m_data
.
numRows
();
418
}
420
int
numColumns
()
const
{
421
return
m_data
.
numColumns
();
422
}
424
MWAWColor
get
(
int
i,
int
j)
const
{
425
return
m_data
.
get
(i,j);
426
}
428
MWAWColor
const
*
getRow
(
int
j)
const
{
429
return
m_data
.
getRow
(j);
430
}
431
433
void
set
(
int
i,
int
j,
MWAWColor
const
&v) {
434
m_data
.
set
(i,j, v);
435
}
437
void
setRow
(
int
j,
MWAWColor
const
*val) {
438
m_data
.
setRow
(j, val);
439
}
441
void
setColumn
(
int
i,
MWAWColor
const
*val) {
442
m_data
.
setColumn
(i, val);
443
}
444
445
protected
:
447
virtual
bool
createFileData
(WPXBinaryData &result)
const
;
448
450
MWAWPictBitmapContainer<MWAWColor>
m_data
;
451
};
452
#endif
453
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:
Generated on Mon Jul 1 2013 23:57:59 for libmwaw by
doxygen
1.8.4