xref: /aoo41x/main/vcl/source/gdi/bitmap3.cxx (revision 9f62ea84)
1*9f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9f62ea84SAndrew Rist  * distributed with this work for additional information
6*9f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
9*9f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9f62ea84SAndrew Rist  *
11*9f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9f62ea84SAndrew Rist  *
13*9f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9f62ea84SAndrew Rist  * software distributed under the License is distributed on an
15*9f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9f62ea84SAndrew Rist  * specific language governing permissions and limitations
18*9f62ea84SAndrew Rist  * under the License.
19*9f62ea84SAndrew Rist  *
20*9f62ea84SAndrew Rist  *************************************************************/
21*9f62ea84SAndrew Rist 
22*9f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <stdlib.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <vcl/bmpacc.hxx>
30cdf0e10cSrcweir #include <vcl/octree.hxx>
31cdf0e10cSrcweir #include <vcl/bitmapex.hxx>
32cdf0e10cSrcweir #include <vcl/bitmap.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <impoct.hxx>
35cdf0e10cSrcweir #include <impvect.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir // -----------
38cdf0e10cSrcweir // - Defines -
39cdf0e10cSrcweir // -----------
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #define RGB15( _def_cR, _def_cG, _def_cB )	(((sal_uLong)(_def_cR)<<10UL)|((sal_uLong)(_def_cG)<<5UL)|(sal_uLong)(_def_cB))
42cdf0e10cSrcweir #define GAMMA( _def_cVal, _def_InvGamma )	((sal_uInt8)MinMax(FRound(pow( _def_cVal/255.0,_def_InvGamma)*255.0),0L,255L))
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #define CALC_ERRORS																\
45cdf0e10cSrcweir 						nTemp   = p1T[nX++] >> 12;								\
46cdf0e10cSrcweir 						nBErr = MinMax( nTemp, 0, 255 );						\
47cdf0e10cSrcweir 						nBErr = nBErr - FloydIndexMap[ nBC = FloydMap[nBErr] ];	\
48cdf0e10cSrcweir 						nTemp   = p1T[nX++] >> 12;								\
49cdf0e10cSrcweir 						nGErr = MinMax( nTemp, 0, 255 );						\
50cdf0e10cSrcweir 						nGErr = nGErr - FloydIndexMap[ nGC = FloydMap[nGErr] ];	\
51cdf0e10cSrcweir 						nTemp   = p1T[nX] >> 12;								\
52cdf0e10cSrcweir 						nRErr = MinMax( nTemp, 0, 255 );						\
53cdf0e10cSrcweir 						nRErr = nRErr - FloydIndexMap[ nRC = FloydMap[nRErr] ];
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #define CALC_TABLES3										\
56cdf0e10cSrcweir 						p2T[nX++] += FloydError3[nBErr];	\
57cdf0e10cSrcweir 						p2T[nX++] += FloydError3[nGErr];	\
58cdf0e10cSrcweir 						p2T[nX++] += FloydError3[nRErr];
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #define CALC_TABLES5										\
61cdf0e10cSrcweir 						p2T[nX++] += FloydError5[nBErr];	\
62cdf0e10cSrcweir 						p2T[nX++] += FloydError5[nGErr];	\
63cdf0e10cSrcweir 						p2T[nX++] += FloydError5[nRErr];
64cdf0e10cSrcweir 
65cdf0e10cSrcweir #define CALC_TABLES7										\
66cdf0e10cSrcweir 						p1T[++nX] += FloydError7[nBErr];	\
67cdf0e10cSrcweir 						p2T[nX++] += FloydError1[nBErr];	\
68cdf0e10cSrcweir 						p1T[nX] += FloydError7[nGErr];		\
69cdf0e10cSrcweir 						p2T[nX++] += FloydError1[nGErr];	\
70cdf0e10cSrcweir 						p1T[nX] += FloydError7[nRErr];		\
71cdf0e10cSrcweir 						p2T[nX] += FloydError1[nRErr];
72cdf0e10cSrcweir 
73cdf0e10cSrcweir // -----------
74cdf0e10cSrcweir // - Statics -
75cdf0e10cSrcweir // -----------
76cdf0e10cSrcweir 
77cdf0e10cSrcweir sal_uLong nVCLRLut[ 6 ] = { 16, 17, 18, 19, 20, 21 };
78cdf0e10cSrcweir sal_uLong nVCLGLut[ 6 ] = { 0, 6, 12, 18, 24, 30 };
79cdf0e10cSrcweir sal_uLong nVCLBLut[ 6 ] = { 0, 36, 72, 108, 144, 180 };
80cdf0e10cSrcweir 
81cdf0e10cSrcweir // ------------------------------------------------------------------------
82cdf0e10cSrcweir 
83cdf0e10cSrcweir sal_uLong nVCLDitherLut[ 256 ] =
84cdf0e10cSrcweir {
85cdf0e10cSrcweir        0, 49152, 12288, 61440,  3072, 52224, 15360, 64512,   768, 49920, 13056,
86cdf0e10cSrcweir    62208,  3840, 52992, 16128, 65280, 32768, 16384, 45056, 28672, 35840, 19456,
87cdf0e10cSrcweir    48128, 31744, 33536, 17152, 45824, 29440, 36608, 20224, 48896, 32512, 8192,
88cdf0e10cSrcweir    57344,  4096, 53248, 11264, 60416,  7168, 56320,  8960, 58112,  4864, 54016,
89cdf0e10cSrcweir    12032, 61184,  7936, 57088, 40960, 24576, 36864, 20480, 44032, 27648, 39936,
90cdf0e10cSrcweir    23552, 41728, 25344, 37632, 21248, 44800, 28416, 40704, 24320, 2048, 51200,
91cdf0e10cSrcweir    14336, 63488,  1024, 50176, 13312, 62464,  2816, 51968, 15104, 64256,  1792,
92cdf0e10cSrcweir    50944, 14080, 63232, 34816, 18432, 47104, 30720, 33792, 17408, 46080, 29696,
93cdf0e10cSrcweir    35584, 19200, 47872, 31488, 34560, 18176, 46848, 30464, 10240, 59392,  6144,
94cdf0e10cSrcweir    55296,  9216, 58368,  5120, 54272, 11008, 60160,  6912, 56064,  9984, 59136,
95cdf0e10cSrcweir     5888, 55040, 43008, 26624, 38912, 22528, 41984, 25600, 37888, 21504, 43776,
96cdf0e10cSrcweir    27392, 39680, 23296, 42752, 26368, 38656, 22272,   512, 49664, 12800, 61952,
97cdf0e10cSrcweir     3584, 52736, 15872, 65024,   256, 49408, 12544, 61696,  3328, 52480, 15616,
98cdf0e10cSrcweir    64768, 33280, 16896, 45568, 29184, 36352, 19968, 48640, 32256, 33024, 16640,
99cdf0e10cSrcweir    45312, 28928, 36096, 19712, 48384, 32000,  8704, 57856,  4608, 53760, 11776,
100cdf0e10cSrcweir    60928,  7680, 56832,  8448, 57600,  4352, 53504, 11520, 60672,  7424, 56576,
101cdf0e10cSrcweir    41472, 25088, 37376, 20992, 44544, 28160, 40448, 24064, 41216, 24832, 37120,
102cdf0e10cSrcweir    20736, 44288, 27904, 40192, 23808,  2560, 51712, 14848, 64000,  1536, 50688,
103cdf0e10cSrcweir    13824, 62976,  2304, 51456, 14592, 63744,  1280, 50432, 13568, 62720, 35328,
104cdf0e10cSrcweir    18944, 47616, 31232, 34304, 17920, 46592, 30208, 35072, 18688, 47360, 30976,
105cdf0e10cSrcweir    34048, 17664, 46336, 29952, 10752, 59904,  6656, 55808,  9728, 58880,  5632,
106cdf0e10cSrcweir    54784, 10496, 59648,  6400, 55552,  9472, 58624,  5376, 54528, 43520, 27136,
107cdf0e10cSrcweir    39424, 23040, 42496, 26112, 38400, 22016, 43264, 26880, 39168, 22784, 42240,
108cdf0e10cSrcweir    25856, 38144, 21760
109cdf0e10cSrcweir };
110cdf0e10cSrcweir 
111cdf0e10cSrcweir // ------------------------------------------------------------------------
112cdf0e10cSrcweir 
113cdf0e10cSrcweir sal_uLong nVCLLut[ 256 ] =
114cdf0e10cSrcweir {
115cdf0e10cSrcweir          0,  1286,  2572,  3858,  5144,  6430,  7716,  9002,
116cdf0e10cSrcweir      10288, 11574, 12860, 14146, 15432, 16718, 18004, 19290,
117cdf0e10cSrcweir      20576, 21862, 23148, 24434, 25720, 27006, 28292, 29578,
118cdf0e10cSrcweir      30864, 32150, 33436, 34722, 36008, 37294, 38580, 39866,
119cdf0e10cSrcweir      41152, 42438, 43724, 45010, 46296, 47582, 48868, 50154,
120cdf0e10cSrcweir      51440, 52726, 54012, 55298, 56584, 57870, 59156, 60442,
121cdf0e10cSrcweir      61728, 63014, 64300, 65586, 66872, 68158, 69444, 70730,
122cdf0e10cSrcweir      72016, 73302, 74588, 75874, 77160, 78446, 79732, 81018,
123cdf0e10cSrcweir      82304, 83590, 84876, 86162, 87448, 88734, 90020, 91306,
124cdf0e10cSrcweir      92592, 93878, 95164, 96450, 97736, 99022,100308,101594,
125cdf0e10cSrcweir     102880,104166,105452,106738,108024,109310,110596,111882,
126cdf0e10cSrcweir     113168,114454,115740,117026,118312,119598,120884,122170,
127cdf0e10cSrcweir     123456,124742,126028,127314,128600,129886,131172,132458,
128cdf0e10cSrcweir     133744,135030,136316,137602,138888,140174,141460,142746,
129cdf0e10cSrcweir     144032,145318,146604,147890,149176,150462,151748,153034,
130cdf0e10cSrcweir     154320,155606,156892,158178,159464,160750,162036,163322,
131cdf0e10cSrcweir     164608,165894,167180,168466,169752,171038,172324,173610,
132cdf0e10cSrcweir     174896,176182,177468,178754,180040,181326,182612,183898,
133cdf0e10cSrcweir     185184,186470,187756,189042,190328,191614,192900,194186,
134cdf0e10cSrcweir     195472,196758,198044,199330,200616,201902,203188,204474,
135cdf0e10cSrcweir     205760,207046,208332,209618,210904,212190,213476,214762,
136cdf0e10cSrcweir     216048,217334,218620,219906,221192,222478,223764,225050,
137cdf0e10cSrcweir     226336,227622,228908,230194,231480,232766,234052,235338,
138cdf0e10cSrcweir     236624,237910,239196,240482,241768,243054,244340,245626,
139cdf0e10cSrcweir     246912,248198,249484,250770,252056,253342,254628,255914,
140cdf0e10cSrcweir     257200,258486,259772,261058,262344,263630,264916,266202,
141cdf0e10cSrcweir     267488,268774,270060,271346,272632,273918,275204,276490,
142cdf0e10cSrcweir     277776,279062,280348,281634,282920,284206,285492,286778,
143cdf0e10cSrcweir     288064,289350,290636,291922,293208,294494,295780,297066,
144cdf0e10cSrcweir     298352,299638,300924,302210,303496,304782,306068,307354,
145cdf0e10cSrcweir     308640,309926,311212,312498,313784,315070,316356,317642,
146cdf0e10cSrcweir     318928,320214,321500,322786,324072,325358,326644,327930
147cdf0e10cSrcweir };
148cdf0e10cSrcweir 
149cdf0e10cSrcweir // ------------------------------------------------------------------------
150cdf0e10cSrcweir 
151cdf0e10cSrcweir long FloydMap[256] =
152cdf0e10cSrcweir {
153cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
154cdf0e10cSrcweir     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
155cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
156cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
157cdf0e10cSrcweir     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
158cdf0e10cSrcweir     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
159cdf0e10cSrcweir     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
160cdf0e10cSrcweir     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
161cdf0e10cSrcweir     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
162cdf0e10cSrcweir     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
163cdf0e10cSrcweir     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
164cdf0e10cSrcweir     3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
165cdf0e10cSrcweir     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
166cdf0e10cSrcweir     4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
167cdf0e10cSrcweir     4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
168cdf0e10cSrcweir     5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
169cdf0e10cSrcweir };
170cdf0e10cSrcweir 
171cdf0e10cSrcweir // ------------------------------------------------------------------------
172cdf0e10cSrcweir 
173cdf0e10cSrcweir long FloydError1[61] =
174cdf0e10cSrcweir {
175cdf0e10cSrcweir     -7680, -7424, -7168, -6912, -6656, -6400, -6144,
176cdf0e10cSrcweir     -5888, -5632, -5376, -5120, -4864, -4608, -4352,
177cdf0e10cSrcweir     -4096, -3840, -3584, -3328, -3072, -2816, -2560,
178cdf0e10cSrcweir     -2304, -2048, -1792, -1536, -1280, -1024, -768,
179cdf0e10cSrcweir     -512, -256, 0, 256, 512, 768, 1024, 1280, 1536,
180cdf0e10cSrcweir     1792, 2048, 2304, 2560, 2816, 3072, 3328, 3584,
181cdf0e10cSrcweir     3840, 4096, 4352, 4608, 4864, 5120, 5376, 5632,
182cdf0e10cSrcweir     5888, 6144, 6400, 6656, 6912, 7168, 7424, 7680
183cdf0e10cSrcweir };
184cdf0e10cSrcweir 
185cdf0e10cSrcweir // ------------------------------------------------------------------------
186cdf0e10cSrcweir 
187cdf0e10cSrcweir long FloydError3[61] =
188cdf0e10cSrcweir {
189cdf0e10cSrcweir     -23040, -22272, -21504, -20736, -19968, -19200,
190cdf0e10cSrcweir     -18432, -17664, -16896, -16128, -15360, -14592,
191cdf0e10cSrcweir     -13824, -13056, -12288, -11520, -10752, -9984,
192cdf0e10cSrcweir     -9216, -8448, -7680, -6912, -6144, -5376, -4608,
193cdf0e10cSrcweir     -3840, -3072, -2304, -1536, -768, 0, 768, 1536,
194cdf0e10cSrcweir     2304, 3072, 3840, 4608, 5376, 6144, 6912, 7680,
195cdf0e10cSrcweir     8448, 9216, 9984, 10752, 11520, 12288, 13056,
196cdf0e10cSrcweir     13824, 14592, 15360, 16128, 16896, 17664, 18432,
197cdf0e10cSrcweir     19200, 19968, 20736, 21504, 22272, 23040
198cdf0e10cSrcweir };
199cdf0e10cSrcweir 
200cdf0e10cSrcweir // ------------------------------------------------------------------------
201cdf0e10cSrcweir 
202cdf0e10cSrcweir long FloydError5[61] =
203cdf0e10cSrcweir {
204cdf0e10cSrcweir     -38400, -37120, -35840, -34560, -33280, -32000,
205cdf0e10cSrcweir     -30720, -29440, -28160, -26880, -25600, -24320,
206cdf0e10cSrcweir     -23040, -21760, -20480, -19200, -17920, -16640,
207cdf0e10cSrcweir     -15360, -14080, -12800, -11520, -10240, -8960,
208cdf0e10cSrcweir     -7680, -6400, -5120, -3840, -2560, -1280,   0,
209cdf0e10cSrcweir     1280, 2560, 3840, 5120, 6400, 7680, 8960, 10240,
210cdf0e10cSrcweir     11520, 12800, 14080, 15360, 16640, 17920, 19200,
211cdf0e10cSrcweir     20480, 21760, 23040, 24320, 25600, 26880, 28160,
212cdf0e10cSrcweir     29440, 30720, 32000, 33280, 34560, 35840, 37120,
213cdf0e10cSrcweir     38400
214cdf0e10cSrcweir };
215cdf0e10cSrcweir 
216cdf0e10cSrcweir // ------------------------------------------------------------------------
217cdf0e10cSrcweir 
218cdf0e10cSrcweir long FloydError7[61] =
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     -53760, -51968, -50176, -48384, -46592, -44800,
221cdf0e10cSrcweir     -43008, -41216, -39424, -37632, -35840, -34048,
222cdf0e10cSrcweir     -32256, -30464, -28672, -26880, -25088, -23296,
223cdf0e10cSrcweir     -21504, -19712, -17920, -16128, -14336, -12544,
224cdf0e10cSrcweir     -10752, -8960, -7168, -5376, -3584, -1792,  0,
225cdf0e10cSrcweir     1792, 3584, 5376, 7168, 8960, 10752, 12544, 14336,
226cdf0e10cSrcweir     16128, 17920, 19712, 21504, 23296, 25088, 26880,
227cdf0e10cSrcweir     28672, 30464, 32256, 34048, 35840, 37632, 39424,
228cdf0e10cSrcweir     41216, 43008, 44800, 46592, 48384, 50176, 51968,
229cdf0e10cSrcweir     53760
230cdf0e10cSrcweir };
231cdf0e10cSrcweir 
232cdf0e10cSrcweir // ------------------------------------------------------------------------
233cdf0e10cSrcweir 
234cdf0e10cSrcweir long FloydIndexMap[6] =
235cdf0e10cSrcweir {
236cdf0e10cSrcweir     -30,  21, 72, 123, 174, 225
237cdf0e10cSrcweir };
238cdf0e10cSrcweir 
239cdf0e10cSrcweir // --------------------------
240cdf0e10cSrcweir // - ImplCreateDitherMatrix -
241cdf0e10cSrcweir // --------------------------
242cdf0e10cSrcweir 
243cdf0e10cSrcweir void ImplCreateDitherMatrix( sal_uInt8 (*pDitherMatrix)[16][16] )
244cdf0e10cSrcweir {
245cdf0e10cSrcweir 	double			fVal = 3.125;
246cdf0e10cSrcweir 	const double	fVal16 = fVal / 16.;
247cdf0e10cSrcweir 	long			i, j, k, l;
248cdf0e10cSrcweir 	sal_uInt16			pMtx[ 16 ][ 16 ];
249cdf0e10cSrcweir 	sal_uInt16			nMax = 0;
250cdf0e10cSrcweir 	static sal_uInt8 	pMagic[4][4] = { { 0, 14,  3, 13, },
251cdf0e10cSrcweir                                      {11,  5,  8,  6, },
252cdf0e10cSrcweir                                      {12,  2, 15,  1, },
253cdf0e10cSrcweir                                      {7,   9,  4, 10 } };
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 	// MagicSquare aufbauen
256cdf0e10cSrcweir 	for ( i = 0; i < 4; i++ )
257cdf0e10cSrcweir 	   for ( j = 0; j < 4; j++ )
258cdf0e10cSrcweir 		   for ( k = 0; k < 4; k++ )
259cdf0e10cSrcweir 				for ( l = 0; l < 4; l++ )
260cdf0e10cSrcweir 					nMax = Max ( pMtx[ (k<<2) + i][(l<<2 ) + j] =
261cdf0e10cSrcweir 					(sal_uInt16) ( 0.5 + pMagic[i][j]*fVal + pMagic[k][l]*fVal16 ), nMax );
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 	// auf Intervall [0;254] skalieren
264cdf0e10cSrcweir 	for ( i = 0, fVal = 254. / nMax; i < 16; i++ )
265cdf0e10cSrcweir 		for( j = 0; j < 16; j++ )
266cdf0e10cSrcweir 			(*pDitherMatrix)[i][j] = (sal_uInt8) ( fVal * pMtx[i][j] );
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir // ----------
270cdf0e10cSrcweir // - Bitmap -
271cdf0e10cSrcweir // ----------
272cdf0e10cSrcweir 
273cdf0e10cSrcweir sal_Bool Bitmap::Convert( BmpConversion eConversion )
274cdf0e10cSrcweir {
275cdf0e10cSrcweir 	const sal_uInt16	nBitCount = GetBitCount();
276cdf0e10cSrcweir 	sal_Bool			bRet = sal_False;
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 	switch( eConversion )
279cdf0e10cSrcweir 	{
280cdf0e10cSrcweir 		case( BMP_CONVERSION_1BIT_THRESHOLD ):
281cdf0e10cSrcweir 			bRet = ImplMakeMono( 128 );
282cdf0e10cSrcweir 		break;
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 		case( BMP_CONVERSION_1BIT_MATRIX ):
285cdf0e10cSrcweir 			bRet = ImplMakeMonoDither();
286cdf0e10cSrcweir 		break;
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 		case( BMP_CONVERSION_4BIT_GREYS ):
289cdf0e10cSrcweir 			bRet = ImplMakeGreyscales( 16 );
290cdf0e10cSrcweir 		break;
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 		case( BMP_CONVERSION_4BIT_COLORS ):
293cdf0e10cSrcweir 		{
294cdf0e10cSrcweir 			if( nBitCount < 4 )
295cdf0e10cSrcweir 				bRet = ImplConvertUp( 4, NULL );
296cdf0e10cSrcweir 			else if( nBitCount > 4 )
297cdf0e10cSrcweir 				bRet = ImplConvertDown( 4, NULL );
298cdf0e10cSrcweir 			else
299cdf0e10cSrcweir 				bRet = sal_True;
300cdf0e10cSrcweir 		}
301cdf0e10cSrcweir 		break;
302cdf0e10cSrcweir 
303cdf0e10cSrcweir 		case( BMP_CONVERSION_4BIT_TRANS ):
304cdf0e10cSrcweir 		{
305cdf0e10cSrcweir 			Color aTrans( BMP_COL_TRANS );
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 			if( nBitCount < 4 )
308cdf0e10cSrcweir 				bRet = ImplConvertUp( 4, &aTrans );
309cdf0e10cSrcweir 			else
310cdf0e10cSrcweir 				bRet = ImplConvertDown( 4, &aTrans );
311cdf0e10cSrcweir 		}
312cdf0e10cSrcweir 		break;
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 		case( BMP_CONVERSION_8BIT_GREYS ):
315cdf0e10cSrcweir 			bRet = ImplMakeGreyscales( 256 );
316cdf0e10cSrcweir 		break;
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 		case( BMP_CONVERSION_8BIT_COLORS ):
319cdf0e10cSrcweir 		{
320cdf0e10cSrcweir 			if( nBitCount < 8 )
321cdf0e10cSrcweir 				bRet = ImplConvertUp( 8 );
322cdf0e10cSrcweir 			else if( nBitCount > 8 )
323cdf0e10cSrcweir 				bRet = ImplConvertDown( 8 );
324cdf0e10cSrcweir 			else
325cdf0e10cSrcweir 				bRet = sal_True;
326cdf0e10cSrcweir 		}
327cdf0e10cSrcweir 		break;
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 		case( BMP_CONVERSION_8BIT_TRANS ):
330cdf0e10cSrcweir 		{
331cdf0e10cSrcweir 			Color aTrans( BMP_COL_TRANS );
332cdf0e10cSrcweir 
333cdf0e10cSrcweir 			if( nBitCount < 8 )
334cdf0e10cSrcweir 				bRet = ImplConvertUp( 8, &aTrans );
335cdf0e10cSrcweir 			else
336cdf0e10cSrcweir 				bRet = ImplConvertDown( 8, &aTrans );
337cdf0e10cSrcweir 		}
338cdf0e10cSrcweir 		break;
339cdf0e10cSrcweir 
340cdf0e10cSrcweir 		case( BMP_CONVERSION_24BIT ):
341cdf0e10cSrcweir 		{
342cdf0e10cSrcweir 			if( nBitCount < 24 )
343cdf0e10cSrcweir 				bRet = ImplConvertUp( 24, sal_False );
344cdf0e10cSrcweir 			else
345cdf0e10cSrcweir 				bRet = sal_True;
346cdf0e10cSrcweir 		}
347cdf0e10cSrcweir 		break;
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 		case( BMP_CONVERSION_GHOSTED ):
350cdf0e10cSrcweir 			bRet = ImplConvertGhosted();
351cdf0e10cSrcweir 		break;
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 		default:
354cdf0e10cSrcweir 			DBG_ERROR( "Bitmap::Convert(): Unsupported conversion" );
355cdf0e10cSrcweir 		break;
356cdf0e10cSrcweir 	}
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 	return bRet;
359cdf0e10cSrcweir }
360cdf0e10cSrcweir 
361cdf0e10cSrcweir // ------------------------------------------------------------------------
362cdf0e10cSrcweir 
363cdf0e10cSrcweir sal_Bool Bitmap::ImplMakeMono( sal_uInt8 cThreshold )
364cdf0e10cSrcweir {
365cdf0e10cSrcweir 	BitmapReadAccess*	pReadAcc = AcquireReadAccess();
366cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
367cdf0e10cSrcweir 
368cdf0e10cSrcweir 	if( pReadAcc )
369cdf0e10cSrcweir 	{
370cdf0e10cSrcweir 		Bitmap				aNewBmp( GetSizePixel(), 1 );
371cdf0e10cSrcweir 		BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
372cdf0e10cSrcweir 
373cdf0e10cSrcweir 		if( pWriteAcc )
374cdf0e10cSrcweir 		{
375cdf0e10cSrcweir 			const BitmapColor	aBlack( pWriteAcc->GetBestMatchingColor( Color( COL_BLACK ) ) );
376cdf0e10cSrcweir 			const BitmapColor	aWhite( pWriteAcc->GetBestMatchingColor( Color( COL_WHITE ) ) );
377cdf0e10cSrcweir 			const long			nWidth = pWriteAcc->Width();
378cdf0e10cSrcweir 			const long			nHeight = pWriteAcc->Height();
379cdf0e10cSrcweir 
380cdf0e10cSrcweir 			if( pReadAcc->HasPalette() )
381cdf0e10cSrcweir 			{
382cdf0e10cSrcweir 				for( long nY = 0L; nY < nHeight; nY++ )
383cdf0e10cSrcweir 				{
384cdf0e10cSrcweir 					for( long nX = 0L; nX < nWidth; nX++ )
385cdf0e10cSrcweir 					{
386cdf0e10cSrcweir 						if( pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nY, nX ) ).GetLuminance() >=
387cdf0e10cSrcweir 							cThreshold )
388cdf0e10cSrcweir 						{
389cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aWhite );
390cdf0e10cSrcweir 						}
391cdf0e10cSrcweir 						else
392cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aBlack );
393cdf0e10cSrcweir 					}
394cdf0e10cSrcweir 				}
395cdf0e10cSrcweir 			}
396cdf0e10cSrcweir 			else
397cdf0e10cSrcweir 			{
398cdf0e10cSrcweir 				for( long nY = 0L; nY < nHeight; nY++ )
399cdf0e10cSrcweir 				{
400cdf0e10cSrcweir 					for( long nX = 0L; nX < nWidth; nX++ )
401cdf0e10cSrcweir 					{
402cdf0e10cSrcweir 						if( pReadAcc->GetPixel( nY, nX ).GetLuminance() >=
403cdf0e10cSrcweir 							cThreshold )
404cdf0e10cSrcweir 						{
405cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aWhite );
406cdf0e10cSrcweir 						}
407cdf0e10cSrcweir 						else
408cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aBlack );
409cdf0e10cSrcweir 					}
410cdf0e10cSrcweir 				}
411cdf0e10cSrcweir 			}
412cdf0e10cSrcweir 
413cdf0e10cSrcweir 			aNewBmp.ReleaseAccess( pWriteAcc );
414cdf0e10cSrcweir 			bRet = sal_True;
415cdf0e10cSrcweir 		}
416cdf0e10cSrcweir 
417cdf0e10cSrcweir 		ReleaseAccess( pReadAcc );
418cdf0e10cSrcweir 
419cdf0e10cSrcweir 		if( bRet )
420cdf0e10cSrcweir 		{
421cdf0e10cSrcweir 			const MapMode	aMap( maPrefMapMode );
422cdf0e10cSrcweir 			const Size		aSize( maPrefSize );
423cdf0e10cSrcweir 
424cdf0e10cSrcweir 			*this = aNewBmp;
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 			maPrefMapMode = aMap;
427cdf0e10cSrcweir 			maPrefSize = aSize;
428cdf0e10cSrcweir 		}
429cdf0e10cSrcweir 	}
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 	return bRet;
432cdf0e10cSrcweir }
433cdf0e10cSrcweir 
434cdf0e10cSrcweir // ------------------------------------------------------------------------
435cdf0e10cSrcweir 
436cdf0e10cSrcweir sal_Bool Bitmap::ImplMakeMonoDither()
437cdf0e10cSrcweir {
438cdf0e10cSrcweir 	BitmapReadAccess*	pReadAcc = AcquireReadAccess();
439cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
440cdf0e10cSrcweir 
441cdf0e10cSrcweir 	if( pReadAcc )
442cdf0e10cSrcweir 	{
443cdf0e10cSrcweir 		Bitmap				aNewBmp( GetSizePixel(), 1 );
444cdf0e10cSrcweir 		BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 		if( pWriteAcc )
447cdf0e10cSrcweir 		{
448cdf0e10cSrcweir 			const BitmapColor	aBlack( pWriteAcc->GetBestMatchingColor( Color( COL_BLACK ) ) );
449cdf0e10cSrcweir 			const BitmapColor	aWhite( pWriteAcc->GetBestMatchingColor( Color( COL_WHITE ) ) );
450cdf0e10cSrcweir 			const long			nWidth = pWriteAcc->Width();
451cdf0e10cSrcweir 			const long			nHeight = pWriteAcc->Height();
452cdf0e10cSrcweir 			sal_uInt8				pDitherMatrix[ 16 ][ 16 ];
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 			ImplCreateDitherMatrix( &pDitherMatrix );
455cdf0e10cSrcweir 
456cdf0e10cSrcweir 			if( pReadAcc->HasPalette() )
457cdf0e10cSrcweir 			{
458cdf0e10cSrcweir 				for( long nY = 0L; nY < nHeight; nY++ )
459cdf0e10cSrcweir 				{
460cdf0e10cSrcweir 					for( long nX = 0L, nModY = nY % 16; nX < nWidth; nX++ )
461cdf0e10cSrcweir 					{
462cdf0e10cSrcweir 						if( pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nY, nX ) ).GetLuminance() >
463cdf0e10cSrcweir 							pDitherMatrix[ nModY ][ nX % 16 ] )
464cdf0e10cSrcweir 						{
465cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aWhite );
466cdf0e10cSrcweir 						}
467cdf0e10cSrcweir 						else
468cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aBlack );
469cdf0e10cSrcweir 					}
470cdf0e10cSrcweir 				}
471cdf0e10cSrcweir 			}
472cdf0e10cSrcweir 			else
473cdf0e10cSrcweir 			{
474cdf0e10cSrcweir 				for( long nY = 0L; nY < nHeight; nY++ )
475cdf0e10cSrcweir 				{
476cdf0e10cSrcweir 					for( long nX = 0L, nModY = nY % 16; nX < nWidth; nX++ )
477cdf0e10cSrcweir 					{
478cdf0e10cSrcweir 						if( pReadAcc->GetPixel( nY, nX ).GetLuminance() >
479cdf0e10cSrcweir 							pDitherMatrix[ nModY ][ nX % 16 ]  )
480cdf0e10cSrcweir 						{
481cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aWhite );
482cdf0e10cSrcweir 						}
483cdf0e10cSrcweir 						else
484cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aBlack );
485cdf0e10cSrcweir 					}
486cdf0e10cSrcweir 				}
487cdf0e10cSrcweir 			}
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 			aNewBmp.ReleaseAccess( pWriteAcc );
490cdf0e10cSrcweir 			bRet = sal_True;
491cdf0e10cSrcweir 		}
492cdf0e10cSrcweir 
493cdf0e10cSrcweir 		ReleaseAccess( pReadAcc );
494cdf0e10cSrcweir 
495cdf0e10cSrcweir 		if( bRet )
496cdf0e10cSrcweir 		{
497cdf0e10cSrcweir 			const MapMode	aMap( maPrefMapMode );
498cdf0e10cSrcweir 			const Size		aSize( maPrefSize );
499cdf0e10cSrcweir 
500cdf0e10cSrcweir 			*this = aNewBmp;
501cdf0e10cSrcweir 
502cdf0e10cSrcweir 			maPrefMapMode = aMap;
503cdf0e10cSrcweir 			maPrefSize = aSize;
504cdf0e10cSrcweir 		}
505cdf0e10cSrcweir 	}
506cdf0e10cSrcweir 
507cdf0e10cSrcweir 	return bRet;
508cdf0e10cSrcweir }
509cdf0e10cSrcweir 
510cdf0e10cSrcweir // ------------------------------------------------------------------------
511cdf0e10cSrcweir 
512cdf0e10cSrcweir sal_Bool Bitmap::ImplMakeGreyscales( sal_uInt16 nGreys )
513cdf0e10cSrcweir {
514cdf0e10cSrcweir 	DBG_ASSERT( nGreys == 16 || nGreys == 256, "Only 16 or 256 greyscales are supported!" );
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 	BitmapReadAccess*	pReadAcc = AcquireReadAccess();
517cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
518cdf0e10cSrcweir 
519cdf0e10cSrcweir 	if( pReadAcc )
520cdf0e10cSrcweir 	{
521cdf0e10cSrcweir 		const BitmapPalette&	rPal = GetGreyPalette( nGreys );
522cdf0e10cSrcweir 		sal_uLong 					nShift = ( ( nGreys == 16 ) ? 4UL : 0UL );
523cdf0e10cSrcweir 		sal_Bool					bPalDiffers = !pReadAcc->HasPalette() || ( rPal.GetEntryCount() != pReadAcc->GetPaletteEntryCount() );
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 		if( !bPalDiffers )
526cdf0e10cSrcweir 			bPalDiffers = ( (BitmapPalette&) rPal != pReadAcc->GetPalette() );
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 		if( bPalDiffers )
529cdf0e10cSrcweir 		{
530cdf0e10cSrcweir 			Bitmap				aNewBmp( GetSizePixel(), ( nGreys == 16 ) ? 4 : 8, &rPal );
531cdf0e10cSrcweir 			BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 			if( pWriteAcc )
534cdf0e10cSrcweir 			{
535cdf0e10cSrcweir 				const long	nWidth = pWriteAcc->Width();
536cdf0e10cSrcweir 				const long	nHeight = pWriteAcc->Height();
537cdf0e10cSrcweir 
538cdf0e10cSrcweir 				if( pReadAcc->HasPalette() )
539cdf0e10cSrcweir 				{
540cdf0e10cSrcweir 					for( long nY = 0L; nY < nHeight; nY++ )
541cdf0e10cSrcweir 					{
542cdf0e10cSrcweir 						for( long nX = 0L; nX < nWidth; nX++ )
543cdf0e10cSrcweir 						{
544cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX,
545cdf0e10cSrcweir 								(sal_uInt8) ( pReadAcc->GetPaletteColor(
546cdf0e10cSrcweir 									pReadAcc->GetPixel( nY, nX ) ).GetLuminance() >> nShift ) );
547cdf0e10cSrcweir 						}
548cdf0e10cSrcweir 					}
549cdf0e10cSrcweir 				}
550cdf0e10cSrcweir 				else if( pReadAcc->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_BGR &&
551cdf0e10cSrcweir 						 pWriteAcc->GetScanlineFormat() == BMP_FORMAT_8BIT_PAL )
552cdf0e10cSrcweir 				{
553cdf0e10cSrcweir 					nShift += 8;
554cdf0e10cSrcweir 
555cdf0e10cSrcweir 					for( long nY = 0L; nY < nHeight; nY++ )
556cdf0e10cSrcweir 					{
557cdf0e10cSrcweir 						Scanline pReadScan = pReadAcc->GetScanline( nY );
558cdf0e10cSrcweir 						Scanline pWriteScan = pWriteAcc->GetScanline( nY );
559cdf0e10cSrcweir 
560cdf0e10cSrcweir 						for( long nX = 0L; nX < nWidth; nX++ )
561cdf0e10cSrcweir 						{
562cdf0e10cSrcweir 							const sal_uLong nB = *pReadScan++;
563cdf0e10cSrcweir 							const sal_uLong nG = *pReadScan++;
564cdf0e10cSrcweir 							const sal_uLong nR = *pReadScan++;
565cdf0e10cSrcweir 
566cdf0e10cSrcweir 							*pWriteScan++ = (sal_uInt8) ( ( nB * 28UL + nG * 151UL + nR * 77UL ) >> nShift );
567cdf0e10cSrcweir 						}
568cdf0e10cSrcweir 					}
569cdf0e10cSrcweir 				}
570cdf0e10cSrcweir 				else if( pReadAcc->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_RGB &&
571cdf0e10cSrcweir 						 pWriteAcc->GetScanlineFormat() == BMP_FORMAT_8BIT_PAL )
572cdf0e10cSrcweir 				{
573cdf0e10cSrcweir 					nShift += 8;
574cdf0e10cSrcweir 
575cdf0e10cSrcweir 					for( long nY = 0L; nY < nHeight; nY++ )
576cdf0e10cSrcweir 					{
577cdf0e10cSrcweir 						Scanline pReadScan = pReadAcc->GetScanline( nY );
578cdf0e10cSrcweir 						Scanline pWriteScan = pWriteAcc->GetScanline( nY );
579cdf0e10cSrcweir 
580cdf0e10cSrcweir 						for( long nX = 0L; nX < nWidth; nX++ )
581cdf0e10cSrcweir 						{
582cdf0e10cSrcweir 							const sal_uLong nR = *pReadScan++;
583cdf0e10cSrcweir 							const sal_uLong nG = *pReadScan++;
584cdf0e10cSrcweir 							const sal_uLong nB = *pReadScan++;
585cdf0e10cSrcweir 
586cdf0e10cSrcweir 							*pWriteScan++ = (sal_uInt8) ( ( nB * 28UL + nG * 151UL + nR * 77UL ) >> nShift );
587cdf0e10cSrcweir 						}
588cdf0e10cSrcweir 					}
589cdf0e10cSrcweir 				}
590cdf0e10cSrcweir 				else
591cdf0e10cSrcweir 				{
592cdf0e10cSrcweir 					for( long nY = 0L; nY < nHeight; nY++ )
593cdf0e10cSrcweir 						for( long nX = 0L; nX < nWidth; nX++ )
594cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, sal::static_int_cast<sal_uInt8>(( pReadAcc->GetPixel( nY, nX ) ).GetLuminance() >> nShift) );
595cdf0e10cSrcweir 				}
596cdf0e10cSrcweir 
597cdf0e10cSrcweir 				aNewBmp.ReleaseAccess( pWriteAcc );
598cdf0e10cSrcweir 				bRet = sal_True;
599cdf0e10cSrcweir 			}
600cdf0e10cSrcweir 
601cdf0e10cSrcweir 			ReleaseAccess( pReadAcc );
602cdf0e10cSrcweir 
603cdf0e10cSrcweir 			if( bRet )
604cdf0e10cSrcweir 			{
605cdf0e10cSrcweir 				const MapMode	aMap( maPrefMapMode );
606cdf0e10cSrcweir 				const Size		aSize( maPrefSize );
607cdf0e10cSrcweir 
608cdf0e10cSrcweir 				*this = aNewBmp;
609cdf0e10cSrcweir 
610cdf0e10cSrcweir 				maPrefMapMode = aMap;
611cdf0e10cSrcweir 				maPrefSize = aSize;
612cdf0e10cSrcweir 			}
613cdf0e10cSrcweir 		}
614cdf0e10cSrcweir 		else
615cdf0e10cSrcweir 		{
616cdf0e10cSrcweir 			ReleaseAccess( pReadAcc );
617cdf0e10cSrcweir 			bRet = sal_True;
618cdf0e10cSrcweir 		}
619cdf0e10cSrcweir 	}
620cdf0e10cSrcweir 
621cdf0e10cSrcweir 	return bRet;
622cdf0e10cSrcweir }
623cdf0e10cSrcweir 
624cdf0e10cSrcweir // ------------------------------------------------------------------------
625cdf0e10cSrcweir 
626cdf0e10cSrcweir sal_Bool Bitmap::ImplConvertUp( sal_uInt16 nBitCount, Color* pExtColor )
627cdf0e10cSrcweir {
628cdf0e10cSrcweir 	DBG_ASSERT( nBitCount > GetBitCount(), "New BitCount must be greater!" );
629cdf0e10cSrcweir 
630cdf0e10cSrcweir 	BitmapReadAccess*	pReadAcc = AcquireReadAccess();
631cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
632cdf0e10cSrcweir 
633cdf0e10cSrcweir 	if( pReadAcc )
634cdf0e10cSrcweir 	{
635cdf0e10cSrcweir 		BitmapPalette		aPal;
636cdf0e10cSrcweir 		Bitmap				aNewBmp( GetSizePixel(), nBitCount, pReadAcc->HasPalette() ? &pReadAcc->GetPalette() : &aPal );
637cdf0e10cSrcweir 		BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
638cdf0e10cSrcweir 
639cdf0e10cSrcweir 		if( pWriteAcc )
640cdf0e10cSrcweir 		{
641cdf0e10cSrcweir 			const long	nWidth = pWriteAcc->Width();
642cdf0e10cSrcweir 			const long	nHeight = pWriteAcc->Height();
643cdf0e10cSrcweir 
644cdf0e10cSrcweir 			if( pWriteAcc->HasPalette() )
645cdf0e10cSrcweir 			{
646cdf0e10cSrcweir 				const sal_uInt16			nOldCount = 1 << GetBitCount();
647cdf0e10cSrcweir 				const BitmapPalette&	rOldPal = pReadAcc->GetPalette();
648cdf0e10cSrcweir 
649cdf0e10cSrcweir 				aPal.SetEntryCount( 1 << nBitCount );
650cdf0e10cSrcweir 
651cdf0e10cSrcweir 				for( sal_uInt16 i = 0; i < nOldCount; i++ )
652cdf0e10cSrcweir 					aPal[ i ] = rOldPal[ i ];
653cdf0e10cSrcweir 
654cdf0e10cSrcweir 				if( pExtColor )
655cdf0e10cSrcweir 					aPal[ aPal.GetEntryCount() - 1 ] = *pExtColor;
656cdf0e10cSrcweir 
657cdf0e10cSrcweir 				pWriteAcc->SetPalette( aPal );
658cdf0e10cSrcweir 
659cdf0e10cSrcweir 				for( long nY = 0L; nY < nHeight; nY++ )
660cdf0e10cSrcweir 					for( long nX = 0L; nX < nWidth; nX++ )
661cdf0e10cSrcweir 						pWriteAcc->SetPixel( nY, nX, pReadAcc->GetPixel( nY, nX ) );
662cdf0e10cSrcweir 			}
663cdf0e10cSrcweir 			else
664cdf0e10cSrcweir 			{
665cdf0e10cSrcweir 				if( pReadAcc->HasPalette() )
666cdf0e10cSrcweir 				{
667cdf0e10cSrcweir 					for( long nY = 0L; nY < nHeight; nY++ )
668cdf0e10cSrcweir 						for( long nX = 0L; nX < nWidth; nX++ )
669cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nY, nX ) ) );
670cdf0e10cSrcweir 				}
671cdf0e10cSrcweir 				else
672cdf0e10cSrcweir 				{
673cdf0e10cSrcweir 					for( long nY = 0L; nY < nHeight; nY++ )
674cdf0e10cSrcweir 						for( long nX = 0L; nX < nWidth; nX++ )
675cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, pReadAcc->GetPixel( nY, nX ) );
676cdf0e10cSrcweir 				}
677cdf0e10cSrcweir 			}
678cdf0e10cSrcweir 
679cdf0e10cSrcweir 			aNewBmp.ReleaseAccess( pWriteAcc );
680cdf0e10cSrcweir 			bRet = sal_True;
681cdf0e10cSrcweir 		}
682cdf0e10cSrcweir 
683cdf0e10cSrcweir 		ReleaseAccess( pReadAcc );
684cdf0e10cSrcweir 
685cdf0e10cSrcweir 		if( bRet )
686cdf0e10cSrcweir 		{
687cdf0e10cSrcweir 			const MapMode	aMap( maPrefMapMode );
688cdf0e10cSrcweir 			const Size		aSize( maPrefSize );
689cdf0e10cSrcweir 
690cdf0e10cSrcweir 			*this = aNewBmp;
691cdf0e10cSrcweir 
692cdf0e10cSrcweir 			maPrefMapMode = aMap;
693cdf0e10cSrcweir 			maPrefSize = aSize;
694cdf0e10cSrcweir 		}
695cdf0e10cSrcweir 	}
696cdf0e10cSrcweir 
697cdf0e10cSrcweir 	return bRet;
698cdf0e10cSrcweir }
699cdf0e10cSrcweir 
700cdf0e10cSrcweir // ------------------------------------------------------------------------
701cdf0e10cSrcweir 
702cdf0e10cSrcweir sal_Bool Bitmap::ImplConvertDown( sal_uInt16 nBitCount, Color* pExtColor )
703cdf0e10cSrcweir {
704cdf0e10cSrcweir 	DBG_ASSERT( nBitCount <= GetBitCount(), "New BitCount must be lower ( or equal when pExtColor is set )!" );
705cdf0e10cSrcweir 
706cdf0e10cSrcweir 	BitmapReadAccess*	pReadAcc = AcquireReadAccess();
707cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
708cdf0e10cSrcweir 
709cdf0e10cSrcweir 	if( pReadAcc )
710cdf0e10cSrcweir 	{
711cdf0e10cSrcweir 		BitmapPalette		aPal;
712cdf0e10cSrcweir 		Bitmap				aNewBmp( GetSizePixel(), nBitCount, &aPal );
713cdf0e10cSrcweir 		BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
714cdf0e10cSrcweir 
715cdf0e10cSrcweir 		if( pWriteAcc )
716cdf0e10cSrcweir 		{
717cdf0e10cSrcweir 			const sal_uInt16	nCount = 1 << nBitCount;
718cdf0e10cSrcweir 			const long		nWidth = pWriteAcc->Width();
719cdf0e10cSrcweir 			const long		nWidth1 = nWidth - 1L;
720cdf0e10cSrcweir 			const long		nHeight = pWriteAcc->Height();
721cdf0e10cSrcweir 			Octree			aOctree( *pReadAcc, pExtColor ? ( nCount - 1 ) : nCount );
722cdf0e10cSrcweir 			InverseColorMap aColorMap( aPal = aOctree.GetPalette() );
723cdf0e10cSrcweir 			BitmapColor 	aColor;
724cdf0e10cSrcweir 			ImpErrorQuad	aErrQuad;
725cdf0e10cSrcweir 			ImpErrorQuad*	pErrQuad1 = new ImpErrorQuad[ nWidth ];
726cdf0e10cSrcweir 			ImpErrorQuad*	pErrQuad2 = new ImpErrorQuad[ nWidth ];
727cdf0e10cSrcweir 			ImpErrorQuad*	pQLine1 = pErrQuad1;
728cdf0e10cSrcweir 			ImpErrorQuad*	pQLine2 = 0;
729cdf0e10cSrcweir 			long			nX, nY;
730cdf0e10cSrcweir 			long			nYTmp = 0L;
731cdf0e10cSrcweir 			sal_uInt8			cIndex;
732cdf0e10cSrcweir 			sal_Bool			bQ1 = sal_True;
733cdf0e10cSrcweir 
734cdf0e10cSrcweir 			if( pExtColor )
735cdf0e10cSrcweir 			{
736cdf0e10cSrcweir 				aPal.SetEntryCount( aPal.GetEntryCount() + 1 );
737cdf0e10cSrcweir 				aPal[ aPal.GetEntryCount() - 1 ] = *pExtColor;
738cdf0e10cSrcweir 			}
739cdf0e10cSrcweir 
740cdf0e10cSrcweir 			// set Black/White always, if we have enough space
741cdf0e10cSrcweir 			if( aPal.GetEntryCount() < ( nCount - 1 ) )
742cdf0e10cSrcweir 			{
743cdf0e10cSrcweir 				aPal.SetEntryCount( aPal.GetEntryCount() + 2 );
744cdf0e10cSrcweir 				aPal[ aPal.GetEntryCount() - 2 ] = Color( COL_BLACK );
745cdf0e10cSrcweir 				aPal[ aPal.GetEntryCount() - 1 ] = Color( COL_WHITE );
746cdf0e10cSrcweir 			}
747cdf0e10cSrcweir 
748cdf0e10cSrcweir 			pWriteAcc->SetPalette( aPal );
749cdf0e10cSrcweir 
750cdf0e10cSrcweir 			for( nY = 0L; nY < Min( nHeight, 2L ); nY++, nYTmp++ )
751cdf0e10cSrcweir 			{
752cdf0e10cSrcweir 				for( nX = 0L, pQLine2 = !nY ? pErrQuad1 : pErrQuad2; nX < nWidth; nX++ )
753cdf0e10cSrcweir 				{
754cdf0e10cSrcweir 					if( pReadAcc->HasPalette() )
755cdf0e10cSrcweir 						pQLine2[ nX ] = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nYTmp, nX ) );
756cdf0e10cSrcweir 					else
757cdf0e10cSrcweir 						pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
758cdf0e10cSrcweir 				}
759cdf0e10cSrcweir 			}
760cdf0e10cSrcweir 
761cdf0e10cSrcweir 			for( nY = 0L; nY < nHeight; nY++, nYTmp++ )
762cdf0e10cSrcweir 			{
763cdf0e10cSrcweir 				// erstes ZeilenPixel
764cdf0e10cSrcweir 				cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex( pQLine1[ 0 ].ImplGetColor() );
765cdf0e10cSrcweir 				pWriteAcc->SetPixel( nY, 0, cIndex );
766cdf0e10cSrcweir 
767cdf0e10cSrcweir 				for( nX = 1L; nX < nWidth1; nX++ )
768cdf0e10cSrcweir 				{
769cdf0e10cSrcweir 					cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex( aColor = pQLine1[ nX ].ImplGetColor() );
770cdf0e10cSrcweir 					aErrQuad = ( ImpErrorQuad( aColor ) -= pWriteAcc->GetPaletteColor( cIndex ) );
771cdf0e10cSrcweir 					pQLine1[ ++nX ].ImplAddColorError7( aErrQuad );
772cdf0e10cSrcweir 					pQLine2[ nX-- ].ImplAddColorError1( aErrQuad );
773cdf0e10cSrcweir 					pQLine2[ nX-- ].ImplAddColorError5( aErrQuad );
774cdf0e10cSrcweir 					pQLine2[ nX++ ].ImplAddColorError3( aErrQuad );
775cdf0e10cSrcweir 					pWriteAcc->SetPixel( nY, nX, cIndex );
776cdf0e10cSrcweir 				}
777cdf0e10cSrcweir 
778cdf0e10cSrcweir 				// letztes ZeilenPixel
779cdf0e10cSrcweir                 if( nX < nWidth )
780cdf0e10cSrcweir                 {
781cdf0e10cSrcweir 				    cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex( pQLine1[ nWidth1 ].ImplGetColor() );
782cdf0e10cSrcweir 				    pWriteAcc->SetPixel( nY, nX, cIndex );
783cdf0e10cSrcweir                 }
784cdf0e10cSrcweir 
785cdf0e10cSrcweir 				// Zeilenpuffer neu fuellen/kopieren
786cdf0e10cSrcweir 				pQLine1 = pQLine2;
787cdf0e10cSrcweir 				pQLine2 = ( bQ1 = !bQ1 ) != sal_False ? pErrQuad2 : pErrQuad1;
788cdf0e10cSrcweir 
789cdf0e10cSrcweir 				if( nYTmp < nHeight )
790cdf0e10cSrcweir 				{
791cdf0e10cSrcweir 					for( nX = 0L; nX < nWidth; nX++ )
792cdf0e10cSrcweir 					{
793cdf0e10cSrcweir 						if( pReadAcc->HasPalette() )
794cdf0e10cSrcweir 								pQLine2[ nX ] = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nYTmp, nX ) );
795cdf0e10cSrcweir 						else
796cdf0e10cSrcweir 							pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
797cdf0e10cSrcweir 					}
798cdf0e10cSrcweir 				}
799cdf0e10cSrcweir 			}
800cdf0e10cSrcweir 
801cdf0e10cSrcweir 			// Zeilenpuffer zerstoeren
802cdf0e10cSrcweir 			delete[] pErrQuad1;
803cdf0e10cSrcweir 			delete[] pErrQuad2;
804cdf0e10cSrcweir 
805cdf0e10cSrcweir 			aNewBmp.ReleaseAccess( pWriteAcc );
806cdf0e10cSrcweir 			bRet = sal_True;
807cdf0e10cSrcweir 		}
808cdf0e10cSrcweir 
809cdf0e10cSrcweir 		ReleaseAccess( pReadAcc );
810cdf0e10cSrcweir 
811cdf0e10cSrcweir 		if( bRet )
812cdf0e10cSrcweir 		{
813cdf0e10cSrcweir 			const MapMode	aMap( maPrefMapMode );
814cdf0e10cSrcweir 			const Size		aSize( maPrefSize );
815cdf0e10cSrcweir 
816cdf0e10cSrcweir 			*this = aNewBmp;
817cdf0e10cSrcweir 
818cdf0e10cSrcweir 			maPrefMapMode = aMap;
819cdf0e10cSrcweir 			maPrefSize = aSize;
820cdf0e10cSrcweir 		}
821cdf0e10cSrcweir 	}
822cdf0e10cSrcweir 
823cdf0e10cSrcweir 	return bRet;
824cdf0e10cSrcweir }
825cdf0e10cSrcweir 
826cdf0e10cSrcweir // ------------------------------------------------------------------------
827cdf0e10cSrcweir 
828cdf0e10cSrcweir sal_Bool Bitmap::ImplConvertGhosted()
829cdf0e10cSrcweir {
830cdf0e10cSrcweir 	Bitmap				aNewBmp;
831cdf0e10cSrcweir 	BitmapReadAccess*	pR = AcquireReadAccess();
832cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
833cdf0e10cSrcweir 
834cdf0e10cSrcweir 	if( pR )
835cdf0e10cSrcweir 	{
836cdf0e10cSrcweir 		if( pR->HasPalette() )
837cdf0e10cSrcweir 		{
838cdf0e10cSrcweir 			BitmapPalette aNewPal( pR->GetPaletteEntryCount() );
839cdf0e10cSrcweir 
840cdf0e10cSrcweir 			for( long i = 0, nCount = aNewPal.GetEntryCount(); i < nCount; i++ )
841cdf0e10cSrcweir 			{
842cdf0e10cSrcweir 				const BitmapColor& rOld = pR->GetPaletteColor( (sal_uInt16) i );
843cdf0e10cSrcweir 				aNewPal[ (sal_uInt16) i ] = BitmapColor( ( rOld.GetRed() >> 1 ) | 0x80,
844cdf0e10cSrcweir 													 ( rOld.GetGreen() >> 1 ) | 0x80,
845cdf0e10cSrcweir 													 ( rOld.GetBlue() >> 1 ) | 0x80 );
846cdf0e10cSrcweir 			}
847cdf0e10cSrcweir 
848cdf0e10cSrcweir 			aNewBmp = Bitmap( GetSizePixel(), GetBitCount(), &aNewPal );
849cdf0e10cSrcweir 			BitmapWriteAccess* pW = aNewBmp.AcquireWriteAccess();
850cdf0e10cSrcweir 
851cdf0e10cSrcweir 			if( pW )
852cdf0e10cSrcweir 			{
853cdf0e10cSrcweir 				pW->CopyBuffer( *pR );
854cdf0e10cSrcweir 				aNewBmp.ReleaseAccess( pW );
855cdf0e10cSrcweir 				bRet = sal_True;
856cdf0e10cSrcweir 			}
857cdf0e10cSrcweir 		}
858cdf0e10cSrcweir 		else
859cdf0e10cSrcweir 		{
860cdf0e10cSrcweir 			aNewBmp = Bitmap( GetSizePixel(), 24 );
861cdf0e10cSrcweir 
862cdf0e10cSrcweir 			BitmapWriteAccess* pW = aNewBmp.AcquireWriteAccess();
863cdf0e10cSrcweir 
864cdf0e10cSrcweir 			if( pW )
865cdf0e10cSrcweir 			{
866cdf0e10cSrcweir 				const long nWidth = pR->Width(), nHeight = pR->Height();
867cdf0e10cSrcweir 
868cdf0e10cSrcweir 				for( long nY = 0; nY < nHeight; nY++ )
869cdf0e10cSrcweir 				{
870cdf0e10cSrcweir 					for( long nX = 0; nX < nWidth; nX++ )
871cdf0e10cSrcweir 					{
872cdf0e10cSrcweir 						const BitmapColor aOld( pR->GetPixel( nY, nX ) );
873cdf0e10cSrcweir 						pW->SetPixel( nY, nX, BitmapColor( ( aOld.GetRed() >> 1 ) | 0x80,
874cdf0e10cSrcweir 														   ( aOld.GetGreen() >> 1 ) | 0x80,
875cdf0e10cSrcweir 														   ( aOld.GetBlue() >> 1 ) | 0x80 ) );
876cdf0e10cSrcweir 
877cdf0e10cSrcweir 					}
878cdf0e10cSrcweir 				}
879cdf0e10cSrcweir 
880cdf0e10cSrcweir 				aNewBmp.ReleaseAccess( pW );
881cdf0e10cSrcweir 				bRet = sal_True;
882cdf0e10cSrcweir 			}
883cdf0e10cSrcweir 		}
884cdf0e10cSrcweir 
885cdf0e10cSrcweir 		ReleaseAccess( pR );
886cdf0e10cSrcweir 	}
887cdf0e10cSrcweir 
888cdf0e10cSrcweir 	if( bRet )
889cdf0e10cSrcweir 	{
890cdf0e10cSrcweir 		const MapMode	aMap( maPrefMapMode );
891cdf0e10cSrcweir 		const Size		aSize( maPrefSize );
892cdf0e10cSrcweir 
893cdf0e10cSrcweir 		*this = aNewBmp;
894cdf0e10cSrcweir 
895cdf0e10cSrcweir 		maPrefMapMode = aMap;
896cdf0e10cSrcweir 		maPrefSize = aSize;
897cdf0e10cSrcweir 	}
898cdf0e10cSrcweir 
899cdf0e10cSrcweir 	return bRet;
900cdf0e10cSrcweir }
901cdf0e10cSrcweir 
902cdf0e10cSrcweir // ------------------------------------------------------------------------
903cdf0e10cSrcweir 
904cdf0e10cSrcweir sal_Bool Bitmap::Scale( const double& rScaleX, const double& rScaleY, sal_uLong nScaleFlag )
905cdf0e10cSrcweir {
906cdf0e10cSrcweir 	sal_Bool bRet;
907cdf0e10cSrcweir 
908cdf0e10cSrcweir 	if( ( rScaleX != 1.0 ) || ( rScaleY != 1.0 ) )
909cdf0e10cSrcweir 	{
910cdf0e10cSrcweir 		if( BMP_SCALE_FAST == nScaleFlag )
911cdf0e10cSrcweir 			bRet = ImplScaleFast( rScaleX, rScaleY );
912cdf0e10cSrcweir 		else if( BMP_SCALE_INTERPOLATE == nScaleFlag )
913cdf0e10cSrcweir 			bRet = ImplScaleInterpolate( rScaleX, rScaleY );
914cdf0e10cSrcweir 		else
915cdf0e10cSrcweir 			bRet = sal_False;
916cdf0e10cSrcweir 	}
917cdf0e10cSrcweir 	else
918cdf0e10cSrcweir 		bRet = sal_True;
919cdf0e10cSrcweir 
920cdf0e10cSrcweir 	return bRet;
921cdf0e10cSrcweir }
922cdf0e10cSrcweir 
923cdf0e10cSrcweir // ------------------------------------------------------------------------
924cdf0e10cSrcweir 
925cdf0e10cSrcweir sal_Bool Bitmap::Scale( const Size& rNewSize, sal_uLong nScaleFlag )
926cdf0e10cSrcweir {
927cdf0e10cSrcweir 	const Size	aSize( GetSizePixel() );
928cdf0e10cSrcweir 	sal_Bool		bRet;
929cdf0e10cSrcweir 
930cdf0e10cSrcweir 	if( aSize.Width() && aSize.Height() )
931cdf0e10cSrcweir 	{
932cdf0e10cSrcweir 		bRet = Scale( (double) rNewSize.Width() / aSize.Width(),
933cdf0e10cSrcweir 					  (double) rNewSize.Height() / aSize.Height(),
934cdf0e10cSrcweir 					  nScaleFlag );
935cdf0e10cSrcweir 	}
936cdf0e10cSrcweir 	else
937cdf0e10cSrcweir 		bRet = sal_True;
938cdf0e10cSrcweir 
939cdf0e10cSrcweir 	return bRet;
940cdf0e10cSrcweir }
941cdf0e10cSrcweir 
942cdf0e10cSrcweir // ------------------------------------------------------------------------
943cdf0e10cSrcweir 
944cdf0e10cSrcweir sal_Bool Bitmap::ImplScaleFast( const double& rScaleX, const double& rScaleY )
945cdf0e10cSrcweir {
946cdf0e10cSrcweir 	const Size	aSizePix( GetSizePixel() );
947cdf0e10cSrcweir 	const long	nNewWidth = FRound( aSizePix.Width() * rScaleX );
948cdf0e10cSrcweir 	const long	nNewHeight = FRound( aSizePix.Height() * rScaleY );
949cdf0e10cSrcweir 	sal_Bool		bRet = sal_False;
950cdf0e10cSrcweir 
951cdf0e10cSrcweir 	if( nNewWidth && nNewHeight )
952cdf0e10cSrcweir 	{
953cdf0e10cSrcweir 		BitmapReadAccess*	pReadAcc = AcquireReadAccess();
954cdf0e10cSrcweir 		Bitmap				aNewBmp( Size( nNewWidth, nNewHeight ), GetBitCount(), &pReadAcc->GetPalette() );
955cdf0e10cSrcweir 		BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
956cdf0e10cSrcweir 
957cdf0e10cSrcweir 		if( pReadAcc && pWriteAcc )
958cdf0e10cSrcweir 		{
959cdf0e10cSrcweir 			const long	nScanlineSize = pWriteAcc->GetScanlineSize();
960cdf0e10cSrcweir 			const long	nNewWidth1 = nNewWidth - 1L;
961cdf0e10cSrcweir 			const long	nNewHeight1 = nNewHeight - 1L;
962cdf0e10cSrcweir 			const long	nWidth = pReadAcc->Width();
963cdf0e10cSrcweir 			const long	nHeight = pReadAcc->Height();
964cdf0e10cSrcweir 			long*		pLutX = new long[ nNewWidth ];
965cdf0e10cSrcweir 			long*		pLutY = new long[ nNewHeight ];
966cdf0e10cSrcweir 			long		nX, nY, nMapY, nActY = 0L;
967cdf0e10cSrcweir 
968cdf0e10cSrcweir 			if( nNewWidth1 && nNewHeight1 )
969cdf0e10cSrcweir 			{
970cdf0e10cSrcweir 				for( nX = 0L; nX < nNewWidth; nX++ )
971cdf0e10cSrcweir 					pLutX[ nX ] = nX * nWidth / nNewWidth;
972cdf0e10cSrcweir 
973cdf0e10cSrcweir 				for( nY = 0L; nY < nNewHeight; nY++ )
974cdf0e10cSrcweir 					pLutY[ nY ] = nY * nHeight / nNewHeight;
975cdf0e10cSrcweir 
976cdf0e10cSrcweir 				while( nActY < nNewHeight )
977cdf0e10cSrcweir 				{
978cdf0e10cSrcweir 					nMapY = pLutY[ nActY ];
979cdf0e10cSrcweir 
980cdf0e10cSrcweir 					for( nX = 0L; nX < nNewWidth; nX++ )
981cdf0e10cSrcweir 						pWriteAcc->SetPixel( nActY, nX, pReadAcc->GetPixel( nMapY , pLutX[ nX ] ) );
982cdf0e10cSrcweir 
983cdf0e10cSrcweir 					while( ( nActY < nNewHeight1 ) && ( pLutY[ nActY + 1 ] == nMapY ) )
984cdf0e10cSrcweir 					{
985cdf0e10cSrcweir 						memcpy( pWriteAcc->GetScanline( nActY + 1L ),
986cdf0e10cSrcweir 								 pWriteAcc->GetScanline( nActY ), nScanlineSize );
987cdf0e10cSrcweir 						nActY++;
988cdf0e10cSrcweir 					}
989cdf0e10cSrcweir 
990cdf0e10cSrcweir 					nActY++;
991cdf0e10cSrcweir 				}
992cdf0e10cSrcweir 
993cdf0e10cSrcweir 				bRet = sal_True;
994cdf0e10cSrcweir 			}
995cdf0e10cSrcweir 
996cdf0e10cSrcweir 			delete[] pLutX;
997cdf0e10cSrcweir 			delete[] pLutY;
998cdf0e10cSrcweir 		}
999cdf0e10cSrcweir 
1000cdf0e10cSrcweir 		ReleaseAccess( pReadAcc );
1001cdf0e10cSrcweir 		aNewBmp.ReleaseAccess( pWriteAcc );
1002cdf0e10cSrcweir 
1003cdf0e10cSrcweir 		if( bRet )
1004cdf0e10cSrcweir 			ImplAssignWithSize( aNewBmp );
1005cdf0e10cSrcweir 	}
1006cdf0e10cSrcweir 
1007cdf0e10cSrcweir 	return bRet;
1008cdf0e10cSrcweir }
1009cdf0e10cSrcweir 
1010cdf0e10cSrcweir // ------------------------------------------------------------------------
1011cdf0e10cSrcweir 
1012cdf0e10cSrcweir sal_Bool Bitmap::ImplScaleInterpolate( const double& rScaleX, const double& rScaleY )
1013cdf0e10cSrcweir {
1014cdf0e10cSrcweir 	const Size	aSizePix( GetSizePixel() );
1015cdf0e10cSrcweir 	const long	nNewWidth = FRound( aSizePix.Width() * rScaleX );
1016cdf0e10cSrcweir 	const long	nNewHeight = FRound( aSizePix.Height() * rScaleY );
1017cdf0e10cSrcweir 	sal_Bool		bRet = sal_False;
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir 	if( ( nNewWidth > 1L ) && ( nNewHeight > 1L ) )
1020cdf0e10cSrcweir 	{
1021cdf0e10cSrcweir 		BitmapColor 		aCol0;
1022cdf0e10cSrcweir 		BitmapColor 		aCol1;
1023cdf0e10cSrcweir 		BitmapReadAccess*	pReadAcc = AcquireReadAccess();
1024cdf0e10cSrcweir 		long				nWidth = pReadAcc->Width();
1025cdf0e10cSrcweir 		long				nHeight = pReadAcc->Height();
1026cdf0e10cSrcweir 		Bitmap				aNewBmp( Size( nNewWidth, nHeight ), 24 );
1027cdf0e10cSrcweir 		BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
1028cdf0e10cSrcweir 		long*				pLutInt;
1029cdf0e10cSrcweir 		long*				pLutFrac;
1030cdf0e10cSrcweir 		long				nX, nY;
1031cdf0e10cSrcweir 		long				lXB0, lXB1, lXG0, lXG1, lXR0, lXR1;
1032cdf0e10cSrcweir 		double				fTemp;
1033cdf0e10cSrcweir 		long				nTemp;
1034cdf0e10cSrcweir 
1035cdf0e10cSrcweir 		if( pReadAcc && pWriteAcc )
1036cdf0e10cSrcweir 		{
1037cdf0e10cSrcweir 			const long		nNewWidth1 = nNewWidth - 1L;
1038cdf0e10cSrcweir 			const long		nWidth1 = pReadAcc->Width() - 1L;
1039cdf0e10cSrcweir 			const double	fRevScaleX = (double) nWidth1 / nNewWidth1;
1040cdf0e10cSrcweir 
1041cdf0e10cSrcweir 			pLutInt = new long[ nNewWidth ];
1042cdf0e10cSrcweir 			pLutFrac = new long[ nNewWidth ];
1043cdf0e10cSrcweir 
1044cdf0e10cSrcweir 			for( nX = 0L, nTemp = nWidth - 2L; nX < nNewWidth; nX++ )
1045cdf0e10cSrcweir 			{
1046cdf0e10cSrcweir 				fTemp = nX * fRevScaleX;
1047cdf0e10cSrcweir 				pLutInt[ nX ] = MinMax( (long) fTemp, 0, nTemp );
1048cdf0e10cSrcweir 				fTemp -= pLutInt[ nX ];
1049cdf0e10cSrcweir 				pLutFrac[ nX ] = (long) ( fTemp * 1024. );
1050cdf0e10cSrcweir 			}
1051cdf0e10cSrcweir 
1052cdf0e10cSrcweir 			if( pReadAcc->HasPalette() )
1053cdf0e10cSrcweir 			{
1054cdf0e10cSrcweir 				for( nY = 0L; nY < nHeight; nY++ )
1055cdf0e10cSrcweir 				{
1056cdf0e10cSrcweir 					if( 1 == nWidth )
1057cdf0e10cSrcweir 					{
1058cdf0e10cSrcweir 						aCol0 = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nY, 0 ) );
1059cdf0e10cSrcweir 
1060cdf0e10cSrcweir 						for( nX = 0L; nX < nNewWidth; nX++ )
1061cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aCol0 );
1062cdf0e10cSrcweir 					}
1063cdf0e10cSrcweir 					else
1064cdf0e10cSrcweir 					{
1065cdf0e10cSrcweir 						for( nX = 0L; nX < nNewWidth; nX++ )
1066cdf0e10cSrcweir 						{
1067cdf0e10cSrcweir 							nTemp = pLutInt[ nX ];
1068cdf0e10cSrcweir 
1069cdf0e10cSrcweir 							aCol0 = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nY, nTemp++ ) );
1070cdf0e10cSrcweir 							aCol1 = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nY, nTemp ) );
1071cdf0e10cSrcweir 
1072cdf0e10cSrcweir 							nTemp = pLutFrac[ nX ];
1073cdf0e10cSrcweir 
1074cdf0e10cSrcweir 							lXR1 = aCol1.GetRed() - ( lXR0 = aCol0.GetRed() );
1075cdf0e10cSrcweir 							lXG1 = aCol1.GetGreen() - ( lXG0 = aCol0.GetGreen() );
1076cdf0e10cSrcweir 							lXB1 = aCol1.GetBlue() - ( lXB0 = aCol0.GetBlue() );
1077cdf0e10cSrcweir 
1078cdf0e10cSrcweir 							aCol0.SetRed( (sal_uInt8) ( ( lXR1 * nTemp + ( lXR0 << 10 ) ) >> 10 ) );
1079cdf0e10cSrcweir 							aCol0.SetGreen( (sal_uInt8) ( ( lXG1 * nTemp + ( lXG0 << 10 ) ) >> 10 ) );
1080cdf0e10cSrcweir 							aCol0.SetBlue( (sal_uInt8) ( ( lXB1 * nTemp + ( lXB0 << 10 ) ) >> 10 ) );
1081cdf0e10cSrcweir 
1082cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aCol0 );
1083cdf0e10cSrcweir 						}
1084cdf0e10cSrcweir 					}
1085cdf0e10cSrcweir 				}
1086cdf0e10cSrcweir 			}
1087cdf0e10cSrcweir 			else
1088cdf0e10cSrcweir 			{
1089cdf0e10cSrcweir 				for( nY = 0L; nY < nHeight; nY++ )
1090cdf0e10cSrcweir 				{
1091cdf0e10cSrcweir 					if( 1 == nWidth )
1092cdf0e10cSrcweir 					{
1093cdf0e10cSrcweir 						aCol0 = pReadAcc->GetPixel( nY, 0 );
1094cdf0e10cSrcweir 
1095cdf0e10cSrcweir 						for( nX = 0L; nX < nNewWidth; nX++ )
1096cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aCol0 );
1097cdf0e10cSrcweir 					}
1098cdf0e10cSrcweir 					else
1099cdf0e10cSrcweir 					{
1100cdf0e10cSrcweir 						for( nX = 0L; nX < nNewWidth; nX++ )
1101cdf0e10cSrcweir 						{
1102cdf0e10cSrcweir 							nTemp = pLutInt[ nX ];
1103cdf0e10cSrcweir 
1104cdf0e10cSrcweir 							aCol0 = pReadAcc->GetPixel( nY, nTemp++ );
1105cdf0e10cSrcweir 							aCol1 = pReadAcc->GetPixel( nY, nTemp );
1106cdf0e10cSrcweir 
1107cdf0e10cSrcweir 							nTemp = pLutFrac[ nX ];
1108cdf0e10cSrcweir 
1109cdf0e10cSrcweir 							lXR1 = aCol1.GetRed() - ( lXR0 = aCol0.GetRed() );
1110cdf0e10cSrcweir 							lXG1 = aCol1.GetGreen() - ( lXG0 = aCol0.GetGreen() );
1111cdf0e10cSrcweir 							lXB1 = aCol1.GetBlue() - ( lXB0 = aCol0.GetBlue() );
1112cdf0e10cSrcweir 
1113cdf0e10cSrcweir 							aCol0.SetRed( (sal_uInt8) ( ( lXR1 * nTemp + ( lXR0 << 10 ) ) >> 10 ) );
1114cdf0e10cSrcweir 							aCol0.SetGreen( (sal_uInt8) ( ( lXG1 * nTemp + ( lXG0 << 10 ) ) >> 10 ) );
1115cdf0e10cSrcweir 							aCol0.SetBlue( (sal_uInt8) ( ( lXB1 * nTemp + ( lXB0 << 10 ) ) >> 10 ) );
1116cdf0e10cSrcweir 
1117cdf0e10cSrcweir 							pWriteAcc->SetPixel( nY, nX, aCol0 );
1118cdf0e10cSrcweir 						}
1119cdf0e10cSrcweir 					}
1120cdf0e10cSrcweir 				}
1121cdf0e10cSrcweir 			}
1122cdf0e10cSrcweir 
1123cdf0e10cSrcweir 			delete[] pLutInt;
1124cdf0e10cSrcweir 			delete[] pLutFrac;
1125cdf0e10cSrcweir 			bRet = sal_True;
1126cdf0e10cSrcweir 		}
1127cdf0e10cSrcweir 
1128cdf0e10cSrcweir 		ReleaseAccess( pReadAcc );
1129cdf0e10cSrcweir 		aNewBmp.ReleaseAccess( pWriteAcc );
1130cdf0e10cSrcweir 
1131cdf0e10cSrcweir 		if( bRet )
1132cdf0e10cSrcweir 		{
1133cdf0e10cSrcweir 			bRet = sal_False;
1134cdf0e10cSrcweir 			ImplAssignWithSize( aNewBmp );
1135cdf0e10cSrcweir 			pReadAcc = AcquireReadAccess();
1136cdf0e10cSrcweir 			aNewBmp = Bitmap( Size( nNewWidth, nNewHeight ), 24 );
1137cdf0e10cSrcweir 			pWriteAcc = aNewBmp.AcquireWriteAccess();
1138cdf0e10cSrcweir 
1139cdf0e10cSrcweir 			if( pReadAcc && pWriteAcc )
1140cdf0e10cSrcweir 			{
1141cdf0e10cSrcweir 				const long		nNewHeight1 = nNewHeight - 1L;
1142cdf0e10cSrcweir 				const long		nHeight1 = pReadAcc->Height() - 1L;
1143cdf0e10cSrcweir 				const double	fRevScaleY = (double) nHeight1 / nNewHeight1;
1144cdf0e10cSrcweir 
1145cdf0e10cSrcweir 				pLutInt = new long[ nNewHeight ];
1146cdf0e10cSrcweir 				pLutFrac = new long[ nNewHeight ];
1147cdf0e10cSrcweir 
1148cdf0e10cSrcweir 				for( nY = 0L, nTemp = nHeight - 2L; nY < nNewHeight; nY++ )
1149cdf0e10cSrcweir 				{
1150cdf0e10cSrcweir 					fTemp = nY * fRevScaleY;
1151cdf0e10cSrcweir 					pLutInt[ nY ] = MinMax( (long) fTemp, 0, nTemp );
1152cdf0e10cSrcweir 					fTemp -= pLutInt[ nY ];
1153cdf0e10cSrcweir 					pLutFrac[ nY ] = (long) ( fTemp * 1024. );
1154cdf0e10cSrcweir 				}
1155cdf0e10cSrcweir 
1156cdf0e10cSrcweir 				if( pReadAcc->HasPalette() )
1157cdf0e10cSrcweir 				{
1158cdf0e10cSrcweir 					for( nX = 0L; nX < nNewWidth; nX++ )
1159cdf0e10cSrcweir 					{
1160cdf0e10cSrcweir 						if( 1 == nHeight )
1161cdf0e10cSrcweir 						{
1162cdf0e10cSrcweir 							aCol0 = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( 0, nX ) );
1163cdf0e10cSrcweir 
1164cdf0e10cSrcweir 							for( nY = 0L; nY < nNewHeight; nY++ )
1165cdf0e10cSrcweir 								pWriteAcc->SetPixel( nY, nX, aCol0 );
1166cdf0e10cSrcweir 						}
1167cdf0e10cSrcweir 						else
1168cdf0e10cSrcweir 						{
1169cdf0e10cSrcweir 							for( nY = 0L; nY < nNewHeight; nY++ )
1170cdf0e10cSrcweir 							{
1171cdf0e10cSrcweir 								nTemp = pLutInt[ nY ];
1172cdf0e10cSrcweir 
1173cdf0e10cSrcweir 								aCol0 = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nTemp++, nX ) );
1174cdf0e10cSrcweir 								aCol1 = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nTemp, nX ) );
1175cdf0e10cSrcweir 
1176cdf0e10cSrcweir 								nTemp = pLutFrac[ nY ];
1177cdf0e10cSrcweir 
1178cdf0e10cSrcweir 								lXR1 = aCol1.GetRed() - ( lXR0 = aCol0.GetRed() );
1179cdf0e10cSrcweir 								lXG1 = aCol1.GetGreen() - ( lXG0 = aCol0.GetGreen() );
1180cdf0e10cSrcweir 								lXB1 = aCol1.GetBlue() - ( lXB0 = aCol0.GetBlue() );
1181cdf0e10cSrcweir 
1182cdf0e10cSrcweir 								aCol0.SetRed( (sal_uInt8) ( ( lXR1 * nTemp + ( lXR0 << 10 ) ) >> 10 ) );
1183cdf0e10cSrcweir 								aCol0.SetGreen( (sal_uInt8) ( ( lXG1 * nTemp + ( lXG0 << 10 ) ) >> 10 ) );
1184cdf0e10cSrcweir 								aCol0.SetBlue( (sal_uInt8) ( ( lXB1 * nTemp + ( lXB0 << 10 ) ) >> 10 ) );
1185cdf0e10cSrcweir 
1186cdf0e10cSrcweir 								pWriteAcc->SetPixel( nY, nX, aCol0 );
1187cdf0e10cSrcweir 							}
1188cdf0e10cSrcweir 						}
1189cdf0e10cSrcweir 					}
1190cdf0e10cSrcweir 				}
1191cdf0e10cSrcweir 				else
1192cdf0e10cSrcweir 				{
1193cdf0e10cSrcweir 					for( nX = 0L; nX < nNewWidth; nX++ )
1194cdf0e10cSrcweir 					{
1195cdf0e10cSrcweir 						if( 1 == nHeight )
1196cdf0e10cSrcweir 						{
1197cdf0e10cSrcweir 							aCol0 = pReadAcc->GetPixel( 0, nX );
1198cdf0e10cSrcweir 
1199cdf0e10cSrcweir 							for( nY = 0L; nY < nNewHeight; nY++ )
1200cdf0e10cSrcweir 								pWriteAcc->SetPixel( nY, nX, aCol0 );
1201cdf0e10cSrcweir 						}
1202cdf0e10cSrcweir 						else
1203cdf0e10cSrcweir 						{
1204cdf0e10cSrcweir 							for( nY = 0L; nY < nNewHeight; nY++ )
1205cdf0e10cSrcweir 							{
1206cdf0e10cSrcweir 								nTemp = pLutInt[ nY ];
1207cdf0e10cSrcweir 
1208cdf0e10cSrcweir 								aCol0 = pReadAcc->GetPixel( nTemp++, nX );
1209cdf0e10cSrcweir 								aCol1 = pReadAcc->GetPixel( nTemp, nX );
1210cdf0e10cSrcweir 
1211cdf0e10cSrcweir 								nTemp = pLutFrac[ nY ];
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir 								lXR1 = aCol1.GetRed() - ( lXR0 = aCol0.GetRed() );
1214cdf0e10cSrcweir 								lXG1 = aCol1.GetGreen() - ( lXG0 = aCol0.GetGreen() );
1215cdf0e10cSrcweir 								lXB1 = aCol1.GetBlue() - ( lXB0 = aCol0.GetBlue() );
1216cdf0e10cSrcweir 
1217cdf0e10cSrcweir 								aCol0.SetRed( (sal_uInt8) ( ( lXR1 * nTemp + ( lXR0 << 10 ) ) >> 10 ) );
1218cdf0e10cSrcweir 								aCol0.SetGreen( (sal_uInt8) ( ( lXG1 * nTemp + ( lXG0 << 10 ) ) >> 10 ) );
1219cdf0e10cSrcweir 								aCol0.SetBlue( (sal_uInt8) ( ( lXB1 * nTemp + ( lXB0 << 10 ) ) >> 10 ) );
1220cdf0e10cSrcweir 
1221cdf0e10cSrcweir 								pWriteAcc->SetPixel( nY, nX, aCol0 );
1222cdf0e10cSrcweir 							}
1223cdf0e10cSrcweir 						}
1224cdf0e10cSrcweir 					}
1225cdf0e10cSrcweir 				}
1226cdf0e10cSrcweir 
1227cdf0e10cSrcweir 				delete[] pLutInt;
1228cdf0e10cSrcweir 				delete[] pLutFrac;
1229cdf0e10cSrcweir 				bRet = sal_True;
1230cdf0e10cSrcweir 			}
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir 			ReleaseAccess( pReadAcc );
1233cdf0e10cSrcweir 			aNewBmp.ReleaseAccess( pWriteAcc );
1234cdf0e10cSrcweir 
1235cdf0e10cSrcweir 			if( bRet )
1236cdf0e10cSrcweir 				ImplAssignWithSize( aNewBmp );
1237cdf0e10cSrcweir 		}
1238cdf0e10cSrcweir 	}
1239cdf0e10cSrcweir 
1240cdf0e10cSrcweir 	if( !bRet )
1241cdf0e10cSrcweir 		bRet = ImplScaleFast( rScaleX, rScaleY );
1242cdf0e10cSrcweir 
1243cdf0e10cSrcweir 	return bRet;
1244cdf0e10cSrcweir }
1245cdf0e10cSrcweir 
1246cdf0e10cSrcweir // ------------------------------------------------------------------------
1247cdf0e10cSrcweir 
1248cdf0e10cSrcweir sal_Bool Bitmap::Dither( sal_uLong nDitherFlags )
1249cdf0e10cSrcweir {
1250cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
1251cdf0e10cSrcweir 
1252cdf0e10cSrcweir 	const Size aSizePix( GetSizePixel() );
1253cdf0e10cSrcweir 
1254cdf0e10cSrcweir 	if( aSizePix.Width() == 1 || aSizePix.Height() == 1 )
1255cdf0e10cSrcweir 		bRet = sal_True;
1256cdf0e10cSrcweir 	else if( nDitherFlags & BMP_DITHER_MATRIX )
1257cdf0e10cSrcweir 		bRet = ImplDitherMatrix();
1258cdf0e10cSrcweir 	else if( nDitherFlags & BMP_DITHER_FLOYD )
1259cdf0e10cSrcweir 		bRet = ImplDitherFloyd();
1260cdf0e10cSrcweir 	else if( ( nDitherFlags & BMP_DITHER_FLOYD_16 ) && ( GetBitCount() == 24 ) )
1261cdf0e10cSrcweir 		bRet = ImplDitherFloyd16();
1262cdf0e10cSrcweir 
1263cdf0e10cSrcweir 	return bRet;
1264cdf0e10cSrcweir }
1265cdf0e10cSrcweir 
1266cdf0e10cSrcweir // ------------------------------------------------------------------------
1267cdf0e10cSrcweir 
1268cdf0e10cSrcweir sal_Bool Bitmap::ImplDitherMatrix()
1269cdf0e10cSrcweir {
1270cdf0e10cSrcweir 	BitmapReadAccess*	pReadAcc = AcquireReadAccess();
1271cdf0e10cSrcweir 	Bitmap				aNewBmp( GetSizePixel(), 8 );
1272cdf0e10cSrcweir 	BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
1273cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
1274cdf0e10cSrcweir 
1275cdf0e10cSrcweir 	if( pReadAcc && pWriteAcc )
1276cdf0e10cSrcweir 	{
1277cdf0e10cSrcweir 		const sal_uLong	nWidth = pReadAcc->Width();
1278cdf0e10cSrcweir 		const sal_uLong	nHeight = pReadAcc->Height();
1279cdf0e10cSrcweir 		BitmapColor	aIndex( (sal_uInt8) 0 );
1280cdf0e10cSrcweir 
1281cdf0e10cSrcweir 		if( pReadAcc->HasPalette() )
1282cdf0e10cSrcweir 		{
1283cdf0e10cSrcweir 			for( sal_uLong nY = 0UL; nY < nHeight; nY++ )
1284cdf0e10cSrcweir 			{
1285cdf0e10cSrcweir 				for( sal_uLong nX = 0UL, nModY = ( nY & 0x0FUL ) << 4UL; nX < nWidth; nX++ )
1286cdf0e10cSrcweir 				{
1287cdf0e10cSrcweir 					const BitmapColor	aCol( pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nY, nX ) ) );
1288cdf0e10cSrcweir 					const sal_uLong			nD = nVCLDitherLut[ nModY + ( nX & 0x0FUL ) ];
1289cdf0e10cSrcweir 					const sal_uLong			nR = ( nVCLLut[ aCol.GetRed() ] + nD ) >> 16UL;
1290cdf0e10cSrcweir 					const sal_uLong			nG = ( nVCLLut[ aCol.GetGreen() ] + nD ) >> 16UL;
1291cdf0e10cSrcweir 					const sal_uLong			nB = ( nVCLLut[ aCol.GetBlue() ] + nD ) >> 16UL;
1292cdf0e10cSrcweir 
1293cdf0e10cSrcweir 					aIndex.SetIndex( (sal_uInt8) ( nVCLRLut[ nR ] + nVCLGLut[ nG ] + nVCLBLut[ nB ] ) );
1294cdf0e10cSrcweir 					pWriteAcc->SetPixel( nY, nX, aIndex );
1295cdf0e10cSrcweir 				}
1296cdf0e10cSrcweir 			}
1297cdf0e10cSrcweir 		}
1298cdf0e10cSrcweir 		else
1299cdf0e10cSrcweir 		{
1300cdf0e10cSrcweir 			for( sal_uLong nY = 0UL; nY < nHeight; nY++ )
1301cdf0e10cSrcweir 			{
1302cdf0e10cSrcweir 				for( sal_uLong nX = 0UL, nModY = ( nY & 0x0FUL ) << 4UL; nX < nWidth; nX++ )
1303cdf0e10cSrcweir 				{
1304cdf0e10cSrcweir 					const BitmapColor	aCol( pReadAcc->GetPixel( nY, nX ) );
1305cdf0e10cSrcweir 					const sal_uLong			nD = nVCLDitherLut[ nModY + ( nX & 0x0FUL ) ];
1306cdf0e10cSrcweir 					const sal_uLong			nR = ( nVCLLut[ aCol.GetRed() ] + nD ) >> 16UL;
1307cdf0e10cSrcweir 					const sal_uLong			nG = ( nVCLLut[ aCol.GetGreen() ] + nD ) >> 16UL;
1308cdf0e10cSrcweir 					const sal_uLong			nB = ( nVCLLut[ aCol.GetBlue() ] + nD ) >> 16UL;
1309cdf0e10cSrcweir 
1310cdf0e10cSrcweir 					aIndex.SetIndex( (sal_uInt8) ( nVCLRLut[ nR ] + nVCLGLut[ nG ] + nVCLBLut[ nB ] ) );
1311cdf0e10cSrcweir 					pWriteAcc->SetPixel( nY, nX, aIndex );
1312cdf0e10cSrcweir 				}
1313cdf0e10cSrcweir 			}
1314cdf0e10cSrcweir 		}
1315cdf0e10cSrcweir 
1316cdf0e10cSrcweir 		bRet = sal_True;
1317cdf0e10cSrcweir 	}
1318cdf0e10cSrcweir 
1319cdf0e10cSrcweir 	ReleaseAccess( pReadAcc );
1320cdf0e10cSrcweir 	aNewBmp.ReleaseAccess( pWriteAcc );
1321cdf0e10cSrcweir 
1322cdf0e10cSrcweir 	if( bRet )
1323cdf0e10cSrcweir 	{
1324cdf0e10cSrcweir 		const MapMode	aMap( maPrefMapMode );
1325cdf0e10cSrcweir 		const Size		aSize( maPrefSize );
1326cdf0e10cSrcweir 
1327cdf0e10cSrcweir 		*this = aNewBmp;
1328cdf0e10cSrcweir 
1329cdf0e10cSrcweir 		maPrefMapMode = aMap;
1330cdf0e10cSrcweir 		maPrefSize = aSize;
1331cdf0e10cSrcweir 	}
1332cdf0e10cSrcweir 
1333cdf0e10cSrcweir 	return bRet;
1334cdf0e10cSrcweir }
1335cdf0e10cSrcweir 
1336cdf0e10cSrcweir // ------------------------------------------------------------------------
1337cdf0e10cSrcweir 
1338cdf0e10cSrcweir sal_Bool Bitmap::ImplDitherFloyd()
1339cdf0e10cSrcweir {
1340cdf0e10cSrcweir 	const Size	aSize( GetSizePixel() );
1341cdf0e10cSrcweir 	sal_Bool		bRet = sal_False;
1342cdf0e10cSrcweir 
1343cdf0e10cSrcweir 	if( ( aSize.Width() > 3 ) && ( aSize.Height() > 2 ) )
1344cdf0e10cSrcweir 	{
1345cdf0e10cSrcweir 		BitmapReadAccess*	pReadAcc = AcquireReadAccess();
1346cdf0e10cSrcweir 		Bitmap				aNewBmp( GetSizePixel(), 8 );
1347cdf0e10cSrcweir 		BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
1348cdf0e10cSrcweir 
1349cdf0e10cSrcweir 		if( pReadAcc && pWriteAcc )
1350cdf0e10cSrcweir 		{
1351cdf0e10cSrcweir 			BitmapColor	aColor;
1352cdf0e10cSrcweir 			long		nWidth = pReadAcc->Width();
1353cdf0e10cSrcweir 			long		nWidth1 = nWidth - 1L;
1354cdf0e10cSrcweir 			long		nHeight = pReadAcc->Height();
1355cdf0e10cSrcweir 			long		nX;
1356cdf0e10cSrcweir 			long		nW = nWidth * 3L;
1357cdf0e10cSrcweir 			long		nW2 = nW - 3L;
1358cdf0e10cSrcweir 			long		nRErr, nGErr, nBErr;
1359cdf0e10cSrcweir 			long		nRC, nGC, nBC;
1360cdf0e10cSrcweir 			long		nTemp;
1361cdf0e10cSrcweir 			long		nZ;
1362cdf0e10cSrcweir 			long*		p1 = new long[ nW ];
1363cdf0e10cSrcweir 			long*		p2 = new long[ nW ];
1364cdf0e10cSrcweir 			long*		p1T = p1;
1365cdf0e10cSrcweir 			long*		p2T = p2;
1366cdf0e10cSrcweir 			long*		pTmp;
1367cdf0e10cSrcweir 			sal_Bool		bPal = pReadAcc->HasPalette();
1368cdf0e10cSrcweir 
1369cdf0e10cSrcweir 			pTmp = p2T;
1370cdf0e10cSrcweir 
1371cdf0e10cSrcweir 			if( bPal )
1372cdf0e10cSrcweir 			{
1373cdf0e10cSrcweir 				for( nZ = 0; nZ < nWidth; nZ++ )
1374cdf0e10cSrcweir 				{
1375cdf0e10cSrcweir 					aColor = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( 0, nZ ) );
1376cdf0e10cSrcweir 
1377cdf0e10cSrcweir 					*pTmp++ = (long) aColor.GetBlue() << 12;
1378cdf0e10cSrcweir 					*pTmp++ = (long) aColor.GetGreen() << 12;
1379cdf0e10cSrcweir 					*pTmp++ = (long) aColor.GetRed() << 12;
1380cdf0e10cSrcweir 				}
1381cdf0e10cSrcweir 			}
1382cdf0e10cSrcweir 			else
1383cdf0e10cSrcweir 			{
1384cdf0e10cSrcweir 				for( nZ = 0; nZ < nWidth; nZ++ )
1385cdf0e10cSrcweir 				{
1386cdf0e10cSrcweir 					aColor = pReadAcc->GetPixel( 0, nZ );
1387cdf0e10cSrcweir 
1388cdf0e10cSrcweir 					*pTmp++ = (long) aColor.GetBlue() << 12;
1389cdf0e10cSrcweir 					*pTmp++ = (long) aColor.GetGreen() << 12;
1390cdf0e10cSrcweir 					*pTmp++ = (long) aColor.GetRed() << 12;
1391cdf0e10cSrcweir 				}
1392cdf0e10cSrcweir 			}
1393cdf0e10cSrcweir 
1394cdf0e10cSrcweir 			for( long nY = 1, nYAcc = 0L; nY <= nHeight; nY++, nYAcc++ )
1395cdf0e10cSrcweir 			{
1396cdf0e10cSrcweir 				pTmp = p1T;
1397cdf0e10cSrcweir 				p1T = p2T;
1398cdf0e10cSrcweir 				p2T = pTmp;
1399cdf0e10cSrcweir 
1400cdf0e10cSrcweir 				if( nY < nHeight )
1401cdf0e10cSrcweir 				{
1402cdf0e10cSrcweir 					if( bPal )
1403cdf0e10cSrcweir 					{
1404cdf0e10cSrcweir 						for( nZ = 0; nZ < nWidth; nZ++ )
1405cdf0e10cSrcweir 						{
1406cdf0e10cSrcweir 							aColor = pReadAcc->GetPaletteColor( pReadAcc->GetPixel( nY, nZ ) );
1407cdf0e10cSrcweir 
1408cdf0e10cSrcweir 							*pTmp++ = (long) aColor.GetBlue() << 12;
1409cdf0e10cSrcweir 							*pTmp++ = (long) aColor.GetGreen() << 12;
1410cdf0e10cSrcweir 							*pTmp++ = (long) aColor.GetRed() << 12;
1411cdf0e10cSrcweir 						}
1412cdf0e10cSrcweir 					}
1413cdf0e10cSrcweir 					else
1414cdf0e10cSrcweir 					{
1415cdf0e10cSrcweir 						for( nZ = 0; nZ < nWidth; nZ++ )
1416cdf0e10cSrcweir 						{
1417cdf0e10cSrcweir 							aColor = pReadAcc->GetPixel( nY, nZ );
1418cdf0e10cSrcweir 
1419cdf0e10cSrcweir 							*pTmp++ = (long) aColor.GetBlue() << 12;
1420cdf0e10cSrcweir 							*pTmp++ = (long) aColor.GetGreen() << 12;
1421cdf0e10cSrcweir 							*pTmp++ = (long) aColor.GetRed() << 12;
1422cdf0e10cSrcweir 						}
1423cdf0e10cSrcweir 					}
1424cdf0e10cSrcweir 				}
1425cdf0e10cSrcweir 
1426cdf0e10cSrcweir 				// erstes Pixel gesondert betrachten
1427cdf0e10cSrcweir 				nX = 0;
1428cdf0e10cSrcweir 				CALC_ERRORS;
1429cdf0e10cSrcweir 				CALC_TABLES7;
1430cdf0e10cSrcweir 				nX -= 5;
1431cdf0e10cSrcweir 				CALC_TABLES5;
1432cdf0e10cSrcweir 				pWriteAcc->SetPixel( nYAcc, 0, BitmapColor( (sal_uInt8) ( nVCLBLut[ nBC ] + nVCLGLut[nGC ] + nVCLRLut[nRC ] ) ) );
1433cdf0e10cSrcweir 
1434cdf0e10cSrcweir 				// mittlere Pixel ueber Schleife
1435cdf0e10cSrcweir 				long nXAcc;
1436cdf0e10cSrcweir 				for ( nX = 3L, nXAcc = 1L; nX < nW2; nXAcc++ )
1437cdf0e10cSrcweir 				{
1438cdf0e10cSrcweir 					CALC_ERRORS;
1439cdf0e10cSrcweir 					CALC_TABLES7;
1440cdf0e10cSrcweir 					nX -= 8;
1441cdf0e10cSrcweir 					CALC_TABLES3;
1442cdf0e10cSrcweir 					CALC_TABLES5;
1443cdf0e10cSrcweir 					pWriteAcc->SetPixel( nYAcc, nXAcc, BitmapColor( (sal_uInt8) ( nVCLBLut[ nBC ] + nVCLGLut[nGC ] + nVCLRLut[nRC ] ) ) );
1444cdf0e10cSrcweir 				}
1445cdf0e10cSrcweir 
1446cdf0e10cSrcweir 				// letztes Pixel gesondert betrachten
1447cdf0e10cSrcweir 				CALC_ERRORS;
1448cdf0e10cSrcweir 				nX -= 5;
1449cdf0e10cSrcweir 				CALC_TABLES3;
1450cdf0e10cSrcweir 				CALC_TABLES5;
1451cdf0e10cSrcweir 				pWriteAcc->SetPixel( nYAcc, nWidth1, BitmapColor( (sal_uInt8) ( nVCLBLut[ nBC ] + nVCLGLut[nGC ] + nVCLRLut[nRC ] ) ) );
1452cdf0e10cSrcweir 			}
1453cdf0e10cSrcweir 
1454cdf0e10cSrcweir 			delete[] p1;
1455cdf0e10cSrcweir 			delete[] p2;
1456cdf0e10cSrcweir 			bRet = sal_True;
1457cdf0e10cSrcweir 		}
1458cdf0e10cSrcweir 
1459cdf0e10cSrcweir 		ReleaseAccess( pReadAcc );
1460cdf0e10cSrcweir 		aNewBmp.ReleaseAccess( pWriteAcc );
1461cdf0e10cSrcweir 
1462cdf0e10cSrcweir 		if( bRet )
1463cdf0e10cSrcweir 		{
1464cdf0e10cSrcweir 			const MapMode	aMap( maPrefMapMode );
1465cdf0e10cSrcweir 			const Size		aPrefSize( maPrefSize );
1466cdf0e10cSrcweir 
1467cdf0e10cSrcweir 			*this = aNewBmp;
1468cdf0e10cSrcweir 
1469cdf0e10cSrcweir 			maPrefMapMode = aMap;
1470cdf0e10cSrcweir 			maPrefSize = aPrefSize;
1471cdf0e10cSrcweir 		}
1472cdf0e10cSrcweir 	}
1473cdf0e10cSrcweir 
1474cdf0e10cSrcweir 	return bRet;
1475cdf0e10cSrcweir }
1476cdf0e10cSrcweir 
1477cdf0e10cSrcweir // ------------------------------------------------------------------------
1478cdf0e10cSrcweir 
1479cdf0e10cSrcweir sal_Bool Bitmap::ImplDitherFloyd16()
1480cdf0e10cSrcweir {
1481cdf0e10cSrcweir 	BitmapReadAccess*	pReadAcc = AcquireReadAccess();
1482cdf0e10cSrcweir 	Bitmap				aNewBmp( GetSizePixel(), 24 );
1483cdf0e10cSrcweir 	BitmapWriteAccess*	pWriteAcc = aNewBmp.AcquireWriteAccess();
1484cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
1485cdf0e10cSrcweir 
1486cdf0e10cSrcweir 	if( pReadAcc && pWriteAcc )
1487cdf0e10cSrcweir 	{
1488cdf0e10cSrcweir 		const long		nWidth = pWriteAcc->Width();
1489cdf0e10cSrcweir 		const long		nWidth1 = nWidth - 1L;
1490cdf0e10cSrcweir 		const long		nHeight = pWriteAcc->Height();
1491cdf0e10cSrcweir 		BitmapColor 	aColor;
1492cdf0e10cSrcweir 		BitmapColor		aBestCol;
1493cdf0e10cSrcweir 		ImpErrorQuad	aErrQuad;
1494cdf0e10cSrcweir 		ImpErrorQuad*	pErrQuad1 = new ImpErrorQuad[ nWidth ];
1495cdf0e10cSrcweir 		ImpErrorQuad*	pErrQuad2 = new ImpErrorQuad[ nWidth ];
1496cdf0e10cSrcweir 		ImpErrorQuad*	pQLine1 = pErrQuad1;
1497cdf0e10cSrcweir 		ImpErrorQuad*	pQLine2 = 0;
1498cdf0e10cSrcweir 		long			nX, nY;
1499cdf0e10cSrcweir 		long			nYTmp = 0L;
1500cdf0e10cSrcweir 		sal_Bool			bQ1 = sal_True;
1501cdf0e10cSrcweir 
1502cdf0e10cSrcweir 		for( nY = 0L; nY < Min( nHeight, 2L ); nY++, nYTmp++ )
1503cdf0e10cSrcweir 			for( nX = 0L, pQLine2 = !nY ? pErrQuad1 : pErrQuad2; nX < nWidth; nX++ )
1504cdf0e10cSrcweir 				pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
1505cdf0e10cSrcweir 
1506cdf0e10cSrcweir 		for( nY = 0L; nY < nHeight; nY++, nYTmp++ )
1507cdf0e10cSrcweir 		{
1508cdf0e10cSrcweir 			// erstes ZeilenPixel
1509cdf0e10cSrcweir 			aBestCol = pQLine1[ 0 ].ImplGetColor();
1510cdf0e10cSrcweir 			aBestCol.SetRed( ( aBestCol.GetRed() & 248 ) | 7 );
1511cdf0e10cSrcweir 			aBestCol.SetGreen( ( aBestCol.GetGreen() & 248 ) | 7 );
1512cdf0e10cSrcweir 			aBestCol.SetBlue( ( aBestCol.GetBlue() & 248 ) | 7 );
1513cdf0e10cSrcweir 			pWriteAcc->SetPixel( nY, 0, aBestCol );
1514cdf0e10cSrcweir 
1515cdf0e10cSrcweir 			for( nX = 1L; nX < nWidth1; nX++ )
1516cdf0e10cSrcweir 			{
1517cdf0e10cSrcweir 				aColor = pQLine1[ nX ].ImplGetColor();
1518cdf0e10cSrcweir 				aBestCol.SetRed( ( aColor.GetRed() & 248 ) | 7 );
1519cdf0e10cSrcweir 				aBestCol.SetGreen( ( aColor.GetGreen() & 248 ) | 7 );
1520cdf0e10cSrcweir 				aBestCol.SetBlue( ( aColor.GetBlue() & 248 ) | 7 );
1521cdf0e10cSrcweir 				aErrQuad = ( ImpErrorQuad( aColor ) -= aBestCol );
1522cdf0e10cSrcweir 				pQLine1[ ++nX ].ImplAddColorError7( aErrQuad );
1523cdf0e10cSrcweir 				pQLine2[ nX-- ].ImplAddColorError1( aErrQuad );
1524cdf0e10cSrcweir 				pQLine2[ nX-- ].ImplAddColorError5( aErrQuad );
1525cdf0e10cSrcweir 				pQLine2[ nX++ ].ImplAddColorError3( aErrQuad );
1526cdf0e10cSrcweir 				pWriteAcc->SetPixel( nY, nX, aBestCol );
1527cdf0e10cSrcweir 			}
1528cdf0e10cSrcweir 
1529cdf0e10cSrcweir 			// letztes ZeilenPixel
1530cdf0e10cSrcweir 			aBestCol = pQLine1[ nWidth1 ].ImplGetColor();
1531cdf0e10cSrcweir 			aBestCol.SetRed( ( aBestCol.GetRed() & 248 ) | 7 );
1532cdf0e10cSrcweir 			aBestCol.SetGreen( ( aBestCol.GetGreen() & 248 ) | 7 );
1533cdf0e10cSrcweir 			aBestCol.SetBlue( ( aBestCol.GetBlue() & 248 ) | 7 );
1534cdf0e10cSrcweir 			pWriteAcc->SetPixel( nY, nX, aBestCol );
1535cdf0e10cSrcweir 
1536cdf0e10cSrcweir 			// Zeilenpuffer neu fuellen/kopieren
1537cdf0e10cSrcweir 			pQLine1 = pQLine2;
1538cdf0e10cSrcweir 			pQLine2 = ( bQ1 = !bQ1 ) != sal_False ? pErrQuad2 : pErrQuad1;
1539cdf0e10cSrcweir 
1540cdf0e10cSrcweir 			if( nYTmp < nHeight )
1541cdf0e10cSrcweir 				for( nX = 0L; nX < nWidth; nX++ )
1542cdf0e10cSrcweir 					pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
1543cdf0e10cSrcweir 		}
1544cdf0e10cSrcweir 
1545cdf0e10cSrcweir 		// Zeilenpuffer zerstoeren
1546cdf0e10cSrcweir 		delete[] pErrQuad1;
1547cdf0e10cSrcweir 		delete[] pErrQuad2;
1548cdf0e10cSrcweir 		bRet = sal_True;
1549cdf0e10cSrcweir 	}
1550cdf0e10cSrcweir 
1551cdf0e10cSrcweir 	ReleaseAccess( pReadAcc );
1552cdf0e10cSrcweir 	aNewBmp.ReleaseAccess( pWriteAcc );
1553cdf0e10cSrcweir 
1554cdf0e10cSrcweir 	if( bRet )
1555cdf0e10cSrcweir 	{
1556cdf0e10cSrcweir 		const MapMode	aMap( maPrefMapMode );
1557cdf0e10cSrcweir 		const Size		aSize( maPrefSize );
1558cdf0e10cSrcweir 
1559cdf0e10cSrcweir 		*this = aNewBmp;
1560cdf0e10cSrcweir 
1561cdf0e10cSrcweir 		maPrefMapMode = aMap;
1562cdf0e10cSrcweir 		maPrefSize = aSize;
1563cdf0e10cSrcweir 	}
1564cdf0e10cSrcweir 
1565cdf0e10cSrcweir 	return bRet;
1566cdf0e10cSrcweir }
1567cdf0e10cSrcweir 
1568cdf0e10cSrcweir // ------------------------------------------------------------------------
1569cdf0e10cSrcweir 
1570cdf0e10cSrcweir sal_Bool Bitmap::ReduceColors( sal_uInt16 nColorCount, BmpReduce eReduce )
1571cdf0e10cSrcweir {
1572cdf0e10cSrcweir 	sal_Bool bRet;
1573cdf0e10cSrcweir 
1574cdf0e10cSrcweir 	if( GetColorCount() <= (sal_uLong) nColorCount )
1575cdf0e10cSrcweir 		bRet = sal_True;
1576cdf0e10cSrcweir 	else if( nColorCount )
1577cdf0e10cSrcweir 	{
1578cdf0e10cSrcweir 		if( BMP_REDUCE_SIMPLE == eReduce )
1579cdf0e10cSrcweir 			bRet = ImplReduceSimple( nColorCount );
1580cdf0e10cSrcweir 		else if( BMP_REDUCE_POPULAR == eReduce )
1581cdf0e10cSrcweir 			bRet = ImplReducePopular( nColorCount );
1582cdf0e10cSrcweir 		else
1583cdf0e10cSrcweir 			bRet = ImplReduceMedian( nColorCount );
1584cdf0e10cSrcweir 	}
1585cdf0e10cSrcweir 	else
1586cdf0e10cSrcweir 		bRet = sal_False;
1587cdf0e10cSrcweir 
1588cdf0e10cSrcweir 	return bRet;
1589cdf0e10cSrcweir }
1590cdf0e10cSrcweir 
1591cdf0e10cSrcweir // ------------------------------------------------------------------------
1592cdf0e10cSrcweir 
1593cdf0e10cSrcweir sal_Bool Bitmap::ImplReduceSimple( sal_uInt16 nColorCount )
1594cdf0e10cSrcweir {
1595cdf0e10cSrcweir 	Bitmap				aNewBmp;
1596cdf0e10cSrcweir 	BitmapReadAccess*	pRAcc = AcquireReadAccess();
1597cdf0e10cSrcweir 	const sal_uInt16		nColCount = Min( nColorCount, (sal_uInt16) 256 );
1598cdf0e10cSrcweir 	sal_uInt16				nBitCount;
1599cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
1600cdf0e10cSrcweir 
1601cdf0e10cSrcweir 	if( nColCount <= 2 )
1602cdf0e10cSrcweir 		nBitCount = 1;
1603cdf0e10cSrcweir 	else if( nColCount <= 16 )
1604cdf0e10cSrcweir 		nBitCount = 4;
1605cdf0e10cSrcweir 	else
1606cdf0e10cSrcweir 		nBitCount = 8;
1607cdf0e10cSrcweir 
1608cdf0e10cSrcweir 	if( pRAcc )
1609cdf0e10cSrcweir 	{
1610cdf0e10cSrcweir 		Octree					aOct( *pRAcc, nColCount );
1611cdf0e10cSrcweir 		const BitmapPalette&	rPal = aOct.GetPalette();
1612cdf0e10cSrcweir 		BitmapWriteAccess*		pWAcc;
1613cdf0e10cSrcweir 
1614cdf0e10cSrcweir 		aNewBmp = Bitmap( GetSizePixel(), nBitCount, &rPal );
1615cdf0e10cSrcweir 		pWAcc = aNewBmp.AcquireWriteAccess();
1616cdf0e10cSrcweir 
1617cdf0e10cSrcweir 		if( pWAcc )
1618cdf0e10cSrcweir 		{
1619cdf0e10cSrcweir 			const long nWidth = pRAcc->Width();
1620cdf0e10cSrcweir 			const long nHeight = pRAcc->Height();
1621cdf0e10cSrcweir 
1622cdf0e10cSrcweir 			if( pRAcc->HasPalette() )
1623cdf0e10cSrcweir 			{
1624cdf0e10cSrcweir 				for( long nY = 0L; nY < nHeight; nY++ )
1625cdf0e10cSrcweir 					for( long nX =0L; nX < nWidth; nX++ )
1626cdf0e10cSrcweir 						pWAcc->SetPixel( nY, nX, (sal_uInt8) aOct.GetBestPaletteIndex( pRAcc->GetPaletteColor( pRAcc->GetPixel( nY, nX ) ) ) );
1627cdf0e10cSrcweir 			}
1628cdf0e10cSrcweir 			else
1629cdf0e10cSrcweir 			{
1630cdf0e10cSrcweir 				for( long nY = 0L; nY < nHeight; nY++ )
1631cdf0e10cSrcweir 					for( long nX =0L; nX < nWidth; nX++ )
1632cdf0e10cSrcweir 						pWAcc->SetPixel( nY, nX, (sal_uInt8) aOct.GetBestPaletteIndex( pRAcc->GetPixel( nY, nX ) ) );
1633cdf0e10cSrcweir 			}
1634cdf0e10cSrcweir 
1635cdf0e10cSrcweir 			aNewBmp.ReleaseAccess( pWAcc );
1636cdf0e10cSrcweir 			bRet = sal_True;
1637cdf0e10cSrcweir 		}
1638cdf0e10cSrcweir 
1639cdf0e10cSrcweir 		ReleaseAccess( pRAcc );
1640cdf0e10cSrcweir 	}
1641cdf0e10cSrcweir 
1642cdf0e10cSrcweir 	if( bRet )
1643cdf0e10cSrcweir 	{
1644cdf0e10cSrcweir 		const MapMode	aMap( maPrefMapMode );
1645cdf0e10cSrcweir 		const Size		aSize( maPrefSize );
1646cdf0e10cSrcweir 
1647cdf0e10cSrcweir 		*this = aNewBmp;
1648cdf0e10cSrcweir 		maPrefMapMode = aMap;
1649cdf0e10cSrcweir 		maPrefSize = aSize;
1650cdf0e10cSrcweir 	}
1651cdf0e10cSrcweir 
1652cdf0e10cSrcweir 	return bRet;
1653cdf0e10cSrcweir }
1654cdf0e10cSrcweir 
1655cdf0e10cSrcweir // ------------------------------------------------------------------------
1656cdf0e10cSrcweir 
1657cdf0e10cSrcweir struct PopularColorCount
1658cdf0e10cSrcweir {
1659cdf0e10cSrcweir 	sal_uInt32	mnIndex;
1660cdf0e10cSrcweir 	sal_uInt32	mnCount;
1661cdf0e10cSrcweir };
1662cdf0e10cSrcweir 
1663cdf0e10cSrcweir // ------------------------------------------------------------------------
1664cdf0e10cSrcweir 
1665cdf0e10cSrcweir extern "C" int __LOADONCALLAPI ImplPopularCmpFnc( const void* p1, const void* p2 )
1666cdf0e10cSrcweir {
1667cdf0e10cSrcweir 	int nRet;
1668cdf0e10cSrcweir 
1669cdf0e10cSrcweir 	if( ( (PopularColorCount*) p1 )->mnCount < ( (PopularColorCount*) p2 )->mnCount )
1670cdf0e10cSrcweir 		nRet = 1;
1671cdf0e10cSrcweir 	else if( ( (PopularColorCount*) p1 )->mnCount == ( (PopularColorCount*) p2 )->mnCount )
1672cdf0e10cSrcweir 		nRet = 0;
1673cdf0e10cSrcweir 	else
1674cdf0e10cSrcweir 		nRet = -1;
1675cdf0e10cSrcweir 
1676cdf0e10cSrcweir 	return nRet;
1677cdf0e10cSrcweir }
1678cdf0e10cSrcweir 
1679cdf0e10cSrcweir // ------------------------------------------------------------------------
1680cdf0e10cSrcweir 
1681cdf0e10cSrcweir sal_Bool Bitmap::ImplReducePopular( sal_uInt16 nColCount )
1682cdf0e10cSrcweir {
1683cdf0e10cSrcweir 	BitmapReadAccess*	pRAcc = AcquireReadAccess();
1684cdf0e10cSrcweir 	sal_uInt16				nBitCount;
1685cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
1686cdf0e10cSrcweir 
1687cdf0e10cSrcweir 	if( nColCount > 256 )
1688cdf0e10cSrcweir 		nColCount = 256;
1689cdf0e10cSrcweir 
1690cdf0e10cSrcweir 	if( nColCount < 17 )
1691cdf0e10cSrcweir 		nBitCount = 4;
1692cdf0e10cSrcweir 	else
1693cdf0e10cSrcweir 		nBitCount = 8;
1694cdf0e10cSrcweir 
1695cdf0e10cSrcweir 	if( pRAcc )
1696cdf0e10cSrcweir 	{
1697cdf0e10cSrcweir 		const sal_uInt32	nValidBits = 4;
1698cdf0e10cSrcweir 		const sal_uInt32	nRightShiftBits = 8 - nValidBits;
1699cdf0e10cSrcweir 		const sal_uInt32	nLeftShiftBits1 = nValidBits;
1700cdf0e10cSrcweir 		const sal_uInt32	nLeftShiftBits2 = nValidBits << 1;
1701cdf0e10cSrcweir 		const sal_uInt32	nColorsPerComponent = 1 << nValidBits;
1702cdf0e10cSrcweir 		const sal_uInt32	nColorOffset = 256 / nColorsPerComponent;
1703cdf0e10cSrcweir 		const sal_uInt32	nTotalColors = nColorsPerComponent * nColorsPerComponent * nColorsPerComponent;
1704cdf0e10cSrcweir 		const long			nWidth = pRAcc->Width();
1705cdf0e10cSrcweir 		const long			nHeight = pRAcc->Height();
1706cdf0e10cSrcweir 		PopularColorCount*	pCountTable = new PopularColorCount[ nTotalColors ];
1707cdf0e10cSrcweir 		long				nX, nY, nR, nG, nB, nIndex;
1708cdf0e10cSrcweir 
1709cdf0e10cSrcweir 		rtl_zeroMemory( pCountTable, nTotalColors * sizeof( PopularColorCount ) );
1710cdf0e10cSrcweir 
1711cdf0e10cSrcweir 		for( nR = 0, nIndex = 0; nR < 256; nR += nColorOffset )
1712cdf0e10cSrcweir         {
1713cdf0e10cSrcweir 			for( nG = 0; nG < 256; nG += nColorOffset )
1714cdf0e10cSrcweir             {
1715cdf0e10cSrcweir 				for( nB = 0; nB < 256; nB += nColorOffset )
1716cdf0e10cSrcweir                 {
1717cdf0e10cSrcweir 					pCountTable[ nIndex ].mnIndex = nIndex;
1718cdf0e10cSrcweir                     nIndex++;
1719cdf0e10cSrcweir                 }
1720cdf0e10cSrcweir             }
1721cdf0e10cSrcweir         }
1722cdf0e10cSrcweir 
1723cdf0e10cSrcweir 		if( pRAcc->HasPalette() )
1724cdf0e10cSrcweir 		{
1725cdf0e10cSrcweir 			for( nY = 0L; nY < nHeight; nY++ )
1726cdf0e10cSrcweir 			{
1727cdf0e10cSrcweir 				for( nX = 0L; nX < nWidth; nX++ )
1728cdf0e10cSrcweir 				{
1729cdf0e10cSrcweir 					const BitmapColor& rCol = pRAcc->GetPaletteColor( pRAcc->GetPixel( nY, nX ) );
1730cdf0e10cSrcweir 					pCountTable[ ( ( ( (sal_uInt32) rCol.GetRed() ) >> nRightShiftBits ) << nLeftShiftBits2 ) |
1731cdf0e10cSrcweir 								 ( ( ( (sal_uInt32) rCol.GetGreen() ) >> nRightShiftBits ) << nLeftShiftBits1 ) |
1732cdf0e10cSrcweir 								 ( ( (sal_uInt32) rCol.GetBlue() ) >> nRightShiftBits ) ].mnCount++;
1733cdf0e10cSrcweir 				}
1734cdf0e10cSrcweir 			}
1735cdf0e10cSrcweir 		}
1736cdf0e10cSrcweir 		else
1737cdf0e10cSrcweir 		{
1738cdf0e10cSrcweir 			for( nY = 0L; nY < nHeight; nY++ )
1739cdf0e10cSrcweir 			{
1740cdf0e10cSrcweir 				for( nX = 0L; nX < nWidth; nX++ )
1741cdf0e10cSrcweir 				{
1742cdf0e10cSrcweir 					const BitmapColor aCol( pRAcc->GetPixel( nY, nX ) );
1743cdf0e10cSrcweir 					pCountTable[ ( ( ( (sal_uInt32) aCol.GetRed() ) >> nRightShiftBits ) << nLeftShiftBits2 ) |
1744cdf0e10cSrcweir 								 ( ( ( (sal_uInt32) aCol.GetGreen() ) >> nRightShiftBits ) << nLeftShiftBits1 ) |
1745cdf0e10cSrcweir 								 ( ( (sal_uInt32) aCol.GetBlue() ) >> nRightShiftBits ) ].mnCount++;
1746cdf0e10cSrcweir 				}
1747cdf0e10cSrcweir 			}
1748cdf0e10cSrcweir 		}
1749cdf0e10cSrcweir 
1750cdf0e10cSrcweir 		BitmapPalette aNewPal( nColCount );
1751cdf0e10cSrcweir 
1752cdf0e10cSrcweir 		qsort( pCountTable, nTotalColors, sizeof( PopularColorCount ), ImplPopularCmpFnc );
1753cdf0e10cSrcweir 
1754cdf0e10cSrcweir 		for( sal_uInt16 n = 0; n < nColCount; n++ )
1755cdf0e10cSrcweir 		{
1756cdf0e10cSrcweir 			const PopularColorCount& rPop = pCountTable[ n ];
1757cdf0e10cSrcweir 			aNewPal[ n ] = BitmapColor( (sal_uInt8) ( ( rPop.mnIndex >> nLeftShiftBits2 ) << nRightShiftBits ),
1758cdf0e10cSrcweir 										(sal_uInt8) ( ( ( rPop.mnIndex >> nLeftShiftBits1 ) & ( nColorsPerComponent - 1 ) ) << nRightShiftBits ),
1759cdf0e10cSrcweir 										(sal_uInt8) ( ( rPop.mnIndex & ( nColorsPerComponent - 1 ) ) << nRightShiftBits ) );
1760cdf0e10cSrcweir 		}
1761cdf0e10cSrcweir 
1762cdf0e10cSrcweir 		Bitmap				aNewBmp( GetSizePixel(), nBitCount, &aNewPal );
1763cdf0e10cSrcweir 		BitmapWriteAccess*	pWAcc = aNewBmp.AcquireWriteAccess();
1764cdf0e10cSrcweir 
1765cdf0e10cSrcweir 		if( pWAcc )
1766cdf0e10cSrcweir 		{
1767cdf0e10cSrcweir 			BitmapColor	aDstCol( (sal_uInt8) 0 );
1768cdf0e10cSrcweir 			sal_uInt8*		pIndexMap = new sal_uInt8[ nTotalColors ];
1769cdf0e10cSrcweir 
1770cdf0e10cSrcweir 			for( nR = 0, nIndex = 0; nR < 256; nR += nColorOffset )
1771cdf0e10cSrcweir 				for( nG = 0; nG < 256; nG += nColorOffset )
1772cdf0e10cSrcweir 					for( nB = 0; nB < 256; nB += nColorOffset )
1773cdf0e10cSrcweir 						pIndexMap[ nIndex++ ] = (sal_uInt8) aNewPal.GetBestIndex( BitmapColor( (sal_uInt8) nR, (sal_uInt8) nG, (sal_uInt8) nB ) );
1774cdf0e10cSrcweir 
1775cdf0e10cSrcweir 			if( pRAcc->HasPalette() )
1776cdf0e10cSrcweir 			{
1777cdf0e10cSrcweir 				for( nY = 0L; nY < nHeight; nY++ )
1778cdf0e10cSrcweir 				{
1779cdf0e10cSrcweir 					for( nX = 0L; nX < nWidth; nX++ )
1780cdf0e10cSrcweir 					{
1781cdf0e10cSrcweir 						const BitmapColor& rCol = pRAcc->GetPaletteColor( pRAcc->GetPixel( nY, nX ) );
1782cdf0e10cSrcweir 						aDstCol.SetIndex( pIndexMap[ ( ( ( (sal_uInt32) rCol.GetRed() ) >> nRightShiftBits ) << nLeftShiftBits2 ) |
1783cdf0e10cSrcweir 													 ( ( ( (sal_uInt32) rCol.GetGreen() ) >> nRightShiftBits ) << nLeftShiftBits1 ) |
1784cdf0e10cSrcweir 													 ( ( (sal_uInt32) rCol.GetBlue() ) >> nRightShiftBits ) ] );
1785cdf0e10cSrcweir 						pWAcc->SetPixel( nY, nX, aDstCol );
1786cdf0e10cSrcweir 					}
1787cdf0e10cSrcweir 				}
1788cdf0e10cSrcweir 			}
1789cdf0e10cSrcweir 			else
1790cdf0e10cSrcweir 			{
1791cdf0e10cSrcweir 				for( nY = 0L; nY < nHeight; nY++ )
1792cdf0e10cSrcweir 				{
1793cdf0e10cSrcweir 					for( nX = 0L; nX < nWidth; nX++ )
1794cdf0e10cSrcweir 					{
1795cdf0e10cSrcweir 						const BitmapColor aCol( pRAcc->GetPixel( nY, nX ) );
1796cdf0e10cSrcweir 						aDstCol.SetIndex( pIndexMap[ ( ( ( (sal_uInt32) aCol.GetRed() ) >> nRightShiftBits ) << nLeftShiftBits2 ) |
1797cdf0e10cSrcweir 													 ( ( ( (sal_uInt32) aCol.GetGreen() ) >> nRightShiftBits ) << nLeftShiftBits1 ) |
1798cdf0e10cSrcweir 													 ( ( (sal_uInt32) aCol.GetBlue() ) >> nRightShiftBits ) ] );
1799cdf0e10cSrcweir 						pWAcc->SetPixel( nY, nX, aDstCol );
1800cdf0e10cSrcweir 					}
1801cdf0e10cSrcweir 				}
1802cdf0e10cSrcweir 			}
1803cdf0e10cSrcweir 
1804cdf0e10cSrcweir 			delete[] pIndexMap;
1805cdf0e10cSrcweir 			aNewBmp.ReleaseAccess( pWAcc );
1806cdf0e10cSrcweir 			bRet = sal_True;
1807cdf0e10cSrcweir 		}
1808cdf0e10cSrcweir 
1809cdf0e10cSrcweir 		delete[] pCountTable;
1810cdf0e10cSrcweir 		ReleaseAccess( pRAcc );
1811cdf0e10cSrcweir 
1812cdf0e10cSrcweir 		if( bRet )
1813cdf0e10cSrcweir 		{
1814cdf0e10cSrcweir 			const MapMode	aMap( maPrefMapMode );
1815cdf0e10cSrcweir 			const Size		aSize( maPrefSize );
1816cdf0e10cSrcweir 
1817cdf0e10cSrcweir 			*this = aNewBmp;
1818cdf0e10cSrcweir 			maPrefMapMode = aMap;
1819cdf0e10cSrcweir 			maPrefSize = aSize;
1820cdf0e10cSrcweir 		}
1821cdf0e10cSrcweir 	}
1822cdf0e10cSrcweir 
1823cdf0e10cSrcweir 	return bRet;
1824cdf0e10cSrcweir }
1825cdf0e10cSrcweir 
1826cdf0e10cSrcweir // ------------------------------------------------------------------------
1827cdf0e10cSrcweir 
1828cdf0e10cSrcweir sal_Bool Bitmap::ImplReduceMedian( sal_uInt16 nColCount )
1829cdf0e10cSrcweir {
1830cdf0e10cSrcweir 	BitmapReadAccess*	pRAcc = AcquireReadAccess();
1831cdf0e10cSrcweir 	sal_uInt16				nBitCount;
1832cdf0e10cSrcweir 	sal_Bool				bRet = sal_False;
1833cdf0e10cSrcweir 
1834cdf0e10cSrcweir 	if( nColCount < 17 )
1835cdf0e10cSrcweir 		nBitCount = 4;
1836cdf0e10cSrcweir 	else if( nColCount < 257 )
1837cdf0e10cSrcweir 		nBitCount = 8;
1838cdf0e10cSrcweir 	else
1839cdf0e10cSrcweir 	{
1840cdf0e10cSrcweir 		DBG_ERROR( "Bitmap::ImplReduceMedian(): invalid color count!" );
1841cdf0e10cSrcweir 		nBitCount = 8;
1842cdf0e10cSrcweir 		nColCount = 256;
1843cdf0e10cSrcweir 	}
1844cdf0e10cSrcweir 
1845cdf0e10cSrcweir 	if( pRAcc )
1846cdf0e10cSrcweir 	{
1847cdf0e10cSrcweir 		Bitmap				aNewBmp( GetSizePixel(), nBitCount );
1848cdf0e10cSrcweir 		BitmapWriteAccess*	pWAcc = aNewBmp.AcquireWriteAccess();
1849cdf0e10cSrcweir 
1850cdf0e10cSrcweir 		if( pWAcc )
1851cdf0e10cSrcweir 		{
1852cdf0e10cSrcweir 			const sal_uLong	nSize = 32768UL * sizeof( sal_uLong );
1853cdf0e10cSrcweir 			sal_uLong*		pColBuf = (sal_uLong*) rtl_allocateMemory( nSize );
1854cdf0e10cSrcweir 			const long	nWidth = pWAcc->Width();
1855cdf0e10cSrcweir 			const long	nHeight = pWAcc->Height();
1856cdf0e10cSrcweir 			long		nIndex = 0L;
1857cdf0e10cSrcweir 
1858cdf0e10cSrcweir 			memset( (HPBYTE) pColBuf, 0, nSize );
1859cdf0e10cSrcweir 
1860cdf0e10cSrcweir 			// create Buffer
1861cdf0e10cSrcweir 			if( pRAcc->HasPalette() )
1862cdf0e10cSrcweir 			{
1863cdf0e10cSrcweir 				for( long nY = 0L; nY < nHeight; nY++ )
1864cdf0e10cSrcweir 				{
1865cdf0e10cSrcweir 					for( long nX = 0L; nX < nWidth; nX++ )
1866cdf0e10cSrcweir 					{
1867cdf0e10cSrcweir 						const BitmapColor& rCol = pRAcc->GetPaletteColor( pRAcc->GetPixel( nY, nX ) );
1868cdf0e10cSrcweir 						pColBuf[ RGB15( rCol.GetRed() >> 3, rCol.GetGreen() >> 3, rCol.GetBlue() >> 3 ) ]++;
1869cdf0e10cSrcweir 					}
1870cdf0e10cSrcweir 				}
1871cdf0e10cSrcweir 			}
1872cdf0e10cSrcweir 			else
1873cdf0e10cSrcweir 			{
1874cdf0e10cSrcweir 				for( long nY = 0L; nY < nHeight; nY++ )
1875cdf0e10cSrcweir 				{
1876cdf0e10cSrcweir 					for( long nX = 0L; nX < nWidth; nX++ )
1877cdf0e10cSrcweir 					{
1878cdf0e10cSrcweir 						const BitmapColor aCol( pRAcc->GetPixel( nY, nX ) );
1879cdf0e10cSrcweir 						pColBuf[ RGB15( aCol.GetRed() >> 3, aCol.GetGreen() >> 3, aCol.GetBlue() >> 3 ) ]++;
1880cdf0e10cSrcweir 					}
1881cdf0e10cSrcweir 				}
1882cdf0e10cSrcweir 			}
1883cdf0e10cSrcweir 
1884cdf0e10cSrcweir 			// create palette via median cut
1885cdf0e10cSrcweir 			BitmapPalette aPal( pWAcc->GetPaletteEntryCount() );
1886cdf0e10cSrcweir 			ImplMedianCut( pColBuf, aPal, 0, 31, 0, 31, 0, 31,
1887cdf0e10cSrcweir 						   nColCount, nWidth * nHeight, nIndex );
1888cdf0e10cSrcweir 
1889cdf0e10cSrcweir 			// do mapping of colors to palette
1890cdf0e10cSrcweir 			InverseColorMap aMap( aPal );
1891cdf0e10cSrcweir 			pWAcc->SetPalette( aPal );
1892cdf0e10cSrcweir 			for( long nY = 0L; nY < nHeight; nY++ )
1893cdf0e10cSrcweir 				for( long nX = 0L; nX < nWidth; nX++ )
1894cdf0e10cSrcweir 					pWAcc->SetPixel( nY, nX, (sal_uInt8) aMap.GetBestPaletteIndex( pRAcc->GetColor( nY, nX ) ) );
1895cdf0e10cSrcweir 
1896cdf0e10cSrcweir 			rtl_freeMemory( pColBuf );
1897cdf0e10cSrcweir 			aNewBmp.ReleaseAccess( pWAcc );
1898cdf0e10cSrcweir 			bRet = sal_True;
1899cdf0e10cSrcweir 		}
1900cdf0e10cSrcweir 
1901cdf0e10cSrcweir 		ReleaseAccess( pRAcc );
1902cdf0e10cSrcweir 
1903cdf0e10cSrcweir 		if( bRet )
1904cdf0e10cSrcweir 		{
1905cdf0e10cSrcweir 			const MapMode	aMap( maPrefMapMode );
1906cdf0e10cSrcweir 			const Size		aSize( maPrefSize );
1907cdf0e10cSrcweir 
1908cdf0e10cSrcweir 			*this = aNewBmp;
1909cdf0e10cSrcweir 			maPrefMapMode = aMap;
1910cdf0e10cSrcweir 			maPrefSize = aSize;
1911cdf0e10cSrcweir 		}
1912cdf0e10cSrcweir 	}
1913cdf0e10cSrcweir 
1914cdf0e10cSrcweir 	return bRet;
1915cdf0e10cSrcweir }
1916cdf0e10cSrcweir 
1917cdf0e10cSrcweir // ------------------------------------------------------------------------
1918cdf0e10cSrcweir 
1919cdf0e10cSrcweir void Bitmap::ImplMedianCut( sal_uLong* pColBuf, BitmapPalette& rPal,
1920cdf0e10cSrcweir 							long nR1, long nR2, long nG1, long nG2, long nB1, long nB2,
1921cdf0e10cSrcweir 							long nColors, long nPixels, long& rIndex )
1922cdf0e10cSrcweir {
1923cdf0e10cSrcweir 	if( !nPixels )
1924cdf0e10cSrcweir 		return;
1925cdf0e10cSrcweir 
1926cdf0e10cSrcweir 	BitmapColor	aCol;
1927cdf0e10cSrcweir 	const long	nRLen = nR2 - nR1;
1928cdf0e10cSrcweir 	const long	nGLen = nG2 - nG1;
1929cdf0e10cSrcweir 	const long	nBLen = nB2 - nB1;
1930cdf0e10cSrcweir 	long 		nR, nG, nB;
1931cdf0e10cSrcweir 	sal_uLong*		pBuf = pColBuf;
1932cdf0e10cSrcweir 
1933cdf0e10cSrcweir 	if( !nRLen && !nGLen && !nBLen )
1934cdf0e10cSrcweir 	{
1935cdf0e10cSrcweir 		if( pBuf[ RGB15( nR1, nG1, nB1 ) ] )
1936cdf0e10cSrcweir 		{
1937cdf0e10cSrcweir 			aCol.SetRed( (sal_uInt8) ( nR1 << 3 ) );
1938cdf0e10cSrcweir 			aCol.SetGreen( (sal_uInt8) ( nG1 << 3 ) );
1939cdf0e10cSrcweir 			aCol.SetBlue( (sal_uInt8) ( nB1 << 3 ) );
1940cdf0e10cSrcweir 			rPal[ (sal_uInt16) rIndex++ ] = aCol;
1941cdf0e10cSrcweir 		}
1942cdf0e10cSrcweir 	}
1943cdf0e10cSrcweir 	else
1944cdf0e10cSrcweir 	{
1945cdf0e10cSrcweir 		if( 1 == nColors || 1 == nPixels )
1946cdf0e10cSrcweir 		{
1947cdf0e10cSrcweir 			long nPixSum = 0, nRSum = 0, nGSum = 0, nBSum = 0;
1948cdf0e10cSrcweir 
1949cdf0e10cSrcweir 			for( nR = nR1; nR <= nR2; nR++ )
1950cdf0e10cSrcweir 			{
1951cdf0e10cSrcweir 				for( nG = nG1; nG <= nG2; nG++ )
1952cdf0e10cSrcweir 				{
1953cdf0e10cSrcweir 					for( nB = nB1; nB <= nB2; nB++ )
1954cdf0e10cSrcweir 					{
1955cdf0e10cSrcweir 						nPixSum = pBuf[ RGB15( nR, nG, nB ) ];
1956cdf0e10cSrcweir 
1957cdf0e10cSrcweir 						if( nPixSum )
1958cdf0e10cSrcweir 						{
1959cdf0e10cSrcweir 							nRSum += nR * nPixSum;
1960cdf0e10cSrcweir 							nGSum += nG * nPixSum;
1961cdf0e10cSrcweir 							nBSum += nB * nPixSum;
1962cdf0e10cSrcweir 						}
1963cdf0e10cSrcweir 					}
1964cdf0e10cSrcweir 				}
1965cdf0e10cSrcweir 			}
1966cdf0e10cSrcweir 
1967cdf0e10cSrcweir 			aCol.SetRed( (sal_uInt8) ( ( nRSum / nPixels ) << 3 ) );
1968cdf0e10cSrcweir 			aCol.SetGreen( (sal_uInt8) ( ( nGSum / nPixels ) << 3 ) );
1969cdf0e10cSrcweir 			aCol.SetBlue( (sal_uInt8) ( ( nBSum / nPixels ) << 3 ) );
1970cdf0e10cSrcweir 			rPal[ (sal_uInt16) rIndex++ ] = aCol;
1971cdf0e10cSrcweir 		}
1972cdf0e10cSrcweir 		else
1973cdf0e10cSrcweir 		{
1974cdf0e10cSrcweir 			const long	nTest = ( nPixels >> 1 );
1975cdf0e10cSrcweir 			long		nPixOld = 0;
1976cdf0e10cSrcweir 			long		nPixNew = 0;
1977cdf0e10cSrcweir 
1978cdf0e10cSrcweir 			if( nBLen > nGLen && nBLen > nRLen )
1979cdf0e10cSrcweir 			{
1980cdf0e10cSrcweir 				nB = nB1 - 1;
1981cdf0e10cSrcweir 
1982cdf0e10cSrcweir 				while( nPixNew < nTest )
1983cdf0e10cSrcweir 				{
1984cdf0e10cSrcweir 					nB++, nPixOld = nPixNew;
1985cdf0e10cSrcweir 					for( nR = nR1; nR <= nR2; nR++ )
1986cdf0e10cSrcweir 						for( nG = nG1; nG <= nG2; nG++ )
1987cdf0e10cSrcweir 							nPixNew += pBuf[ RGB15( nR, nG, nB ) ];
1988cdf0e10cSrcweir 				}
1989cdf0e10cSrcweir 
1990cdf0e10cSrcweir 				if( nB < nB2 )
1991cdf0e10cSrcweir 				{
1992cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG2, nB1, nB, nColors >> 1, nPixNew, rIndex );
1993cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG2, nB + 1, nB2, nColors >> 1, nPixels - nPixNew, rIndex );
1994cdf0e10cSrcweir 				}
1995cdf0e10cSrcweir 				else
1996cdf0e10cSrcweir 				{
1997cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG2, nB1, nB - 1, nColors >> 1, nPixOld, rIndex );
1998cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG2, nB, nB2, nColors >> 1, nPixels - nPixOld, rIndex );
1999cdf0e10cSrcweir 				}
2000cdf0e10cSrcweir 			}
2001cdf0e10cSrcweir 			else if( nGLen > nRLen )
2002cdf0e10cSrcweir 			{
2003cdf0e10cSrcweir 				nG = nG1 - 1;
2004cdf0e10cSrcweir 
2005cdf0e10cSrcweir 				while( nPixNew < nTest )
2006cdf0e10cSrcweir 				{
2007cdf0e10cSrcweir 					nG++, nPixOld = nPixNew;
2008cdf0e10cSrcweir 					for( nR = nR1; nR <= nR2; nR++ )
2009cdf0e10cSrcweir 						for( nB = nB1; nB <= nB2; nB++ )
2010cdf0e10cSrcweir 							nPixNew += pBuf[ RGB15( nR, nG, nB ) ];
2011cdf0e10cSrcweir 				}
2012cdf0e10cSrcweir 
2013cdf0e10cSrcweir 				if( nG < nG2 )
2014cdf0e10cSrcweir 				{
2015cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG, nB1, nB2, nColors >> 1, nPixNew, rIndex );
2016cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR2, nG + 1, nG2, nB1, nB2, nColors >> 1, nPixels - nPixNew, rIndex );
2017cdf0e10cSrcweir 				}
2018cdf0e10cSrcweir 				else
2019cdf0e10cSrcweir 				{
2020cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG - 1, nB1, nB2, nColors >> 1, nPixOld, rIndex );
2021cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR2, nG, nG2, nB1, nB2, nColors >> 1, nPixels - nPixOld, rIndex );
2022cdf0e10cSrcweir 				}
2023cdf0e10cSrcweir 			}
2024cdf0e10cSrcweir 			else
2025cdf0e10cSrcweir 			{
2026cdf0e10cSrcweir 				nR = nR1 - 1;
2027cdf0e10cSrcweir 
2028cdf0e10cSrcweir 				while( nPixNew < nTest )
2029cdf0e10cSrcweir 				{
2030cdf0e10cSrcweir 					nR++, nPixOld = nPixNew;
2031cdf0e10cSrcweir 					for( nG = nG1; nG <= nG2; nG++ )
2032cdf0e10cSrcweir 						for( nB = nB1; nB <= nB2; nB++ )
2033cdf0e10cSrcweir 							nPixNew += pBuf[ RGB15( nR, nG, nB ) ];
2034cdf0e10cSrcweir 				}
2035cdf0e10cSrcweir 
2036cdf0e10cSrcweir 				if( nR < nR2 )
2037cdf0e10cSrcweir 				{
2038cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR, nG1, nG2, nB1, nB2, nColors >> 1, nPixNew, rIndex );
2039cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1 + 1, nR2, nG1, nG2, nB1, nB2, nColors >> 1, nPixels - nPixNew, rIndex );
2040cdf0e10cSrcweir 				}
2041cdf0e10cSrcweir 				else
2042cdf0e10cSrcweir 				{
2043cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR1, nR - 1, nG1, nG2, nB1, nB2, nColors >> 1, nPixOld, rIndex );
2044cdf0e10cSrcweir 					ImplMedianCut( pBuf, rPal, nR, nR2, nG1, nG2, nB1, nB2, nColors >> 1, nPixels - nPixOld, rIndex );
2045cdf0e10cSrcweir 				}
2046cdf0e10cSrcweir 			}
2047cdf0e10cSrcweir 		}
2048cdf0e10cSrcweir 	}
2049cdf0e10cSrcweir }
2050cdf0e10cSrcweir 
2051cdf0e10cSrcweir // ------------------------------------------------------------------------
2052cdf0e10cSrcweir 
2053cdf0e10cSrcweir sal_Bool Bitmap::Vectorize( PolyPolygon& rPolyPoly, sal_uLong nFlags, const Link* pProgress )
2054cdf0e10cSrcweir {
2055cdf0e10cSrcweir 	return ImplVectorizer().ImplVectorize( *this, rPolyPoly, nFlags, pProgress );
2056cdf0e10cSrcweir }
2057cdf0e10cSrcweir 
2058cdf0e10cSrcweir // ------------------------------------------------------------------------
2059cdf0e10cSrcweir 
2060cdf0e10cSrcweir sal_Bool Bitmap::Vectorize( GDIMetaFile& rMtf, sal_uInt8 cReduce, sal_uLong nFlags, const Link* pProgress )
2061cdf0e10cSrcweir {
2062cdf0e10cSrcweir 	return ImplVectorizer().ImplVectorize( *this, rMtf, cReduce, nFlags, pProgress );
2063cdf0e10cSrcweir }
2064cdf0e10cSrcweir 
2065cdf0e10cSrcweir // ------------------------------------------------------------------------
2066cdf0e10cSrcweir 
2067cdf0e10cSrcweir sal_Bool Bitmap::Adjust( short nLuminancePercent, short nContrastPercent,
2068cdf0e10cSrcweir 					 short nChannelRPercent, short nChannelGPercent, short nChannelBPercent,
2069cdf0e10cSrcweir 					 double fGamma, sal_Bool bInvert )
2070cdf0e10cSrcweir {
2071cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
2072cdf0e10cSrcweir 
2073cdf0e10cSrcweir 	// nothing to do => return quickly
2074cdf0e10cSrcweir 	if( !nLuminancePercent && !nContrastPercent &&
2075cdf0e10cSrcweir 		!nChannelRPercent && !nChannelGPercent && !nChannelBPercent &&
2076cdf0e10cSrcweir 		( fGamma == 1.0 ) && !bInvert )
2077cdf0e10cSrcweir 	{
2078cdf0e10cSrcweir 		bRet = sal_True;
2079cdf0e10cSrcweir 	}
2080cdf0e10cSrcweir 	else
2081cdf0e10cSrcweir 	{
2082cdf0e10cSrcweir 		BitmapWriteAccess* pAcc = AcquireWriteAccess();
2083cdf0e10cSrcweir 
2084cdf0e10cSrcweir 		if( pAcc )
2085cdf0e10cSrcweir 		{
2086cdf0e10cSrcweir 			BitmapColor		aCol;
2087cdf0e10cSrcweir 			const long		nW = pAcc->Width();
2088cdf0e10cSrcweir 			const long		nH = pAcc->Height();
2089cdf0e10cSrcweir 			sal_uInt8*			cMapR = new sal_uInt8[ 256 ];
2090cdf0e10cSrcweir 			sal_uInt8*			cMapG = new sal_uInt8[ 256 ];
2091cdf0e10cSrcweir 			sal_uInt8*			cMapB = new sal_uInt8[ 256 ];
2092cdf0e10cSrcweir 			long			nX, nY;
2093cdf0e10cSrcweir 			double			fM, fROff, fGOff, fBOff, fOff;
2094cdf0e10cSrcweir 
2095cdf0e10cSrcweir 			// calculate slope
2096cdf0e10cSrcweir 			if( nContrastPercent >= 0 )
2097cdf0e10cSrcweir 				fM = 128.0 / ( 128.0 - 1.27 * MinMax( nContrastPercent, 0L, 100L ) );
2098cdf0e10cSrcweir 			else
2099cdf0e10cSrcweir 				fM = ( 128.0 + 1.27 * MinMax( nContrastPercent, -100L, 0L ) ) / 128.0;
2100cdf0e10cSrcweir 
2101cdf0e10cSrcweir 			// total offset = luminance offset + contrast offset
2102cdf0e10cSrcweir 			fOff = MinMax( nLuminancePercent, -100L, 100L ) * 2.55 + 128.0 - fM * 128.0;
2103cdf0e10cSrcweir 
2104cdf0e10cSrcweir 			// channel offset = channel offset  + total offset
2105cdf0e10cSrcweir 			fROff = nChannelRPercent * 2.55 + fOff;
2106cdf0e10cSrcweir 			fGOff = nChannelGPercent * 2.55 + fOff;
2107cdf0e10cSrcweir 			fBOff = nChannelBPercent * 2.55 + fOff;
2108cdf0e10cSrcweir 
2109cdf0e10cSrcweir 			// calculate gamma value
2110cdf0e10cSrcweir 			fGamma = ( fGamma <= 0.0 || fGamma > 10.0 ) ? 1.0 : ( 1.0 / fGamma );
2111cdf0e10cSrcweir 			const sal_Bool bGamma = ( fGamma != 1.0 );
2112cdf0e10cSrcweir 
2113cdf0e10cSrcweir 			// create mapping table
2114cdf0e10cSrcweir 			for( nX = 0L; nX < 256L; nX++ )
2115cdf0e10cSrcweir 			{
2116cdf0e10cSrcweir 				cMapR[ nX ] = (sal_uInt8) MinMax( FRound( nX * fM + fROff ), 0L, 255L );
2117cdf0e10cSrcweir 				cMapG[ nX ] = (sal_uInt8) MinMax( FRound( nX * fM + fGOff ), 0L, 255L );
2118cdf0e10cSrcweir 				cMapB[ nX ] = (sal_uInt8) MinMax( FRound( nX * fM + fBOff ), 0L, 255L );
2119cdf0e10cSrcweir 
2120cdf0e10cSrcweir 				if( bGamma )
2121cdf0e10cSrcweir 				{
2122cdf0e10cSrcweir 					cMapR[ nX ] = GAMMA( cMapR[ nX ], fGamma );
2123cdf0e10cSrcweir 					cMapG[ nX ] = GAMMA( cMapG[ nX ], fGamma );
2124cdf0e10cSrcweir 					cMapB[ nX ] = GAMMA( cMapB[ nX ], fGamma );
2125cdf0e10cSrcweir 				}
2126cdf0e10cSrcweir 
2127cdf0e10cSrcweir 				if( bInvert )
2128cdf0e10cSrcweir 				{
2129cdf0e10cSrcweir 					cMapR[ nX ] = ~cMapR[ nX ];
2130cdf0e10cSrcweir 					cMapG[ nX ] = ~cMapG[ nX ];
2131cdf0e10cSrcweir 					cMapB[ nX ] = ~cMapB[ nX ];
2132cdf0e10cSrcweir 				}
2133cdf0e10cSrcweir 			}
2134cdf0e10cSrcweir 
2135cdf0e10cSrcweir 			// do modifying
2136cdf0e10cSrcweir 			if( pAcc->HasPalette() )
2137cdf0e10cSrcweir 			{
2138cdf0e10cSrcweir 				BitmapColor aNewCol;
2139cdf0e10cSrcweir 
2140cdf0e10cSrcweir 				for( sal_uInt16 i = 0, nCount = pAcc->GetPaletteEntryCount(); i < nCount; i++ )
2141cdf0e10cSrcweir 				{
2142cdf0e10cSrcweir 					const BitmapColor& rCol = pAcc->GetPaletteColor( i );
2143cdf0e10cSrcweir 					aNewCol.SetRed( cMapR[ rCol.GetRed() ] );
2144cdf0e10cSrcweir 					aNewCol.SetGreen( cMapG[ rCol.GetGreen() ] );
2145cdf0e10cSrcweir 					aNewCol.SetBlue( cMapB[ rCol.GetBlue() ] );
2146cdf0e10cSrcweir 					pAcc->SetPaletteColor( i, aNewCol );
2147cdf0e10cSrcweir 				}
2148cdf0e10cSrcweir 			}
2149cdf0e10cSrcweir 			else if( pAcc->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_BGR )
2150cdf0e10cSrcweir 			{
2151cdf0e10cSrcweir 				for( nY = 0L; nY < nH; nY++ )
2152cdf0e10cSrcweir 				{
2153cdf0e10cSrcweir 					Scanline pScan = pAcc->GetScanline( nY );
2154cdf0e10cSrcweir 
2155cdf0e10cSrcweir 					for( nX = 0L; nX < nW; nX++ )
2156cdf0e10cSrcweir 					{
2157cdf0e10cSrcweir 						*pScan = cMapB[ *pScan ]; pScan++;
2158cdf0e10cSrcweir 						*pScan = cMapG[ *pScan ]; pScan++;
2159cdf0e10cSrcweir 						*pScan = cMapR[ *pScan ]; pScan++;
2160cdf0e10cSrcweir 					}
2161cdf0e10cSrcweir 				}
2162cdf0e10cSrcweir 			}
2163cdf0e10cSrcweir 			else if( pAcc->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_RGB )
2164cdf0e10cSrcweir 			{
2165cdf0e10cSrcweir 				for( nY = 0L; nY < nH; nY++ )
2166cdf0e10cSrcweir 				{
2167cdf0e10cSrcweir 					Scanline pScan = pAcc->GetScanline( nY );
2168cdf0e10cSrcweir 
2169cdf0e10cSrcweir 					for( nX = 0L; nX < nW; nX++ )
2170cdf0e10cSrcweir 					{
2171cdf0e10cSrcweir 						*pScan = cMapR[ *pScan ]; pScan++;
2172cdf0e10cSrcweir 						*pScan = cMapG[ *pScan ]; pScan++;
2173cdf0e10cSrcweir 						*pScan = cMapB[ *pScan ]; pScan++;
2174cdf0e10cSrcweir 					}
2175cdf0e10cSrcweir 				}
2176cdf0e10cSrcweir 			}
2177cdf0e10cSrcweir 			else
2178cdf0e10cSrcweir 			{
2179cdf0e10cSrcweir 				for( nY = 0L; nY < nH; nY++ )
2180cdf0e10cSrcweir 				{
2181cdf0e10cSrcweir 					for( nX = 0L; nX < nW; nX++ )
2182cdf0e10cSrcweir 					{
2183cdf0e10cSrcweir 						aCol = pAcc->GetPixel( nY, nX );
2184cdf0e10cSrcweir 						aCol.SetRed( cMapR[ aCol.GetRed() ] );
2185cdf0e10cSrcweir 						aCol.SetGreen( cMapG[ aCol.GetGreen() ] );
2186cdf0e10cSrcweir 						aCol.SetBlue( cMapB[ aCol.GetBlue() ] );
2187cdf0e10cSrcweir 						pAcc->SetPixel( nY, nX, aCol );
2188cdf0e10cSrcweir 					}
2189cdf0e10cSrcweir 				}
2190cdf0e10cSrcweir 			}
2191cdf0e10cSrcweir 
2192cdf0e10cSrcweir 			delete[] cMapR;
2193cdf0e10cSrcweir 			delete[] cMapG;
2194cdf0e10cSrcweir 			delete[] cMapB;
2195cdf0e10cSrcweir 			ReleaseAccess( pAcc );
2196cdf0e10cSrcweir 			bRet = sal_True;
2197cdf0e10cSrcweir 		}
2198cdf0e10cSrcweir 	}
2199cdf0e10cSrcweir 
2200cdf0e10cSrcweir 	return bRet;
2201cdf0e10cSrcweir }
2202