xref: /AOO42X/main/vcl/source/gdi/bitmap3.cxx (revision 87bc88d3ed834c36654f277ed18005c290f77bdd)
19f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59f62ea84SAndrew Rist  * distributed with this work for additional information
69f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
119f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
139f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist  * software distributed under the License is distributed on an
159f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
179f62ea84SAndrew Rist  * specific language governing permissions and limitations
189f62ea84SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
209f62ea84SAndrew Rist  *************************************************************/
219f62ea84SAndrew Rist 
229f62ea84SAndrew 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                     {
386*87bc88d3SHerbert Dürr                         const sal_uInt8 cIndex = pReadAcc->GetPixelIndex( nY, nX );
387*87bc88d3SHerbert Dürr                         if( pReadAcc->GetPaletteColor( cIndex ).GetLuminance() >=
388cdf0e10cSrcweir                             cThreshold )
389cdf0e10cSrcweir                         {
390cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aWhite );
391cdf0e10cSrcweir                         }
392cdf0e10cSrcweir                         else
393cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aBlack );
394cdf0e10cSrcweir                     }
395cdf0e10cSrcweir                 }
396cdf0e10cSrcweir             }
397cdf0e10cSrcweir             else
398cdf0e10cSrcweir             {
399cdf0e10cSrcweir                 for( long nY = 0L; nY < nHeight; nY++ )
400cdf0e10cSrcweir                 {
401cdf0e10cSrcweir                     for( long nX = 0L; nX < nWidth; nX++ )
402cdf0e10cSrcweir                     {
403cdf0e10cSrcweir                         if( pReadAcc->GetPixel( nY, nX ).GetLuminance() >=
404cdf0e10cSrcweir                             cThreshold )
405cdf0e10cSrcweir                         {
406cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aWhite );
407cdf0e10cSrcweir                         }
408cdf0e10cSrcweir                         else
409cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aBlack );
410cdf0e10cSrcweir                     }
411cdf0e10cSrcweir                 }
412cdf0e10cSrcweir             }
413cdf0e10cSrcweir 
414cdf0e10cSrcweir             aNewBmp.ReleaseAccess( pWriteAcc );
415cdf0e10cSrcweir             bRet = sal_True;
416cdf0e10cSrcweir         }
417cdf0e10cSrcweir 
418cdf0e10cSrcweir         ReleaseAccess( pReadAcc );
419cdf0e10cSrcweir 
420cdf0e10cSrcweir         if( bRet )
421cdf0e10cSrcweir         {
422cdf0e10cSrcweir             const MapMode   aMap( maPrefMapMode );
423cdf0e10cSrcweir             const Size      aSize( maPrefSize );
424cdf0e10cSrcweir 
425cdf0e10cSrcweir             *this = aNewBmp;
426cdf0e10cSrcweir 
427cdf0e10cSrcweir             maPrefMapMode = aMap;
428cdf0e10cSrcweir             maPrefSize = aSize;
429cdf0e10cSrcweir         }
430cdf0e10cSrcweir     }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir     return bRet;
433cdf0e10cSrcweir }
434cdf0e10cSrcweir 
435cdf0e10cSrcweir // ------------------------------------------------------------------------
436cdf0e10cSrcweir 
437cdf0e10cSrcweir sal_Bool Bitmap::ImplMakeMonoDither()
438cdf0e10cSrcweir {
439cdf0e10cSrcweir     BitmapReadAccess*   pReadAcc = AcquireReadAccess();
440cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
441cdf0e10cSrcweir 
442cdf0e10cSrcweir     if( pReadAcc )
443cdf0e10cSrcweir     {
444cdf0e10cSrcweir         Bitmap              aNewBmp( GetSizePixel(), 1 );
445cdf0e10cSrcweir         BitmapWriteAccess*  pWriteAcc = aNewBmp.AcquireWriteAccess();
446cdf0e10cSrcweir 
447cdf0e10cSrcweir         if( pWriteAcc )
448cdf0e10cSrcweir         {
449cdf0e10cSrcweir             const BitmapColor   aBlack( pWriteAcc->GetBestMatchingColor( Color( COL_BLACK ) ) );
450cdf0e10cSrcweir             const BitmapColor   aWhite( pWriteAcc->GetBestMatchingColor( Color( COL_WHITE ) ) );
451cdf0e10cSrcweir             const long          nWidth = pWriteAcc->Width();
452cdf0e10cSrcweir             const long          nHeight = pWriteAcc->Height();
453cdf0e10cSrcweir             sal_uInt8               pDitherMatrix[ 16 ][ 16 ];
454cdf0e10cSrcweir 
455cdf0e10cSrcweir             ImplCreateDitherMatrix( &pDitherMatrix );
456cdf0e10cSrcweir 
457cdf0e10cSrcweir             if( pReadAcc->HasPalette() )
458cdf0e10cSrcweir             {
459cdf0e10cSrcweir                 for( long nY = 0L; nY < nHeight; nY++ )
460cdf0e10cSrcweir                 {
461cdf0e10cSrcweir                     for( long nX = 0L, nModY = nY % 16; nX < nWidth; nX++ )
462cdf0e10cSrcweir                     {
463*87bc88d3SHerbert Dürr                         const sal_uInt8 cIndex = pReadAcc->GetPixelIndex( nY, nX );
464*87bc88d3SHerbert Dürr                         if( pReadAcc->GetPaletteColor( cIndex ).GetLuminance() >
465cdf0e10cSrcweir                             pDitherMatrix[ nModY ][ nX % 16 ] )
466cdf0e10cSrcweir                         {
467cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aWhite );
468cdf0e10cSrcweir                         }
469cdf0e10cSrcweir                         else
470cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aBlack );
471cdf0e10cSrcweir                     }
472cdf0e10cSrcweir                 }
473cdf0e10cSrcweir             }
474cdf0e10cSrcweir             else
475cdf0e10cSrcweir             {
476cdf0e10cSrcweir                 for( long nY = 0L; nY < nHeight; nY++ )
477cdf0e10cSrcweir                 {
478cdf0e10cSrcweir                     for( long nX = 0L, nModY = nY % 16; nX < nWidth; nX++ )
479cdf0e10cSrcweir                     {
480cdf0e10cSrcweir                         if( pReadAcc->GetPixel( nY, nX ).GetLuminance() >
481cdf0e10cSrcweir                             pDitherMatrix[ nModY ][ nX % 16 ]  )
482cdf0e10cSrcweir                         {
483cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aWhite );
484cdf0e10cSrcweir                         }
485cdf0e10cSrcweir                         else
486cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aBlack );
487cdf0e10cSrcweir                     }
488cdf0e10cSrcweir                 }
489cdf0e10cSrcweir             }
490cdf0e10cSrcweir 
491cdf0e10cSrcweir             aNewBmp.ReleaseAccess( pWriteAcc );
492cdf0e10cSrcweir             bRet = sal_True;
493cdf0e10cSrcweir         }
494cdf0e10cSrcweir 
495cdf0e10cSrcweir         ReleaseAccess( pReadAcc );
496cdf0e10cSrcweir 
497cdf0e10cSrcweir         if( bRet )
498cdf0e10cSrcweir         {
499cdf0e10cSrcweir             const MapMode   aMap( maPrefMapMode );
500cdf0e10cSrcweir             const Size      aSize( maPrefSize );
501cdf0e10cSrcweir 
502cdf0e10cSrcweir             *this = aNewBmp;
503cdf0e10cSrcweir 
504cdf0e10cSrcweir             maPrefMapMode = aMap;
505cdf0e10cSrcweir             maPrefSize = aSize;
506cdf0e10cSrcweir         }
507cdf0e10cSrcweir     }
508cdf0e10cSrcweir 
509cdf0e10cSrcweir     return bRet;
510cdf0e10cSrcweir }
511cdf0e10cSrcweir 
512cdf0e10cSrcweir // ------------------------------------------------------------------------
513cdf0e10cSrcweir 
514cdf0e10cSrcweir sal_Bool Bitmap::ImplMakeGreyscales( sal_uInt16 nGreys )
515cdf0e10cSrcweir {
516cdf0e10cSrcweir     DBG_ASSERT( nGreys == 16 || nGreys == 256, "Only 16 or 256 greyscales are supported!" );
517cdf0e10cSrcweir 
518cdf0e10cSrcweir     BitmapReadAccess*   pReadAcc = AcquireReadAccess();
519cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
520cdf0e10cSrcweir 
521cdf0e10cSrcweir     if( pReadAcc )
522cdf0e10cSrcweir     {
523cdf0e10cSrcweir         const BitmapPalette&    rPal = GetGreyPalette( nGreys );
524cdf0e10cSrcweir         sal_uLong                   nShift = ( ( nGreys == 16 ) ? 4UL : 0UL );
525cdf0e10cSrcweir         sal_Bool                    bPalDiffers = !pReadAcc->HasPalette() || ( rPal.GetEntryCount() != pReadAcc->GetPaletteEntryCount() );
526cdf0e10cSrcweir 
527cdf0e10cSrcweir         if( !bPalDiffers )
528cdf0e10cSrcweir             bPalDiffers = ( (BitmapPalette&) rPal != pReadAcc->GetPalette() );
529cdf0e10cSrcweir 
530cdf0e10cSrcweir         if( bPalDiffers )
531cdf0e10cSrcweir         {
532cdf0e10cSrcweir             Bitmap              aNewBmp( GetSizePixel(), ( nGreys == 16 ) ? 4 : 8, &rPal );
533cdf0e10cSrcweir             BitmapWriteAccess*  pWriteAcc = aNewBmp.AcquireWriteAccess();
534cdf0e10cSrcweir 
535cdf0e10cSrcweir             if( pWriteAcc )
536cdf0e10cSrcweir             {
537cdf0e10cSrcweir                 const long  nWidth = pWriteAcc->Width();
538cdf0e10cSrcweir                 const long  nHeight = pWriteAcc->Height();
539cdf0e10cSrcweir 
540cdf0e10cSrcweir                 if( pReadAcc->HasPalette() )
541cdf0e10cSrcweir                 {
542cdf0e10cSrcweir                     for( long nY = 0L; nY < nHeight; nY++ )
543cdf0e10cSrcweir                     {
544cdf0e10cSrcweir                         for( long nX = 0L; nX < nWidth; nX++ )
545cdf0e10cSrcweir                         {
546*87bc88d3SHerbert Dürr                             const sal_uInt8 cIndex = pReadAcc->GetPixelIndex( nY, nX );
547*87bc88d3SHerbert Dürr                             pWriteAcc->SetPixelIndex( nY, nX,
548*87bc88d3SHerbert Dürr                                 (pReadAcc->GetPaletteColor( cIndex ).GetLuminance() >> nShift) );
549cdf0e10cSrcweir                         }
550cdf0e10cSrcweir                     }
551cdf0e10cSrcweir                 }
552cdf0e10cSrcweir                 else if( pReadAcc->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_BGR &&
553cdf0e10cSrcweir                          pWriteAcc->GetScanlineFormat() == BMP_FORMAT_8BIT_PAL )
554cdf0e10cSrcweir                 {
555cdf0e10cSrcweir                     nShift += 8;
556cdf0e10cSrcweir 
557cdf0e10cSrcweir                     for( long nY = 0L; nY < nHeight; nY++ )
558cdf0e10cSrcweir                     {
559cdf0e10cSrcweir                         Scanline pReadScan = pReadAcc->GetScanline( nY );
560cdf0e10cSrcweir                         Scanline pWriteScan = pWriteAcc->GetScanline( nY );
561cdf0e10cSrcweir 
562cdf0e10cSrcweir                         for( long nX = 0L; nX < nWidth; nX++ )
563cdf0e10cSrcweir                         {
564cdf0e10cSrcweir                             const sal_uLong nB = *pReadScan++;
565cdf0e10cSrcweir                             const sal_uLong nG = *pReadScan++;
566cdf0e10cSrcweir                             const sal_uLong nR = *pReadScan++;
567cdf0e10cSrcweir 
568cdf0e10cSrcweir                             *pWriteScan++ = (sal_uInt8) ( ( nB * 28UL + nG * 151UL + nR * 77UL ) >> nShift );
569cdf0e10cSrcweir                         }
570cdf0e10cSrcweir                     }
571cdf0e10cSrcweir                 }
572cdf0e10cSrcweir                 else if( pReadAcc->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_RGB &&
573cdf0e10cSrcweir                          pWriteAcc->GetScanlineFormat() == BMP_FORMAT_8BIT_PAL )
574cdf0e10cSrcweir                 {
575cdf0e10cSrcweir                     nShift += 8;
576cdf0e10cSrcweir 
577cdf0e10cSrcweir                     for( long nY = 0L; nY < nHeight; nY++ )
578cdf0e10cSrcweir                     {
579cdf0e10cSrcweir                         Scanline pReadScan = pReadAcc->GetScanline( nY );
580cdf0e10cSrcweir                         Scanline pWriteScan = pWriteAcc->GetScanline( nY );
581cdf0e10cSrcweir 
582cdf0e10cSrcweir                         for( long nX = 0L; nX < nWidth; nX++ )
583cdf0e10cSrcweir                         {
584cdf0e10cSrcweir                             const sal_uLong nR = *pReadScan++;
585cdf0e10cSrcweir                             const sal_uLong nG = *pReadScan++;
586cdf0e10cSrcweir                             const sal_uLong nB = *pReadScan++;
587cdf0e10cSrcweir 
588cdf0e10cSrcweir                             *pWriteScan++ = (sal_uInt8) ( ( nB * 28UL + nG * 151UL + nR * 77UL ) >> nShift );
589cdf0e10cSrcweir                         }
590cdf0e10cSrcweir                     }
591cdf0e10cSrcweir                 }
592cdf0e10cSrcweir                 else
593cdf0e10cSrcweir                 {
594cdf0e10cSrcweir                     for( long nY = 0L; nY < nHeight; nY++ )
595cdf0e10cSrcweir                         for( long nX = 0L; nX < nWidth; nX++ )
596*87bc88d3SHerbert Dürr                             pWriteAcc->SetPixelIndex( nY, nX, (pReadAcc->GetPixel( nY, nX ) ).GetLuminance() >> nShift );
597cdf0e10cSrcweir                 }
598cdf0e10cSrcweir 
599cdf0e10cSrcweir                 aNewBmp.ReleaseAccess( pWriteAcc );
600cdf0e10cSrcweir                 bRet = sal_True;
601cdf0e10cSrcweir             }
602cdf0e10cSrcweir 
603cdf0e10cSrcweir             ReleaseAccess( pReadAcc );
604cdf0e10cSrcweir 
605cdf0e10cSrcweir             if( bRet )
606cdf0e10cSrcweir             {
607cdf0e10cSrcweir                 const MapMode   aMap( maPrefMapMode );
608cdf0e10cSrcweir                 const Size      aSize( maPrefSize );
609cdf0e10cSrcweir 
610cdf0e10cSrcweir                 *this = aNewBmp;
611cdf0e10cSrcweir 
612cdf0e10cSrcweir                 maPrefMapMode = aMap;
613cdf0e10cSrcweir                 maPrefSize = aSize;
614cdf0e10cSrcweir             }
615cdf0e10cSrcweir         }
616cdf0e10cSrcweir         else
617cdf0e10cSrcweir         {
618cdf0e10cSrcweir             ReleaseAccess( pReadAcc );
619cdf0e10cSrcweir             bRet = sal_True;
620cdf0e10cSrcweir         }
621cdf0e10cSrcweir     }
622cdf0e10cSrcweir 
623cdf0e10cSrcweir     return bRet;
624cdf0e10cSrcweir }
625cdf0e10cSrcweir 
626cdf0e10cSrcweir // ------------------------------------------------------------------------
627cdf0e10cSrcweir 
628cdf0e10cSrcweir sal_Bool Bitmap::ImplConvertUp( sal_uInt16 nBitCount, Color* pExtColor )
629cdf0e10cSrcweir {
630cdf0e10cSrcweir     DBG_ASSERT( nBitCount > GetBitCount(), "New BitCount must be greater!" );
631cdf0e10cSrcweir 
632cdf0e10cSrcweir     BitmapReadAccess*   pReadAcc = AcquireReadAccess();
633cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
634cdf0e10cSrcweir 
635cdf0e10cSrcweir     if( pReadAcc )
636cdf0e10cSrcweir     {
637cdf0e10cSrcweir         BitmapPalette       aPal;
638cdf0e10cSrcweir         Bitmap              aNewBmp( GetSizePixel(), nBitCount, pReadAcc->HasPalette() ? &pReadAcc->GetPalette() : &aPal );
639cdf0e10cSrcweir         BitmapWriteAccess*  pWriteAcc = aNewBmp.AcquireWriteAccess();
640cdf0e10cSrcweir 
641cdf0e10cSrcweir         if( pWriteAcc )
642cdf0e10cSrcweir         {
643cdf0e10cSrcweir             const long  nWidth = pWriteAcc->Width();
644cdf0e10cSrcweir             const long  nHeight = pWriteAcc->Height();
645cdf0e10cSrcweir 
646cdf0e10cSrcweir             if( pWriteAcc->HasPalette() )
647cdf0e10cSrcweir             {
648cdf0e10cSrcweir                 const sal_uInt16            nOldCount = 1 << GetBitCount();
649cdf0e10cSrcweir                 const BitmapPalette&    rOldPal = pReadAcc->GetPalette();
650cdf0e10cSrcweir 
651cdf0e10cSrcweir                 aPal.SetEntryCount( 1 << nBitCount );
652cdf0e10cSrcweir 
653cdf0e10cSrcweir                 for( sal_uInt16 i = 0; i < nOldCount; i++ )
654cdf0e10cSrcweir                     aPal[ i ] = rOldPal[ i ];
655cdf0e10cSrcweir 
656cdf0e10cSrcweir                 if( pExtColor )
657cdf0e10cSrcweir                     aPal[ aPal.GetEntryCount() - 1 ] = *pExtColor;
658cdf0e10cSrcweir 
659cdf0e10cSrcweir                 pWriteAcc->SetPalette( aPal );
660cdf0e10cSrcweir 
661cdf0e10cSrcweir                 for( long nY = 0L; nY < nHeight; nY++ )
662cdf0e10cSrcweir                     for( long nX = 0L; nX < nWidth; nX++ )
663cdf0e10cSrcweir                         pWriteAcc->SetPixel( nY, nX, pReadAcc->GetPixel( nY, nX ) );
664cdf0e10cSrcweir             }
665cdf0e10cSrcweir             else
666cdf0e10cSrcweir             {
667cdf0e10cSrcweir                 if( pReadAcc->HasPalette() )
668cdf0e10cSrcweir                 {
669cdf0e10cSrcweir                     for( long nY = 0L; nY < nHeight; nY++ )
670cdf0e10cSrcweir                         for( long nX = 0L; nX < nWidth; nX++ )
671*87bc88d3SHerbert Dürr                             pWriteAcc->SetPixel( nY, nX, pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nY, nX ) ) );
672cdf0e10cSrcweir                 }
673cdf0e10cSrcweir                 else
674cdf0e10cSrcweir                 {
675cdf0e10cSrcweir                     for( long nY = 0L; nY < nHeight; nY++ )
676cdf0e10cSrcweir                         for( long nX = 0L; nX < nWidth; nX++ )
677cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, pReadAcc->GetPixel( nY, nX ) );
678cdf0e10cSrcweir                 }
679cdf0e10cSrcweir             }
680cdf0e10cSrcweir 
681cdf0e10cSrcweir             aNewBmp.ReleaseAccess( pWriteAcc );
682cdf0e10cSrcweir             bRet = sal_True;
683cdf0e10cSrcweir         }
684cdf0e10cSrcweir 
685cdf0e10cSrcweir         ReleaseAccess( pReadAcc );
686cdf0e10cSrcweir 
687cdf0e10cSrcweir         if( bRet )
688cdf0e10cSrcweir         {
689cdf0e10cSrcweir             const MapMode   aMap( maPrefMapMode );
690cdf0e10cSrcweir             const Size      aSize( maPrefSize );
691cdf0e10cSrcweir 
692cdf0e10cSrcweir             *this = aNewBmp;
693cdf0e10cSrcweir 
694cdf0e10cSrcweir             maPrefMapMode = aMap;
695cdf0e10cSrcweir             maPrefSize = aSize;
696cdf0e10cSrcweir         }
697cdf0e10cSrcweir     }
698cdf0e10cSrcweir 
699cdf0e10cSrcweir     return bRet;
700cdf0e10cSrcweir }
701cdf0e10cSrcweir 
702cdf0e10cSrcweir // ------------------------------------------------------------------------
703cdf0e10cSrcweir 
704cdf0e10cSrcweir sal_Bool Bitmap::ImplConvertDown( sal_uInt16 nBitCount, Color* pExtColor )
705cdf0e10cSrcweir {
706cdf0e10cSrcweir     DBG_ASSERT( nBitCount <= GetBitCount(), "New BitCount must be lower ( or equal when pExtColor is set )!" );
707cdf0e10cSrcweir 
708cdf0e10cSrcweir     BitmapReadAccess*   pReadAcc = AcquireReadAccess();
709cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
710cdf0e10cSrcweir 
711cdf0e10cSrcweir     if( pReadAcc )
712cdf0e10cSrcweir     {
713cdf0e10cSrcweir         BitmapPalette       aPal;
714cdf0e10cSrcweir         Bitmap              aNewBmp( GetSizePixel(), nBitCount, &aPal );
715cdf0e10cSrcweir         BitmapWriteAccess*  pWriteAcc = aNewBmp.AcquireWriteAccess();
716cdf0e10cSrcweir 
717cdf0e10cSrcweir         if( pWriteAcc )
718cdf0e10cSrcweir         {
719cdf0e10cSrcweir             const sal_uInt16    nCount = 1 << nBitCount;
720cdf0e10cSrcweir             const long      nWidth = pWriteAcc->Width();
721cdf0e10cSrcweir             const long      nWidth1 = nWidth - 1L;
722cdf0e10cSrcweir             const long      nHeight = pWriteAcc->Height();
723cdf0e10cSrcweir             Octree          aOctree( *pReadAcc, pExtColor ? ( nCount - 1 ) : nCount );
724cdf0e10cSrcweir             InverseColorMap aColorMap( aPal = aOctree.GetPalette() );
725cdf0e10cSrcweir             BitmapColor     aColor;
726cdf0e10cSrcweir             ImpErrorQuad    aErrQuad;
727cdf0e10cSrcweir             ImpErrorQuad*   pErrQuad1 = new ImpErrorQuad[ nWidth ];
728cdf0e10cSrcweir             ImpErrorQuad*   pErrQuad2 = new ImpErrorQuad[ nWidth ];
729cdf0e10cSrcweir             ImpErrorQuad*   pQLine1 = pErrQuad1;
730cdf0e10cSrcweir             ImpErrorQuad*   pQLine2 = 0;
731cdf0e10cSrcweir             long            nX, nY;
732cdf0e10cSrcweir             long            nYTmp = 0L;
733cdf0e10cSrcweir             sal_uInt8           cIndex;
734cdf0e10cSrcweir             sal_Bool            bQ1 = sal_True;
735cdf0e10cSrcweir 
736cdf0e10cSrcweir             if( pExtColor )
737cdf0e10cSrcweir             {
738cdf0e10cSrcweir                 aPal.SetEntryCount( aPal.GetEntryCount() + 1 );
739cdf0e10cSrcweir                 aPal[ aPal.GetEntryCount() - 1 ] = *pExtColor;
740cdf0e10cSrcweir             }
741cdf0e10cSrcweir 
742cdf0e10cSrcweir             // set Black/White always, if we have enough space
743cdf0e10cSrcweir             if( aPal.GetEntryCount() < ( nCount - 1 ) )
744cdf0e10cSrcweir             {
745cdf0e10cSrcweir                 aPal.SetEntryCount( aPal.GetEntryCount() + 2 );
746cdf0e10cSrcweir                 aPal[ aPal.GetEntryCount() - 2 ] = Color( COL_BLACK );
747cdf0e10cSrcweir                 aPal[ aPal.GetEntryCount() - 1 ] = Color( COL_WHITE );
748cdf0e10cSrcweir             }
749cdf0e10cSrcweir 
750cdf0e10cSrcweir             pWriteAcc->SetPalette( aPal );
751cdf0e10cSrcweir 
752cdf0e10cSrcweir             for( nY = 0L; nY < Min( nHeight, 2L ); nY++, nYTmp++ )
753cdf0e10cSrcweir             {
754cdf0e10cSrcweir                 for( nX = 0L, pQLine2 = !nY ? pErrQuad1 : pErrQuad2; nX < nWidth; nX++ )
755cdf0e10cSrcweir                 {
756cdf0e10cSrcweir                     if( pReadAcc->HasPalette() )
757*87bc88d3SHerbert Dürr                         pQLine2[ nX ] = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nYTmp, nX ) );
758cdf0e10cSrcweir                     else
759cdf0e10cSrcweir                         pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
760cdf0e10cSrcweir                 }
761cdf0e10cSrcweir             }
762cdf0e10cSrcweir 
763cdf0e10cSrcweir             for( nY = 0L; nY < nHeight; nY++, nYTmp++ )
764cdf0e10cSrcweir             {
765*87bc88d3SHerbert Dürr                 // first pixel in the line
766cdf0e10cSrcweir                 cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex( pQLine1[ 0 ].ImplGetColor() );
767*87bc88d3SHerbert Dürr                 pWriteAcc->SetPixelIndex( nY, 0, cIndex );
768cdf0e10cSrcweir 
769cdf0e10cSrcweir                 for( nX = 1L; nX < nWidth1; nX++ )
770cdf0e10cSrcweir                 {
771cdf0e10cSrcweir                     cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex( aColor = pQLine1[ nX ].ImplGetColor() );
772cdf0e10cSrcweir                     aErrQuad = ( ImpErrorQuad( aColor ) -= pWriteAcc->GetPaletteColor( cIndex ) );
773cdf0e10cSrcweir                     pQLine1[ ++nX ].ImplAddColorError7( aErrQuad );
774cdf0e10cSrcweir                     pQLine2[ nX-- ].ImplAddColorError1( aErrQuad );
775cdf0e10cSrcweir                     pQLine2[ nX-- ].ImplAddColorError5( aErrQuad );
776cdf0e10cSrcweir                     pQLine2[ nX++ ].ImplAddColorError3( aErrQuad );
777*87bc88d3SHerbert Dürr                     pWriteAcc->SetPixelIndex( nY, nX, cIndex );
778cdf0e10cSrcweir                 }
779cdf0e10cSrcweir 
780cdf0e10cSrcweir                 // letztes ZeilenPixel
781cdf0e10cSrcweir                 if( nX < nWidth )
782cdf0e10cSrcweir                 {
783cdf0e10cSrcweir                     cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex( pQLine1[ nWidth1 ].ImplGetColor() );
784*87bc88d3SHerbert Dürr                     pWriteAcc->SetPixelIndex( nY, nX, cIndex );
785cdf0e10cSrcweir                 }
786cdf0e10cSrcweir 
787cdf0e10cSrcweir                 // Zeilenpuffer neu fuellen/kopieren
788cdf0e10cSrcweir                 pQLine1 = pQLine2;
789cdf0e10cSrcweir                 pQLine2 = ( bQ1 = !bQ1 ) != sal_False ? pErrQuad2 : pErrQuad1;
790cdf0e10cSrcweir 
791cdf0e10cSrcweir                 if( nYTmp < nHeight )
792cdf0e10cSrcweir                 {
793cdf0e10cSrcweir                     for( nX = 0L; nX < nWidth; nX++ )
794cdf0e10cSrcweir                     {
795cdf0e10cSrcweir                         if( pReadAcc->HasPalette() )
796*87bc88d3SHerbert Dürr                             pQLine2[ nX ] = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nYTmp, nX ) );
797cdf0e10cSrcweir                         else
798cdf0e10cSrcweir                             pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
799cdf0e10cSrcweir                     }
800cdf0e10cSrcweir                 }
801cdf0e10cSrcweir             }
802cdf0e10cSrcweir 
803cdf0e10cSrcweir             // Zeilenpuffer zerstoeren
804cdf0e10cSrcweir             delete[] pErrQuad1;
805cdf0e10cSrcweir             delete[] pErrQuad2;
806cdf0e10cSrcweir 
807cdf0e10cSrcweir             aNewBmp.ReleaseAccess( pWriteAcc );
808cdf0e10cSrcweir             bRet = sal_True;
809cdf0e10cSrcweir         }
810cdf0e10cSrcweir 
811cdf0e10cSrcweir         ReleaseAccess( pReadAcc );
812cdf0e10cSrcweir 
813cdf0e10cSrcweir         if( bRet )
814cdf0e10cSrcweir         {
815cdf0e10cSrcweir             const MapMode   aMap( maPrefMapMode );
816cdf0e10cSrcweir             const Size      aSize( maPrefSize );
817cdf0e10cSrcweir 
818cdf0e10cSrcweir             *this = aNewBmp;
819cdf0e10cSrcweir 
820cdf0e10cSrcweir             maPrefMapMode = aMap;
821cdf0e10cSrcweir             maPrefSize = aSize;
822cdf0e10cSrcweir         }
823cdf0e10cSrcweir     }
824cdf0e10cSrcweir 
825cdf0e10cSrcweir     return bRet;
826cdf0e10cSrcweir }
827cdf0e10cSrcweir 
828cdf0e10cSrcweir // ------------------------------------------------------------------------
829cdf0e10cSrcweir 
830cdf0e10cSrcweir sal_Bool Bitmap::ImplConvertGhosted()
831cdf0e10cSrcweir {
832cdf0e10cSrcweir     Bitmap              aNewBmp;
833cdf0e10cSrcweir     BitmapReadAccess*   pR = AcquireReadAccess();
834cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
835cdf0e10cSrcweir 
836cdf0e10cSrcweir     if( pR )
837cdf0e10cSrcweir     {
838cdf0e10cSrcweir         if( pR->HasPalette() )
839cdf0e10cSrcweir         {
840cdf0e10cSrcweir             BitmapPalette aNewPal( pR->GetPaletteEntryCount() );
841cdf0e10cSrcweir 
842cdf0e10cSrcweir             for( long i = 0, nCount = aNewPal.GetEntryCount(); i < nCount; i++ )
843cdf0e10cSrcweir             {
844cdf0e10cSrcweir                 const BitmapColor& rOld = pR->GetPaletteColor( (sal_uInt16) i );
845cdf0e10cSrcweir                 aNewPal[ (sal_uInt16) i ] = BitmapColor( ( rOld.GetRed() >> 1 ) | 0x80,
846cdf0e10cSrcweir                                                      ( rOld.GetGreen() >> 1 ) | 0x80,
847cdf0e10cSrcweir                                                      ( rOld.GetBlue() >> 1 ) | 0x80 );
848cdf0e10cSrcweir             }
849cdf0e10cSrcweir 
850cdf0e10cSrcweir             aNewBmp = Bitmap( GetSizePixel(), GetBitCount(), &aNewPal );
851cdf0e10cSrcweir             BitmapWriteAccess* pW = aNewBmp.AcquireWriteAccess();
852cdf0e10cSrcweir 
853cdf0e10cSrcweir             if( pW )
854cdf0e10cSrcweir             {
855cdf0e10cSrcweir                 pW->CopyBuffer( *pR );
856cdf0e10cSrcweir                 aNewBmp.ReleaseAccess( pW );
857cdf0e10cSrcweir                 bRet = sal_True;
858cdf0e10cSrcweir             }
859cdf0e10cSrcweir         }
860cdf0e10cSrcweir         else
861cdf0e10cSrcweir         {
862cdf0e10cSrcweir             aNewBmp = Bitmap( GetSizePixel(), 24 );
863cdf0e10cSrcweir 
864cdf0e10cSrcweir             BitmapWriteAccess* pW = aNewBmp.AcquireWriteAccess();
865cdf0e10cSrcweir 
866cdf0e10cSrcweir             if( pW )
867cdf0e10cSrcweir             {
868cdf0e10cSrcweir                 const long nWidth = pR->Width(), nHeight = pR->Height();
869cdf0e10cSrcweir 
870cdf0e10cSrcweir                 for( long nY = 0; nY < nHeight; nY++ )
871cdf0e10cSrcweir                 {
872cdf0e10cSrcweir                     for( long nX = 0; nX < nWidth; nX++ )
873cdf0e10cSrcweir                     {
874cdf0e10cSrcweir                         const BitmapColor aOld( pR->GetPixel( nY, nX ) );
875cdf0e10cSrcweir                         pW->SetPixel( nY, nX, BitmapColor( ( aOld.GetRed() >> 1 ) | 0x80,
876cdf0e10cSrcweir                                                            ( aOld.GetGreen() >> 1 ) | 0x80,
877cdf0e10cSrcweir                                                            ( aOld.GetBlue() >> 1 ) | 0x80 ) );
878cdf0e10cSrcweir 
879cdf0e10cSrcweir                     }
880cdf0e10cSrcweir                 }
881cdf0e10cSrcweir 
882cdf0e10cSrcweir                 aNewBmp.ReleaseAccess( pW );
883cdf0e10cSrcweir                 bRet = sal_True;
884cdf0e10cSrcweir             }
885cdf0e10cSrcweir         }
886cdf0e10cSrcweir 
887cdf0e10cSrcweir         ReleaseAccess( pR );
888cdf0e10cSrcweir     }
889cdf0e10cSrcweir 
890cdf0e10cSrcweir     if( bRet )
891cdf0e10cSrcweir     {
892cdf0e10cSrcweir         const MapMode   aMap( maPrefMapMode );
893cdf0e10cSrcweir         const Size      aSize( maPrefSize );
894cdf0e10cSrcweir 
895cdf0e10cSrcweir         *this = aNewBmp;
896cdf0e10cSrcweir 
897cdf0e10cSrcweir         maPrefMapMode = aMap;
898cdf0e10cSrcweir         maPrefSize = aSize;
899cdf0e10cSrcweir     }
900cdf0e10cSrcweir 
901cdf0e10cSrcweir     return bRet;
902cdf0e10cSrcweir }
903cdf0e10cSrcweir 
904cdf0e10cSrcweir // ------------------------------------------------------------------------
905cdf0e10cSrcweir 
906cdf0e10cSrcweir sal_Bool Bitmap::Scale( const double& rScaleX, const double& rScaleY, sal_uLong nScaleFlag )
907cdf0e10cSrcweir {
908cdf0e10cSrcweir     sal_Bool bRet;
909cdf0e10cSrcweir 
910cdf0e10cSrcweir     if( ( rScaleX != 1.0 ) || ( rScaleY != 1.0 ) )
911cdf0e10cSrcweir     {
912cdf0e10cSrcweir         if( BMP_SCALE_FAST == nScaleFlag )
913cdf0e10cSrcweir             bRet = ImplScaleFast( rScaleX, rScaleY );
914cdf0e10cSrcweir         else if( BMP_SCALE_INTERPOLATE == nScaleFlag )
915cdf0e10cSrcweir             bRet = ImplScaleInterpolate( rScaleX, rScaleY );
916cdf0e10cSrcweir         else
917cdf0e10cSrcweir             bRet = sal_False;
918cdf0e10cSrcweir     }
919cdf0e10cSrcweir     else
920cdf0e10cSrcweir         bRet = sal_True;
921cdf0e10cSrcweir 
922cdf0e10cSrcweir     return bRet;
923cdf0e10cSrcweir }
924cdf0e10cSrcweir 
925cdf0e10cSrcweir // ------------------------------------------------------------------------
926cdf0e10cSrcweir 
927cdf0e10cSrcweir sal_Bool Bitmap::Scale( const Size& rNewSize, sal_uLong nScaleFlag )
928cdf0e10cSrcweir {
929cdf0e10cSrcweir     const Size  aSize( GetSizePixel() );
930cdf0e10cSrcweir     sal_Bool        bRet;
931cdf0e10cSrcweir 
932cdf0e10cSrcweir     if( aSize.Width() && aSize.Height() )
933cdf0e10cSrcweir     {
934cdf0e10cSrcweir         bRet = Scale( (double) rNewSize.Width() / aSize.Width(),
935cdf0e10cSrcweir                       (double) rNewSize.Height() / aSize.Height(),
936cdf0e10cSrcweir                       nScaleFlag );
937cdf0e10cSrcweir     }
938cdf0e10cSrcweir     else
939cdf0e10cSrcweir         bRet = sal_True;
940cdf0e10cSrcweir 
941cdf0e10cSrcweir     return bRet;
942cdf0e10cSrcweir }
943cdf0e10cSrcweir 
944cdf0e10cSrcweir // ------------------------------------------------------------------------
945cdf0e10cSrcweir 
946cdf0e10cSrcweir sal_Bool Bitmap::ImplScaleFast( const double& rScaleX, const double& rScaleY )
947cdf0e10cSrcweir {
948cdf0e10cSrcweir     const Size  aSizePix( GetSizePixel() );
949cdf0e10cSrcweir     const long  nNewWidth = FRound( aSizePix.Width() * rScaleX );
950cdf0e10cSrcweir     const long  nNewHeight = FRound( aSizePix.Height() * rScaleY );
951cdf0e10cSrcweir     sal_Bool        bRet = sal_False;
952cdf0e10cSrcweir 
953cdf0e10cSrcweir     if( nNewWidth && nNewHeight )
954cdf0e10cSrcweir     {
955cdf0e10cSrcweir         BitmapReadAccess*   pReadAcc = AcquireReadAccess();
9560f740837SAriel Constenla-Haile         if ( !pReadAcc )
9570f740837SAriel Constenla-Haile             return sal_False;
9580f740837SAriel Constenla-Haile 
959cdf0e10cSrcweir         Bitmap              aNewBmp( Size( nNewWidth, nNewHeight ), GetBitCount(), &pReadAcc->GetPalette() );
960cdf0e10cSrcweir         BitmapWriteAccess*  pWriteAcc = aNewBmp.AcquireWriteAccess();
961cdf0e10cSrcweir 
9620f740837SAriel Constenla-Haile         if( pWriteAcc )
963cdf0e10cSrcweir         {
964cdf0e10cSrcweir             const long  nScanlineSize = pWriteAcc->GetScanlineSize();
965cdf0e10cSrcweir             const long  nNewWidth1 = nNewWidth - 1L;
966cdf0e10cSrcweir             const long  nNewHeight1 = nNewHeight - 1L;
967cdf0e10cSrcweir             const long  nWidth = pReadAcc->Width();
968cdf0e10cSrcweir             const long  nHeight = pReadAcc->Height();
969cdf0e10cSrcweir             long*       pLutX = new long[ nNewWidth ];
970cdf0e10cSrcweir             long*       pLutY = new long[ nNewHeight ];
971cdf0e10cSrcweir             long        nX, nY, nMapY, nActY = 0L;
972cdf0e10cSrcweir 
973cdf0e10cSrcweir             if( nNewWidth1 && nNewHeight1 )
974cdf0e10cSrcweir             {
975cdf0e10cSrcweir                 for( nX = 0L; nX < nNewWidth; nX++ )
976cdf0e10cSrcweir                     pLutX[ nX ] = nX * nWidth / nNewWidth;
977cdf0e10cSrcweir 
978cdf0e10cSrcweir                 for( nY = 0L; nY < nNewHeight; nY++ )
979cdf0e10cSrcweir                     pLutY[ nY ] = nY * nHeight / nNewHeight;
980cdf0e10cSrcweir 
981cdf0e10cSrcweir                 while( nActY < nNewHeight )
982cdf0e10cSrcweir                 {
983cdf0e10cSrcweir                     nMapY = pLutY[ nActY ];
984cdf0e10cSrcweir 
985cdf0e10cSrcweir                     for( nX = 0L; nX < nNewWidth; nX++ )
986cdf0e10cSrcweir                         pWriteAcc->SetPixel( nActY, nX, pReadAcc->GetPixel( nMapY , pLutX[ nX ] ) );
987cdf0e10cSrcweir 
988cdf0e10cSrcweir                     while( ( nActY < nNewHeight1 ) && ( pLutY[ nActY + 1 ] == nMapY ) )
989cdf0e10cSrcweir                     {
990cdf0e10cSrcweir                         memcpy( pWriteAcc->GetScanline( nActY + 1L ),
991cdf0e10cSrcweir                                  pWriteAcc->GetScanline( nActY ), nScanlineSize );
992cdf0e10cSrcweir                         nActY++;
993cdf0e10cSrcweir                     }
994cdf0e10cSrcweir 
995cdf0e10cSrcweir                     nActY++;
996cdf0e10cSrcweir                 }
997cdf0e10cSrcweir 
998cdf0e10cSrcweir                 bRet = sal_True;
999cdf0e10cSrcweir             }
1000cdf0e10cSrcweir 
1001cdf0e10cSrcweir             delete[] pLutX;
1002cdf0e10cSrcweir             delete[] pLutY;
1003cdf0e10cSrcweir         }
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir         ReleaseAccess( pReadAcc );
1006cdf0e10cSrcweir         aNewBmp.ReleaseAccess( pWriteAcc );
1007cdf0e10cSrcweir 
1008cdf0e10cSrcweir         if( bRet )
1009cdf0e10cSrcweir             ImplAssignWithSize( aNewBmp );
1010cdf0e10cSrcweir     }
1011cdf0e10cSrcweir 
1012cdf0e10cSrcweir     return bRet;
1013cdf0e10cSrcweir }
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir // ------------------------------------------------------------------------
1016cdf0e10cSrcweir 
1017cdf0e10cSrcweir sal_Bool Bitmap::ImplScaleInterpolate( const double& rScaleX, const double& rScaleY )
1018cdf0e10cSrcweir {
1019cdf0e10cSrcweir     const Size  aSizePix( GetSizePixel() );
1020cdf0e10cSrcweir     const long  nNewWidth = FRound( aSizePix.Width() * rScaleX );
1021cdf0e10cSrcweir     const long  nNewHeight = FRound( aSizePix.Height() * rScaleY );
1022cdf0e10cSrcweir     sal_Bool        bRet = sal_False;
1023cdf0e10cSrcweir 
1024cdf0e10cSrcweir     if( ( nNewWidth > 1L ) && ( nNewHeight > 1L ) )
1025cdf0e10cSrcweir     {
1026cdf0e10cSrcweir         BitmapColor         aCol0;
1027cdf0e10cSrcweir         BitmapColor         aCol1;
1028cdf0e10cSrcweir         BitmapReadAccess*   pReadAcc = AcquireReadAccess();
1029cdf0e10cSrcweir         long                nWidth = pReadAcc->Width();
1030cdf0e10cSrcweir         long                nHeight = pReadAcc->Height();
1031cdf0e10cSrcweir         Bitmap              aNewBmp( Size( nNewWidth, nHeight ), 24 );
1032cdf0e10cSrcweir         BitmapWriteAccess*  pWriteAcc = aNewBmp.AcquireWriteAccess();
1033cdf0e10cSrcweir         long*               pLutInt;
1034cdf0e10cSrcweir         long*               pLutFrac;
1035cdf0e10cSrcweir         long                nX, nY;
1036cdf0e10cSrcweir         long                lXB0, lXB1, lXG0, lXG1, lXR0, lXR1;
1037cdf0e10cSrcweir         double              fTemp;
1038cdf0e10cSrcweir         long                nTemp;
1039cdf0e10cSrcweir 
1040cdf0e10cSrcweir         if( pReadAcc && pWriteAcc )
1041cdf0e10cSrcweir         {
1042cdf0e10cSrcweir             const long      nNewWidth1 = nNewWidth - 1L;
1043cdf0e10cSrcweir             const long      nWidth1 = pReadAcc->Width() - 1L;
1044cdf0e10cSrcweir             const double    fRevScaleX = (double) nWidth1 / nNewWidth1;
1045cdf0e10cSrcweir 
1046cdf0e10cSrcweir             pLutInt = new long[ nNewWidth ];
1047cdf0e10cSrcweir             pLutFrac = new long[ nNewWidth ];
1048cdf0e10cSrcweir 
1049cdf0e10cSrcweir             for( nX = 0L, nTemp = nWidth - 2L; nX < nNewWidth; nX++ )
1050cdf0e10cSrcweir             {
1051cdf0e10cSrcweir                 fTemp = nX * fRevScaleX;
1052cdf0e10cSrcweir                 pLutInt[ nX ] = MinMax( (long) fTemp, 0, nTemp );
1053cdf0e10cSrcweir                 fTemp -= pLutInt[ nX ];
1054cdf0e10cSrcweir                 pLutFrac[ nX ] = (long) ( fTemp * 1024. );
1055cdf0e10cSrcweir             }
1056cdf0e10cSrcweir 
1057cdf0e10cSrcweir             if( pReadAcc->HasPalette() )
1058cdf0e10cSrcweir             {
1059cdf0e10cSrcweir                 for( nY = 0L; nY < nHeight; nY++ )
1060cdf0e10cSrcweir                 {
1061cdf0e10cSrcweir                     if( 1 == nWidth )
1062cdf0e10cSrcweir                     {
1063*87bc88d3SHerbert Dürr                         aCol0 = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nY, 0 ) );
1064cdf0e10cSrcweir 
1065cdf0e10cSrcweir                         for( nX = 0L; nX < nNewWidth; nX++ )
1066cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aCol0 );
1067cdf0e10cSrcweir                     }
1068cdf0e10cSrcweir                     else
1069cdf0e10cSrcweir                     {
1070cdf0e10cSrcweir                         for( nX = 0L; nX < nNewWidth; nX++ )
1071cdf0e10cSrcweir                         {
1072cdf0e10cSrcweir                             nTemp = pLutInt[ nX ];
1073cdf0e10cSrcweir 
1074*87bc88d3SHerbert Dürr                             aCol0 = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nY, nTemp++ ) );
1075*87bc88d3SHerbert Dürr                             aCol1 = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nY, nTemp ) );
1076cdf0e10cSrcweir 
1077cdf0e10cSrcweir                             nTemp = pLutFrac[ nX ];
1078cdf0e10cSrcweir 
1079cdf0e10cSrcweir                             lXR1 = aCol1.GetRed() - ( lXR0 = aCol0.GetRed() );
1080cdf0e10cSrcweir                             lXG1 = aCol1.GetGreen() - ( lXG0 = aCol0.GetGreen() );
1081cdf0e10cSrcweir                             lXB1 = aCol1.GetBlue() - ( lXB0 = aCol0.GetBlue() );
1082cdf0e10cSrcweir 
1083cdf0e10cSrcweir                             aCol0.SetRed( (sal_uInt8) ( ( lXR1 * nTemp + ( lXR0 << 10 ) ) >> 10 ) );
1084cdf0e10cSrcweir                             aCol0.SetGreen( (sal_uInt8) ( ( lXG1 * nTemp + ( lXG0 << 10 ) ) >> 10 ) );
1085cdf0e10cSrcweir                             aCol0.SetBlue( (sal_uInt8) ( ( lXB1 * nTemp + ( lXB0 << 10 ) ) >> 10 ) );
1086cdf0e10cSrcweir 
1087cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aCol0 );
1088cdf0e10cSrcweir                         }
1089cdf0e10cSrcweir                     }
1090cdf0e10cSrcweir                 }
1091cdf0e10cSrcweir             }
1092cdf0e10cSrcweir             else
1093cdf0e10cSrcweir             {
1094cdf0e10cSrcweir                 for( nY = 0L; nY < nHeight; nY++ )
1095cdf0e10cSrcweir                 {
1096cdf0e10cSrcweir                     if( 1 == nWidth )
1097cdf0e10cSrcweir                     {
1098cdf0e10cSrcweir                         aCol0 = pReadAcc->GetPixel( nY, 0 );
1099cdf0e10cSrcweir 
1100cdf0e10cSrcweir                         for( nX = 0L; nX < nNewWidth; nX++ )
1101cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aCol0 );
1102cdf0e10cSrcweir                     }
1103cdf0e10cSrcweir                     else
1104cdf0e10cSrcweir                     {
1105cdf0e10cSrcweir                         for( nX = 0L; nX < nNewWidth; nX++ )
1106cdf0e10cSrcweir                         {
1107cdf0e10cSrcweir                             nTemp = pLutInt[ nX ];
1108cdf0e10cSrcweir 
1109cdf0e10cSrcweir                             aCol0 = pReadAcc->GetPixel( nY, nTemp++ );
1110cdf0e10cSrcweir                             aCol1 = pReadAcc->GetPixel( nY, nTemp );
1111cdf0e10cSrcweir 
1112cdf0e10cSrcweir                             nTemp = pLutFrac[ nX ];
1113cdf0e10cSrcweir 
1114cdf0e10cSrcweir                             lXR1 = aCol1.GetRed() - ( lXR0 = aCol0.GetRed() );
1115cdf0e10cSrcweir                             lXG1 = aCol1.GetGreen() - ( lXG0 = aCol0.GetGreen() );
1116cdf0e10cSrcweir                             lXB1 = aCol1.GetBlue() - ( lXB0 = aCol0.GetBlue() );
1117cdf0e10cSrcweir 
1118cdf0e10cSrcweir                             aCol0.SetRed( (sal_uInt8) ( ( lXR1 * nTemp + ( lXR0 << 10 ) ) >> 10 ) );
1119cdf0e10cSrcweir                             aCol0.SetGreen( (sal_uInt8) ( ( lXG1 * nTemp + ( lXG0 << 10 ) ) >> 10 ) );
1120cdf0e10cSrcweir                             aCol0.SetBlue( (sal_uInt8) ( ( lXB1 * nTemp + ( lXB0 << 10 ) ) >> 10 ) );
1121cdf0e10cSrcweir 
1122cdf0e10cSrcweir                             pWriteAcc->SetPixel( nY, nX, aCol0 );
1123cdf0e10cSrcweir                         }
1124cdf0e10cSrcweir                     }
1125cdf0e10cSrcweir                 }
1126cdf0e10cSrcweir             }
1127cdf0e10cSrcweir 
1128cdf0e10cSrcweir             delete[] pLutInt;
1129cdf0e10cSrcweir             delete[] pLutFrac;
1130cdf0e10cSrcweir             bRet = sal_True;
1131cdf0e10cSrcweir         }
1132cdf0e10cSrcweir 
1133cdf0e10cSrcweir         ReleaseAccess( pReadAcc );
1134cdf0e10cSrcweir         aNewBmp.ReleaseAccess( pWriteAcc );
1135cdf0e10cSrcweir 
1136cdf0e10cSrcweir         if( bRet )
1137cdf0e10cSrcweir         {
1138cdf0e10cSrcweir             bRet = sal_False;
1139cdf0e10cSrcweir             ImplAssignWithSize( aNewBmp );
1140cdf0e10cSrcweir             pReadAcc = AcquireReadAccess();
1141cdf0e10cSrcweir             aNewBmp = Bitmap( Size( nNewWidth, nNewHeight ), 24 );
1142cdf0e10cSrcweir             pWriteAcc = aNewBmp.AcquireWriteAccess();
1143cdf0e10cSrcweir 
1144cdf0e10cSrcweir             if( pReadAcc && pWriteAcc )
1145cdf0e10cSrcweir             {
1146cdf0e10cSrcweir                 const long      nNewHeight1 = nNewHeight - 1L;
1147cdf0e10cSrcweir                 const long      nHeight1 = pReadAcc->Height() - 1L;
1148cdf0e10cSrcweir                 const double    fRevScaleY = (double) nHeight1 / nNewHeight1;
1149cdf0e10cSrcweir 
1150cdf0e10cSrcweir                 pLutInt = new long[ nNewHeight ];
1151cdf0e10cSrcweir                 pLutFrac = new long[ nNewHeight ];
1152cdf0e10cSrcweir 
1153cdf0e10cSrcweir                 for( nY = 0L, nTemp = nHeight - 2L; nY < nNewHeight; nY++ )
1154cdf0e10cSrcweir                 {
1155cdf0e10cSrcweir                     fTemp = nY * fRevScaleY;
1156cdf0e10cSrcweir                     pLutInt[ nY ] = MinMax( (long) fTemp, 0, nTemp );
1157cdf0e10cSrcweir                     fTemp -= pLutInt[ nY ];
1158cdf0e10cSrcweir                     pLutFrac[ nY ] = (long) ( fTemp * 1024. );
1159cdf0e10cSrcweir                 }
1160cdf0e10cSrcweir 
1161cdf0e10cSrcweir                 if( pReadAcc->HasPalette() )
1162cdf0e10cSrcweir                 {
1163cdf0e10cSrcweir                     for( nX = 0L; nX < nNewWidth; nX++ )
1164cdf0e10cSrcweir                     {
1165cdf0e10cSrcweir                         if( 1 == nHeight )
1166cdf0e10cSrcweir                         {
1167*87bc88d3SHerbert Dürr                             aCol0 = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( 0, nX ) );
1168cdf0e10cSrcweir 
1169cdf0e10cSrcweir                             for( nY = 0L; nY < nNewHeight; nY++ )
1170cdf0e10cSrcweir                                 pWriteAcc->SetPixel( nY, nX, aCol0 );
1171cdf0e10cSrcweir                         }
1172cdf0e10cSrcweir                         else
1173cdf0e10cSrcweir                         {
1174cdf0e10cSrcweir                             for( nY = 0L; nY < nNewHeight; nY++ )
1175cdf0e10cSrcweir                             {
1176cdf0e10cSrcweir                                 nTemp = pLutInt[ nY ];
1177cdf0e10cSrcweir 
1178*87bc88d3SHerbert Dürr                                 aCol0 = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nTemp++, nX ) );
1179*87bc88d3SHerbert Dürr                                 aCol1 = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nTemp, nX ) );
1180cdf0e10cSrcweir 
1181cdf0e10cSrcweir                                 nTemp = pLutFrac[ nY ];
1182cdf0e10cSrcweir 
1183cdf0e10cSrcweir                                 lXR1 = aCol1.GetRed() - ( lXR0 = aCol0.GetRed() );
1184cdf0e10cSrcweir                                 lXG1 = aCol1.GetGreen() - ( lXG0 = aCol0.GetGreen() );
1185cdf0e10cSrcweir                                 lXB1 = aCol1.GetBlue() - ( lXB0 = aCol0.GetBlue() );
1186cdf0e10cSrcweir 
1187cdf0e10cSrcweir                                 aCol0.SetRed( (sal_uInt8) ( ( lXR1 * nTemp + ( lXR0 << 10 ) ) >> 10 ) );
1188cdf0e10cSrcweir                                 aCol0.SetGreen( (sal_uInt8) ( ( lXG1 * nTemp + ( lXG0 << 10 ) ) >> 10 ) );
1189cdf0e10cSrcweir                                 aCol0.SetBlue( (sal_uInt8) ( ( lXB1 * nTemp + ( lXB0 << 10 ) ) >> 10 ) );
1190cdf0e10cSrcweir 
1191cdf0e10cSrcweir                                 pWriteAcc->SetPixel( nY, nX, aCol0 );
1192cdf0e10cSrcweir                             }
1193cdf0e10cSrcweir                         }
1194cdf0e10cSrcweir                     }
1195cdf0e10cSrcweir                 }
1196cdf0e10cSrcweir                 else
1197cdf0e10cSrcweir                 {
1198cdf0e10cSrcweir                     for( nX = 0L; nX < nNewWidth; nX++ )
1199cdf0e10cSrcweir                     {
1200cdf0e10cSrcweir                         if( 1 == nHeight )
1201cdf0e10cSrcweir                         {
1202cdf0e10cSrcweir                             aCol0 = pReadAcc->GetPixel( 0, nX );
1203cdf0e10cSrcweir 
1204cdf0e10cSrcweir                             for( nY = 0L; nY < nNewHeight; nY++ )
1205cdf0e10cSrcweir                                 pWriteAcc->SetPixel( nY, nX, aCol0 );
1206cdf0e10cSrcweir                         }
1207cdf0e10cSrcweir                         else
1208cdf0e10cSrcweir                         {
1209cdf0e10cSrcweir                             for( nY = 0L; nY < nNewHeight; nY++ )
1210cdf0e10cSrcweir                             {
1211cdf0e10cSrcweir                                 nTemp = pLutInt[ nY ];
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir                                 aCol0 = pReadAcc->GetPixel( nTemp++, nX );
1214cdf0e10cSrcweir                                 aCol1 = pReadAcc->GetPixel( nTemp, nX );
1215cdf0e10cSrcweir 
1216cdf0e10cSrcweir                                 nTemp = pLutFrac[ nY ];
1217cdf0e10cSrcweir 
1218cdf0e10cSrcweir                                 lXR1 = aCol1.GetRed() - ( lXR0 = aCol0.GetRed() );
1219cdf0e10cSrcweir                                 lXG1 = aCol1.GetGreen() - ( lXG0 = aCol0.GetGreen() );
1220cdf0e10cSrcweir                                 lXB1 = aCol1.GetBlue() - ( lXB0 = aCol0.GetBlue() );
1221cdf0e10cSrcweir 
1222cdf0e10cSrcweir                                 aCol0.SetRed( (sal_uInt8) ( ( lXR1 * nTemp + ( lXR0 << 10 ) ) >> 10 ) );
1223cdf0e10cSrcweir                                 aCol0.SetGreen( (sal_uInt8) ( ( lXG1 * nTemp + ( lXG0 << 10 ) ) >> 10 ) );
1224cdf0e10cSrcweir                                 aCol0.SetBlue( (sal_uInt8) ( ( lXB1 * nTemp + ( lXB0 << 10 ) ) >> 10 ) );
1225cdf0e10cSrcweir 
1226cdf0e10cSrcweir                                 pWriteAcc->SetPixel( nY, nX, aCol0 );
1227cdf0e10cSrcweir                             }
1228cdf0e10cSrcweir                         }
1229cdf0e10cSrcweir                     }
1230cdf0e10cSrcweir                 }
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir                 delete[] pLutInt;
1233cdf0e10cSrcweir                 delete[] pLutFrac;
1234cdf0e10cSrcweir                 bRet = sal_True;
1235cdf0e10cSrcweir             }
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir             ReleaseAccess( pReadAcc );
1238cdf0e10cSrcweir             aNewBmp.ReleaseAccess( pWriteAcc );
1239cdf0e10cSrcweir 
1240cdf0e10cSrcweir             if( bRet )
1241cdf0e10cSrcweir                 ImplAssignWithSize( aNewBmp );
1242cdf0e10cSrcweir         }
1243cdf0e10cSrcweir     }
1244cdf0e10cSrcweir 
1245cdf0e10cSrcweir     if( !bRet )
1246cdf0e10cSrcweir         bRet = ImplScaleFast( rScaleX, rScaleY );
1247cdf0e10cSrcweir 
1248cdf0e10cSrcweir     return bRet;
1249cdf0e10cSrcweir }
1250cdf0e10cSrcweir 
1251cdf0e10cSrcweir // ------------------------------------------------------------------------
1252cdf0e10cSrcweir 
1253cdf0e10cSrcweir sal_Bool Bitmap::Dither( sal_uLong nDitherFlags )
1254cdf0e10cSrcweir {
1255cdf0e10cSrcweir     sal_Bool bRet = sal_False;
1256cdf0e10cSrcweir 
1257cdf0e10cSrcweir     const Size aSizePix( GetSizePixel() );
1258cdf0e10cSrcweir 
1259cdf0e10cSrcweir     if( aSizePix.Width() == 1 || aSizePix.Height() == 1 )
1260cdf0e10cSrcweir         bRet = sal_True;
1261cdf0e10cSrcweir     else if( nDitherFlags & BMP_DITHER_MATRIX )
1262cdf0e10cSrcweir         bRet = ImplDitherMatrix();
1263cdf0e10cSrcweir     else if( nDitherFlags & BMP_DITHER_FLOYD )
1264cdf0e10cSrcweir         bRet = ImplDitherFloyd();
1265cdf0e10cSrcweir     else if( ( nDitherFlags & BMP_DITHER_FLOYD_16 ) && ( GetBitCount() == 24 ) )
1266cdf0e10cSrcweir         bRet = ImplDitherFloyd16();
1267cdf0e10cSrcweir 
1268cdf0e10cSrcweir     return bRet;
1269cdf0e10cSrcweir }
1270cdf0e10cSrcweir 
1271cdf0e10cSrcweir // ------------------------------------------------------------------------
1272cdf0e10cSrcweir 
1273cdf0e10cSrcweir sal_Bool Bitmap::ImplDitherMatrix()
1274cdf0e10cSrcweir {
1275cdf0e10cSrcweir     BitmapReadAccess*   pReadAcc = AcquireReadAccess();
1276cdf0e10cSrcweir     Bitmap              aNewBmp( GetSizePixel(), 8 );
1277cdf0e10cSrcweir     BitmapWriteAccess*  pWriteAcc = aNewBmp.AcquireWriteAccess();
1278cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
1279cdf0e10cSrcweir 
1280cdf0e10cSrcweir     if( pReadAcc && pWriteAcc )
1281cdf0e10cSrcweir     {
1282cdf0e10cSrcweir         const sal_uLong nWidth = pReadAcc->Width();
1283cdf0e10cSrcweir         const sal_uLong nHeight = pReadAcc->Height();
1284cdf0e10cSrcweir         BitmapColor aIndex( (sal_uInt8) 0 );
1285cdf0e10cSrcweir 
1286cdf0e10cSrcweir         if( pReadAcc->HasPalette() )
1287cdf0e10cSrcweir         {
1288cdf0e10cSrcweir             for( sal_uLong nY = 0UL; nY < nHeight; nY++ )
1289cdf0e10cSrcweir             {
1290cdf0e10cSrcweir                 for( sal_uLong nX = 0UL, nModY = ( nY & 0x0FUL ) << 4UL; nX < nWidth; nX++ )
1291cdf0e10cSrcweir                 {
1292*87bc88d3SHerbert Dürr                     const BitmapColor   aCol( pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nY, nX ) ) );
1293cdf0e10cSrcweir                     const sal_uLong         nD = nVCLDitherLut[ nModY + ( nX & 0x0FUL ) ];
1294cdf0e10cSrcweir                     const sal_uLong         nR = ( nVCLLut[ aCol.GetRed() ] + nD ) >> 16UL;
1295cdf0e10cSrcweir                     const sal_uLong         nG = ( nVCLLut[ aCol.GetGreen() ] + nD ) >> 16UL;
1296cdf0e10cSrcweir                     const sal_uLong         nB = ( nVCLLut[ aCol.GetBlue() ] + nD ) >> 16UL;
1297cdf0e10cSrcweir 
1298cdf0e10cSrcweir                     aIndex.SetIndex( (sal_uInt8) ( nVCLRLut[ nR ] + nVCLGLut[ nG ] + nVCLBLut[ nB ] ) );
1299cdf0e10cSrcweir                     pWriteAcc->SetPixel( nY, nX, aIndex );
1300cdf0e10cSrcweir                 }
1301cdf0e10cSrcweir             }
1302cdf0e10cSrcweir         }
1303cdf0e10cSrcweir         else
1304cdf0e10cSrcweir         {
1305cdf0e10cSrcweir             for( sal_uLong nY = 0UL; nY < nHeight; nY++ )
1306cdf0e10cSrcweir             {
1307cdf0e10cSrcweir                 for( sal_uLong nX = 0UL, nModY = ( nY & 0x0FUL ) << 4UL; nX < nWidth; nX++ )
1308cdf0e10cSrcweir                 {
1309cdf0e10cSrcweir                     const BitmapColor   aCol( pReadAcc->GetPixel( nY, nX ) );
1310cdf0e10cSrcweir                     const sal_uLong         nD = nVCLDitherLut[ nModY + ( nX & 0x0FUL ) ];
1311cdf0e10cSrcweir                     const sal_uLong         nR = ( nVCLLut[ aCol.GetRed() ] + nD ) >> 16UL;
1312cdf0e10cSrcweir                     const sal_uLong         nG = ( nVCLLut[ aCol.GetGreen() ] + nD ) >> 16UL;
1313cdf0e10cSrcweir                     const sal_uLong         nB = ( nVCLLut[ aCol.GetBlue() ] + nD ) >> 16UL;
1314cdf0e10cSrcweir 
1315cdf0e10cSrcweir                     aIndex.SetIndex( (sal_uInt8) ( nVCLRLut[ nR ] + nVCLGLut[ nG ] + nVCLBLut[ nB ] ) );
1316cdf0e10cSrcweir                     pWriteAcc->SetPixel( nY, nX, aIndex );
1317cdf0e10cSrcweir                 }
1318cdf0e10cSrcweir             }
1319cdf0e10cSrcweir         }
1320cdf0e10cSrcweir 
1321cdf0e10cSrcweir         bRet = sal_True;
1322cdf0e10cSrcweir     }
1323cdf0e10cSrcweir 
1324cdf0e10cSrcweir     ReleaseAccess( pReadAcc );
1325cdf0e10cSrcweir     aNewBmp.ReleaseAccess( pWriteAcc );
1326cdf0e10cSrcweir 
1327cdf0e10cSrcweir     if( bRet )
1328cdf0e10cSrcweir     {
1329cdf0e10cSrcweir         const MapMode   aMap( maPrefMapMode );
1330cdf0e10cSrcweir         const Size      aSize( maPrefSize );
1331cdf0e10cSrcweir 
1332cdf0e10cSrcweir         *this = aNewBmp;
1333cdf0e10cSrcweir 
1334cdf0e10cSrcweir         maPrefMapMode = aMap;
1335cdf0e10cSrcweir         maPrefSize = aSize;
1336cdf0e10cSrcweir     }
1337cdf0e10cSrcweir 
1338cdf0e10cSrcweir     return bRet;
1339cdf0e10cSrcweir }
1340cdf0e10cSrcweir 
1341cdf0e10cSrcweir // ------------------------------------------------------------------------
1342cdf0e10cSrcweir 
1343cdf0e10cSrcweir sal_Bool Bitmap::ImplDitherFloyd()
1344cdf0e10cSrcweir {
1345cdf0e10cSrcweir     const Size  aSize( GetSizePixel() );
1346cdf0e10cSrcweir     sal_Bool        bRet = sal_False;
1347cdf0e10cSrcweir 
1348cdf0e10cSrcweir     if( ( aSize.Width() > 3 ) && ( aSize.Height() > 2 ) )
1349cdf0e10cSrcweir     {
1350cdf0e10cSrcweir         BitmapReadAccess*   pReadAcc = AcquireReadAccess();
1351cdf0e10cSrcweir         Bitmap              aNewBmp( GetSizePixel(), 8 );
1352cdf0e10cSrcweir         BitmapWriteAccess*  pWriteAcc = aNewBmp.AcquireWriteAccess();
1353cdf0e10cSrcweir 
1354cdf0e10cSrcweir         if( pReadAcc && pWriteAcc )
1355cdf0e10cSrcweir         {
1356cdf0e10cSrcweir             BitmapColor aColor;
1357cdf0e10cSrcweir             long        nWidth = pReadAcc->Width();
1358cdf0e10cSrcweir             long        nWidth1 = nWidth - 1L;
1359cdf0e10cSrcweir             long        nHeight = pReadAcc->Height();
1360cdf0e10cSrcweir             long        nX;
1361cdf0e10cSrcweir             long        nW = nWidth * 3L;
1362cdf0e10cSrcweir             long        nW2 = nW - 3L;
1363cdf0e10cSrcweir             long        nRErr, nGErr, nBErr;
1364cdf0e10cSrcweir             long        nRC, nGC, nBC;
1365cdf0e10cSrcweir             long        nTemp;
1366cdf0e10cSrcweir             long        nZ;
1367cdf0e10cSrcweir             long*       p1 = new long[ nW ];
1368cdf0e10cSrcweir             long*       p2 = new long[ nW ];
1369cdf0e10cSrcweir             long*       p1T = p1;
1370cdf0e10cSrcweir             long*       p2T = p2;
1371cdf0e10cSrcweir             long*       pTmp;
1372cdf0e10cSrcweir             sal_Bool        bPal = pReadAcc->HasPalette();
1373cdf0e10cSrcweir 
1374cdf0e10cSrcweir             pTmp = p2T;
1375cdf0e10cSrcweir 
1376cdf0e10cSrcweir             if( bPal )
1377cdf0e10cSrcweir             {
1378cdf0e10cSrcweir                 for( nZ = 0; nZ < nWidth; nZ++ )
1379cdf0e10cSrcweir                 {
1380*87bc88d3SHerbert Dürr                     aColor = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( 0, nZ ) );
1381cdf0e10cSrcweir 
1382cdf0e10cSrcweir                     *pTmp++ = (long) aColor.GetBlue() << 12;
1383cdf0e10cSrcweir                     *pTmp++ = (long) aColor.GetGreen() << 12;
1384cdf0e10cSrcweir                     *pTmp++ = (long) aColor.GetRed() << 12;
1385cdf0e10cSrcweir                 }
1386cdf0e10cSrcweir             }
1387cdf0e10cSrcweir             else
1388cdf0e10cSrcweir             {
1389cdf0e10cSrcweir                 for( nZ = 0; nZ < nWidth; nZ++ )
1390cdf0e10cSrcweir                 {
1391cdf0e10cSrcweir                     aColor = pReadAcc->GetPixel( 0, nZ );
1392cdf0e10cSrcweir 
1393cdf0e10cSrcweir                     *pTmp++ = (long) aColor.GetBlue() << 12;
1394cdf0e10cSrcweir                     *pTmp++ = (long) aColor.GetGreen() << 12;
1395cdf0e10cSrcweir                     *pTmp++ = (long) aColor.GetRed() << 12;
1396cdf0e10cSrcweir                 }
1397cdf0e10cSrcweir             }
1398cdf0e10cSrcweir 
1399cdf0e10cSrcweir             for( long nY = 1, nYAcc = 0L; nY <= nHeight; nY++, nYAcc++ )
1400cdf0e10cSrcweir             {
1401cdf0e10cSrcweir                 pTmp = p1T;
1402cdf0e10cSrcweir                 p1T = p2T;
1403cdf0e10cSrcweir                 p2T = pTmp;
1404cdf0e10cSrcweir 
1405cdf0e10cSrcweir                 if( nY < nHeight )
1406cdf0e10cSrcweir                 {
1407cdf0e10cSrcweir                     if( bPal )
1408cdf0e10cSrcweir                     {
1409cdf0e10cSrcweir                         for( nZ = 0; nZ < nWidth; nZ++ )
1410cdf0e10cSrcweir                         {
1411*87bc88d3SHerbert Dürr                             aColor = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nY, nZ ) );
1412cdf0e10cSrcweir 
1413cdf0e10cSrcweir                             *pTmp++ = (long) aColor.GetBlue() << 12;
1414cdf0e10cSrcweir                             *pTmp++ = (long) aColor.GetGreen() << 12;
1415cdf0e10cSrcweir                             *pTmp++ = (long) aColor.GetRed() << 12;
1416cdf0e10cSrcweir                         }
1417cdf0e10cSrcweir                     }
1418cdf0e10cSrcweir                     else
1419cdf0e10cSrcweir                     {
1420cdf0e10cSrcweir                         for( nZ = 0; nZ < nWidth; nZ++ )
1421cdf0e10cSrcweir                         {
1422cdf0e10cSrcweir                             aColor = pReadAcc->GetPixel( nY, nZ );
1423cdf0e10cSrcweir 
1424cdf0e10cSrcweir                             *pTmp++ = (long) aColor.GetBlue() << 12;
1425cdf0e10cSrcweir                             *pTmp++ = (long) aColor.GetGreen() << 12;
1426cdf0e10cSrcweir                             *pTmp++ = (long) aColor.GetRed() << 12;
1427cdf0e10cSrcweir                         }
1428cdf0e10cSrcweir                     }
1429cdf0e10cSrcweir                 }
1430cdf0e10cSrcweir 
1431cdf0e10cSrcweir                 // erstes Pixel gesondert betrachten
1432cdf0e10cSrcweir                 nX = 0;
1433cdf0e10cSrcweir                 CALC_ERRORS;
1434cdf0e10cSrcweir                 CALC_TABLES7;
1435cdf0e10cSrcweir                 nX -= 5;
1436cdf0e10cSrcweir                 CALC_TABLES5;
1437*87bc88d3SHerbert Dürr                 pWriteAcc->SetPixelIndex( nYAcc, 0, static_cast<sal_uInt8>(nVCLBLut[ nBC ] + nVCLGLut[nGC ] + nVCLRLut[nRC ]) );
1438cdf0e10cSrcweir 
1439cdf0e10cSrcweir                 // mittlere Pixel ueber Schleife
1440cdf0e10cSrcweir                 long nXAcc;
1441cdf0e10cSrcweir                 for ( nX = 3L, nXAcc = 1L; nX < nW2; nXAcc++ )
1442cdf0e10cSrcweir                 {
1443cdf0e10cSrcweir                     CALC_ERRORS;
1444cdf0e10cSrcweir                     CALC_TABLES7;
1445cdf0e10cSrcweir                     nX -= 8;
1446cdf0e10cSrcweir                     CALC_TABLES3;
1447cdf0e10cSrcweir                     CALC_TABLES5;
1448*87bc88d3SHerbert Dürr                     pWriteAcc->SetPixelIndex( nYAcc, nXAcc, static_cast<sal_uInt8>(nVCLBLut[ nBC ] + nVCLGLut[nGC ] + nVCLRLut[nRC ]) );
1449cdf0e10cSrcweir                 }
1450cdf0e10cSrcweir 
1451cdf0e10cSrcweir                 // letztes Pixel gesondert betrachten
1452cdf0e10cSrcweir                 CALC_ERRORS;
1453cdf0e10cSrcweir                 nX -= 5;
1454cdf0e10cSrcweir                 CALC_TABLES3;
1455cdf0e10cSrcweir                 CALC_TABLES5;
1456*87bc88d3SHerbert Dürr                 pWriteAcc->SetPixelIndex( nYAcc, nWidth1, static_cast<sal_uInt8>(nVCLBLut[ nBC ] + nVCLGLut[nGC ] + nVCLRLut[nRC ]) );
1457cdf0e10cSrcweir             }
1458cdf0e10cSrcweir 
1459cdf0e10cSrcweir             delete[] p1;
1460cdf0e10cSrcweir             delete[] p2;
1461cdf0e10cSrcweir             bRet = sal_True;
1462cdf0e10cSrcweir         }
1463cdf0e10cSrcweir 
1464cdf0e10cSrcweir         ReleaseAccess( pReadAcc );
1465cdf0e10cSrcweir         aNewBmp.ReleaseAccess( pWriteAcc );
1466cdf0e10cSrcweir 
1467cdf0e10cSrcweir         if( bRet )
1468cdf0e10cSrcweir         {
1469cdf0e10cSrcweir             const MapMode   aMap( maPrefMapMode );
1470cdf0e10cSrcweir             const Size      aPrefSize( maPrefSize );
1471cdf0e10cSrcweir 
1472cdf0e10cSrcweir             *this = aNewBmp;
1473cdf0e10cSrcweir 
1474cdf0e10cSrcweir             maPrefMapMode = aMap;
1475cdf0e10cSrcweir             maPrefSize = aPrefSize;
1476cdf0e10cSrcweir         }
1477cdf0e10cSrcweir     }
1478cdf0e10cSrcweir 
1479cdf0e10cSrcweir     return bRet;
1480cdf0e10cSrcweir }
1481cdf0e10cSrcweir 
1482cdf0e10cSrcweir // ------------------------------------------------------------------------
1483cdf0e10cSrcweir 
1484cdf0e10cSrcweir sal_Bool Bitmap::ImplDitherFloyd16()
1485cdf0e10cSrcweir {
1486cdf0e10cSrcweir     BitmapReadAccess*   pReadAcc = AcquireReadAccess();
1487cdf0e10cSrcweir     Bitmap              aNewBmp( GetSizePixel(), 24 );
1488cdf0e10cSrcweir     BitmapWriteAccess*  pWriteAcc = aNewBmp.AcquireWriteAccess();
1489cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
1490cdf0e10cSrcweir 
1491cdf0e10cSrcweir     if( pReadAcc && pWriteAcc )
1492cdf0e10cSrcweir     {
1493cdf0e10cSrcweir         const long      nWidth = pWriteAcc->Width();
1494cdf0e10cSrcweir         const long      nWidth1 = nWidth - 1L;
1495cdf0e10cSrcweir         const long      nHeight = pWriteAcc->Height();
1496cdf0e10cSrcweir         BitmapColor     aColor;
1497cdf0e10cSrcweir         BitmapColor     aBestCol;
1498cdf0e10cSrcweir         ImpErrorQuad    aErrQuad;
1499cdf0e10cSrcweir         ImpErrorQuad*   pErrQuad1 = new ImpErrorQuad[ nWidth ];
1500cdf0e10cSrcweir         ImpErrorQuad*   pErrQuad2 = new ImpErrorQuad[ nWidth ];
1501cdf0e10cSrcweir         ImpErrorQuad*   pQLine1 = pErrQuad1;
1502cdf0e10cSrcweir         ImpErrorQuad*   pQLine2 = 0;
1503cdf0e10cSrcweir         long            nX, nY;
1504cdf0e10cSrcweir         long            nYTmp = 0L;
1505cdf0e10cSrcweir         sal_Bool            bQ1 = sal_True;
1506cdf0e10cSrcweir 
1507cdf0e10cSrcweir         for( nY = 0L; nY < Min( nHeight, 2L ); nY++, nYTmp++ )
1508cdf0e10cSrcweir             for( nX = 0L, pQLine2 = !nY ? pErrQuad1 : pErrQuad2; nX < nWidth; nX++ )
1509cdf0e10cSrcweir                 pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
1510cdf0e10cSrcweir 
1511cdf0e10cSrcweir         for( nY = 0L; nY < nHeight; nY++, nYTmp++ )
1512cdf0e10cSrcweir         {
1513cdf0e10cSrcweir             // erstes ZeilenPixel
1514cdf0e10cSrcweir             aBestCol = pQLine1[ 0 ].ImplGetColor();
1515cdf0e10cSrcweir             aBestCol.SetRed( ( aBestCol.GetRed() & 248 ) | 7 );
1516cdf0e10cSrcweir             aBestCol.SetGreen( ( aBestCol.GetGreen() & 248 ) | 7 );
1517cdf0e10cSrcweir             aBestCol.SetBlue( ( aBestCol.GetBlue() & 248 ) | 7 );
1518cdf0e10cSrcweir             pWriteAcc->SetPixel( nY, 0, aBestCol );
1519cdf0e10cSrcweir 
1520cdf0e10cSrcweir             for( nX = 1L; nX < nWidth1; nX++ )
1521cdf0e10cSrcweir             {
1522cdf0e10cSrcweir                 aColor = pQLine1[ nX ].ImplGetColor();
1523cdf0e10cSrcweir                 aBestCol.SetRed( ( aColor.GetRed() & 248 ) | 7 );
1524cdf0e10cSrcweir                 aBestCol.SetGreen( ( aColor.GetGreen() & 248 ) | 7 );
1525cdf0e10cSrcweir                 aBestCol.SetBlue( ( aColor.GetBlue() & 248 ) | 7 );
1526cdf0e10cSrcweir                 aErrQuad = ( ImpErrorQuad( aColor ) -= aBestCol );
1527cdf0e10cSrcweir                 pQLine1[ ++nX ].ImplAddColorError7( aErrQuad );
1528cdf0e10cSrcweir                 pQLine2[ nX-- ].ImplAddColorError1( aErrQuad );
1529cdf0e10cSrcweir                 pQLine2[ nX-- ].ImplAddColorError5( aErrQuad );
1530cdf0e10cSrcweir                 pQLine2[ nX++ ].ImplAddColorError3( aErrQuad );
1531cdf0e10cSrcweir                 pWriteAcc->SetPixel( nY, nX, aBestCol );
1532cdf0e10cSrcweir             }
1533cdf0e10cSrcweir 
1534cdf0e10cSrcweir             // letztes ZeilenPixel
1535cdf0e10cSrcweir             aBestCol = pQLine1[ nWidth1 ].ImplGetColor();
1536cdf0e10cSrcweir             aBestCol.SetRed( ( aBestCol.GetRed() & 248 ) | 7 );
1537cdf0e10cSrcweir             aBestCol.SetGreen( ( aBestCol.GetGreen() & 248 ) | 7 );
1538cdf0e10cSrcweir             aBestCol.SetBlue( ( aBestCol.GetBlue() & 248 ) | 7 );
1539cdf0e10cSrcweir             pWriteAcc->SetPixel( nY, nX, aBestCol );
1540cdf0e10cSrcweir 
1541cdf0e10cSrcweir             // Zeilenpuffer neu fuellen/kopieren
1542cdf0e10cSrcweir             pQLine1 = pQLine2;
1543cdf0e10cSrcweir             pQLine2 = ( bQ1 = !bQ1 ) != sal_False ? pErrQuad2 : pErrQuad1;
1544cdf0e10cSrcweir 
1545cdf0e10cSrcweir             if( nYTmp < nHeight )
1546cdf0e10cSrcweir                 for( nX = 0L; nX < nWidth; nX++ )
1547cdf0e10cSrcweir                     pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
1548cdf0e10cSrcweir         }
1549cdf0e10cSrcweir 
1550cdf0e10cSrcweir         // Zeilenpuffer zerstoeren
1551cdf0e10cSrcweir         delete[] pErrQuad1;
1552cdf0e10cSrcweir         delete[] pErrQuad2;
1553cdf0e10cSrcweir         bRet = sal_True;
1554cdf0e10cSrcweir     }
1555cdf0e10cSrcweir 
1556cdf0e10cSrcweir     ReleaseAccess( pReadAcc );
1557cdf0e10cSrcweir     aNewBmp.ReleaseAccess( pWriteAcc );
1558cdf0e10cSrcweir 
1559cdf0e10cSrcweir     if( bRet )
1560cdf0e10cSrcweir     {
1561cdf0e10cSrcweir         const MapMode   aMap( maPrefMapMode );
1562cdf0e10cSrcweir         const Size      aSize( maPrefSize );
1563cdf0e10cSrcweir 
1564cdf0e10cSrcweir         *this = aNewBmp;
1565cdf0e10cSrcweir 
1566cdf0e10cSrcweir         maPrefMapMode = aMap;
1567cdf0e10cSrcweir         maPrefSize = aSize;
1568cdf0e10cSrcweir     }
1569cdf0e10cSrcweir 
1570cdf0e10cSrcweir     return bRet;
1571cdf0e10cSrcweir }
1572cdf0e10cSrcweir 
1573cdf0e10cSrcweir // ------------------------------------------------------------------------
1574cdf0e10cSrcweir 
1575cdf0e10cSrcweir sal_Bool Bitmap::ReduceColors( sal_uInt16 nColorCount, BmpReduce eReduce )
1576cdf0e10cSrcweir {
1577cdf0e10cSrcweir     sal_Bool bRet;
1578cdf0e10cSrcweir 
1579cdf0e10cSrcweir     if( GetColorCount() <= (sal_uLong) nColorCount )
1580cdf0e10cSrcweir         bRet = sal_True;
1581cdf0e10cSrcweir     else if( nColorCount )
1582cdf0e10cSrcweir     {
1583cdf0e10cSrcweir         if( BMP_REDUCE_SIMPLE == eReduce )
1584cdf0e10cSrcweir             bRet = ImplReduceSimple( nColorCount );
1585cdf0e10cSrcweir         else if( BMP_REDUCE_POPULAR == eReduce )
1586cdf0e10cSrcweir             bRet = ImplReducePopular( nColorCount );
1587cdf0e10cSrcweir         else
1588cdf0e10cSrcweir             bRet = ImplReduceMedian( nColorCount );
1589cdf0e10cSrcweir     }
1590cdf0e10cSrcweir     else
1591cdf0e10cSrcweir         bRet = sal_False;
1592cdf0e10cSrcweir 
1593cdf0e10cSrcweir     return bRet;
1594cdf0e10cSrcweir }
1595cdf0e10cSrcweir 
1596cdf0e10cSrcweir // ------------------------------------------------------------------------
1597cdf0e10cSrcweir 
1598cdf0e10cSrcweir sal_Bool Bitmap::ImplReduceSimple( sal_uInt16 nColorCount )
1599cdf0e10cSrcweir {
1600cdf0e10cSrcweir     Bitmap              aNewBmp;
1601cdf0e10cSrcweir     BitmapReadAccess*   pRAcc = AcquireReadAccess();
1602cdf0e10cSrcweir     const sal_uInt16        nColCount = Min( nColorCount, (sal_uInt16) 256 );
1603cdf0e10cSrcweir     sal_uInt16              nBitCount;
1604cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
1605cdf0e10cSrcweir 
1606cdf0e10cSrcweir     if( nColCount <= 2 )
1607cdf0e10cSrcweir         nBitCount = 1;
1608cdf0e10cSrcweir     else if( nColCount <= 16 )
1609cdf0e10cSrcweir         nBitCount = 4;
1610cdf0e10cSrcweir     else
1611cdf0e10cSrcweir         nBitCount = 8;
1612cdf0e10cSrcweir 
1613cdf0e10cSrcweir     if( pRAcc )
1614cdf0e10cSrcweir     {
1615cdf0e10cSrcweir         Octree                  aOct( *pRAcc, nColCount );
1616cdf0e10cSrcweir         const BitmapPalette&    rPal = aOct.GetPalette();
1617cdf0e10cSrcweir         BitmapWriteAccess*      pWAcc;
1618cdf0e10cSrcweir 
1619cdf0e10cSrcweir         aNewBmp = Bitmap( GetSizePixel(), nBitCount, &rPal );
1620cdf0e10cSrcweir         pWAcc = aNewBmp.AcquireWriteAccess();
1621cdf0e10cSrcweir 
1622cdf0e10cSrcweir         if( pWAcc )
1623cdf0e10cSrcweir         {
1624cdf0e10cSrcweir             const long nWidth = pRAcc->Width();
1625cdf0e10cSrcweir             const long nHeight = pRAcc->Height();
1626cdf0e10cSrcweir 
1627cdf0e10cSrcweir             if( pRAcc->HasPalette() )
1628cdf0e10cSrcweir             {
1629cdf0e10cSrcweir                 for( long nY = 0L; nY < nHeight; nY++ )
1630cdf0e10cSrcweir                     for( long nX =0L; nX < nWidth; nX++ )
1631*87bc88d3SHerbert Dürr                         pWAcc->SetPixelIndex( nY, nX, static_cast<sal_uInt8>(aOct.GetBestPaletteIndex( pRAcc->GetPaletteColor( pRAcc->GetPixelIndex( nY, nX ) ))) );
1632cdf0e10cSrcweir             }
1633cdf0e10cSrcweir             else
1634cdf0e10cSrcweir             {
1635cdf0e10cSrcweir                 for( long nY = 0L; nY < nHeight; nY++ )
1636cdf0e10cSrcweir                     for( long nX =0L; nX < nWidth; nX++ )
1637*87bc88d3SHerbert Dürr                         pWAcc->SetPixelIndex( nY, nX, static_cast<sal_uInt8>(aOct.GetBestPaletteIndex( pRAcc->GetPixel( nY, nX ) )) );
1638cdf0e10cSrcweir             }
1639cdf0e10cSrcweir 
1640cdf0e10cSrcweir             aNewBmp.ReleaseAccess( pWAcc );
1641cdf0e10cSrcweir             bRet = sal_True;
1642cdf0e10cSrcweir         }
1643cdf0e10cSrcweir 
1644cdf0e10cSrcweir         ReleaseAccess( pRAcc );
1645cdf0e10cSrcweir     }
1646cdf0e10cSrcweir 
1647cdf0e10cSrcweir     if( bRet )
1648cdf0e10cSrcweir     {
1649cdf0e10cSrcweir         const MapMode   aMap( maPrefMapMode );
1650cdf0e10cSrcweir         const Size      aSize( maPrefSize );
1651cdf0e10cSrcweir 
1652cdf0e10cSrcweir         *this = aNewBmp;
1653cdf0e10cSrcweir         maPrefMapMode = aMap;
1654cdf0e10cSrcweir         maPrefSize = aSize;
1655cdf0e10cSrcweir     }
1656cdf0e10cSrcweir 
1657cdf0e10cSrcweir     return bRet;
1658cdf0e10cSrcweir }
1659cdf0e10cSrcweir 
1660cdf0e10cSrcweir // ------------------------------------------------------------------------
1661cdf0e10cSrcweir 
1662cdf0e10cSrcweir struct PopularColorCount
1663cdf0e10cSrcweir {
1664cdf0e10cSrcweir     sal_uInt32  mnIndex;
1665cdf0e10cSrcweir     sal_uInt32  mnCount;
1666cdf0e10cSrcweir };
1667cdf0e10cSrcweir 
1668cdf0e10cSrcweir // ------------------------------------------------------------------------
1669cdf0e10cSrcweir 
1670cdf0e10cSrcweir extern "C" int __LOADONCALLAPI ImplPopularCmpFnc( const void* p1, const void* p2 )
1671cdf0e10cSrcweir {
1672cdf0e10cSrcweir     int nRet;
1673cdf0e10cSrcweir 
1674cdf0e10cSrcweir     if( ( (PopularColorCount*) p1 )->mnCount < ( (PopularColorCount*) p2 )->mnCount )
1675cdf0e10cSrcweir         nRet = 1;
1676cdf0e10cSrcweir     else if( ( (PopularColorCount*) p1 )->mnCount == ( (PopularColorCount*) p2 )->mnCount )
1677cdf0e10cSrcweir         nRet = 0;
1678cdf0e10cSrcweir     else
1679cdf0e10cSrcweir         nRet = -1;
1680cdf0e10cSrcweir 
1681cdf0e10cSrcweir     return nRet;
1682cdf0e10cSrcweir }
1683cdf0e10cSrcweir 
1684cdf0e10cSrcweir // ------------------------------------------------------------------------
1685cdf0e10cSrcweir 
1686cdf0e10cSrcweir sal_Bool Bitmap::ImplReducePopular( sal_uInt16 nColCount )
1687cdf0e10cSrcweir {
1688cdf0e10cSrcweir     BitmapReadAccess*   pRAcc = AcquireReadAccess();
1689cdf0e10cSrcweir     sal_uInt16              nBitCount;
1690cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
1691cdf0e10cSrcweir 
1692cdf0e10cSrcweir     if( nColCount > 256 )
1693cdf0e10cSrcweir         nColCount = 256;
1694cdf0e10cSrcweir 
1695cdf0e10cSrcweir     if( nColCount < 17 )
1696cdf0e10cSrcweir         nBitCount = 4;
1697cdf0e10cSrcweir     else
1698cdf0e10cSrcweir         nBitCount = 8;
1699cdf0e10cSrcweir 
1700cdf0e10cSrcweir     if( pRAcc )
1701cdf0e10cSrcweir     {
1702cdf0e10cSrcweir         const sal_uInt32    nValidBits = 4;
1703cdf0e10cSrcweir         const sal_uInt32    nRightShiftBits = 8 - nValidBits;
1704cdf0e10cSrcweir         const sal_uInt32    nLeftShiftBits1 = nValidBits;
1705cdf0e10cSrcweir         const sal_uInt32    nLeftShiftBits2 = nValidBits << 1;
1706cdf0e10cSrcweir         const sal_uInt32    nColorsPerComponent = 1 << nValidBits;
1707cdf0e10cSrcweir         const sal_uInt32    nColorOffset = 256 / nColorsPerComponent;
1708cdf0e10cSrcweir         const sal_uInt32    nTotalColors = nColorsPerComponent * nColorsPerComponent * nColorsPerComponent;
1709cdf0e10cSrcweir         const long          nWidth = pRAcc->Width();
1710cdf0e10cSrcweir         const long          nHeight = pRAcc->Height();
1711cdf0e10cSrcweir         PopularColorCount*  pCountTable = new PopularColorCount[ nTotalColors ];
1712cdf0e10cSrcweir         long                nX, nY, nR, nG, nB, nIndex;
1713cdf0e10cSrcweir 
1714cdf0e10cSrcweir         rtl_zeroMemory( pCountTable, nTotalColors * sizeof( PopularColorCount ) );
1715cdf0e10cSrcweir 
1716cdf0e10cSrcweir         for( nR = 0, nIndex = 0; nR < 256; nR += nColorOffset )
1717cdf0e10cSrcweir         {
1718cdf0e10cSrcweir             for( nG = 0; nG < 256; nG += nColorOffset )
1719cdf0e10cSrcweir             {
1720cdf0e10cSrcweir                 for( nB = 0; nB < 256; nB += nColorOffset )
1721cdf0e10cSrcweir                 {
1722cdf0e10cSrcweir                     pCountTable[ nIndex ].mnIndex = nIndex;
1723cdf0e10cSrcweir                     nIndex++;
1724cdf0e10cSrcweir                 }
1725cdf0e10cSrcweir             }
1726cdf0e10cSrcweir         }
1727cdf0e10cSrcweir 
1728cdf0e10cSrcweir         if( pRAcc->HasPalette() )
1729cdf0e10cSrcweir         {
1730cdf0e10cSrcweir             for( nY = 0L; nY < nHeight; nY++ )
1731cdf0e10cSrcweir             {
1732cdf0e10cSrcweir                 for( nX = 0L; nX < nWidth; nX++ )
1733cdf0e10cSrcweir                 {
1734*87bc88d3SHerbert Dürr                     const BitmapColor& rCol = pRAcc->GetPaletteColor( pRAcc->GetPixelIndex( nY, nX ) );
1735cdf0e10cSrcweir                     pCountTable[ ( ( ( (sal_uInt32) rCol.GetRed() ) >> nRightShiftBits ) << nLeftShiftBits2 ) |
1736cdf0e10cSrcweir                                  ( ( ( (sal_uInt32) rCol.GetGreen() ) >> nRightShiftBits ) << nLeftShiftBits1 ) |
1737cdf0e10cSrcweir                                  ( ( (sal_uInt32) rCol.GetBlue() ) >> nRightShiftBits ) ].mnCount++;
1738cdf0e10cSrcweir                 }
1739cdf0e10cSrcweir             }
1740cdf0e10cSrcweir         }
1741cdf0e10cSrcweir         else
1742cdf0e10cSrcweir         {
1743cdf0e10cSrcweir             for( nY = 0L; nY < nHeight; nY++ )
1744cdf0e10cSrcweir             {
1745cdf0e10cSrcweir                 for( nX = 0L; nX < nWidth; nX++ )
1746cdf0e10cSrcweir                 {
1747cdf0e10cSrcweir                     const BitmapColor aCol( pRAcc->GetPixel( nY, nX ) );
1748cdf0e10cSrcweir                     pCountTable[ ( ( ( (sal_uInt32) aCol.GetRed() ) >> nRightShiftBits ) << nLeftShiftBits2 ) |
1749cdf0e10cSrcweir                                  ( ( ( (sal_uInt32) aCol.GetGreen() ) >> nRightShiftBits ) << nLeftShiftBits1 ) |
1750cdf0e10cSrcweir                                  ( ( (sal_uInt32) aCol.GetBlue() ) >> nRightShiftBits ) ].mnCount++;
1751cdf0e10cSrcweir                 }
1752cdf0e10cSrcweir             }
1753cdf0e10cSrcweir         }
1754cdf0e10cSrcweir 
1755cdf0e10cSrcweir         BitmapPalette aNewPal( nColCount );
1756cdf0e10cSrcweir 
1757cdf0e10cSrcweir         qsort( pCountTable, nTotalColors, sizeof( PopularColorCount ), ImplPopularCmpFnc );
1758cdf0e10cSrcweir 
1759cdf0e10cSrcweir         for( sal_uInt16 n = 0; n < nColCount; n++ )
1760cdf0e10cSrcweir         {
1761cdf0e10cSrcweir             const PopularColorCount& rPop = pCountTable[ n ];
1762cdf0e10cSrcweir             aNewPal[ n ] = BitmapColor( (sal_uInt8) ( ( rPop.mnIndex >> nLeftShiftBits2 ) << nRightShiftBits ),
1763cdf0e10cSrcweir                                         (sal_uInt8) ( ( ( rPop.mnIndex >> nLeftShiftBits1 ) & ( nColorsPerComponent - 1 ) ) << nRightShiftBits ),
1764cdf0e10cSrcweir                                         (sal_uInt8) ( ( rPop.mnIndex & ( nColorsPerComponent - 1 ) ) << nRightShiftBits ) );
1765cdf0e10cSrcweir         }
1766cdf0e10cSrcweir 
1767cdf0e10cSrcweir         Bitmap              aNewBmp( GetSizePixel(), nBitCount, &aNewPal );
1768cdf0e10cSrcweir         BitmapWriteAccess*  pWAcc = aNewBmp.AcquireWriteAccess();
1769cdf0e10cSrcweir 
1770cdf0e10cSrcweir         if( pWAcc )
1771cdf0e10cSrcweir         {
1772cdf0e10cSrcweir             BitmapColor aDstCol( (sal_uInt8) 0 );
1773cdf0e10cSrcweir             sal_uInt8*      pIndexMap = new sal_uInt8[ nTotalColors ];
1774cdf0e10cSrcweir 
1775cdf0e10cSrcweir             for( nR = 0, nIndex = 0; nR < 256; nR += nColorOffset )
1776cdf0e10cSrcweir                 for( nG = 0; nG < 256; nG += nColorOffset )
1777cdf0e10cSrcweir                     for( nB = 0; nB < 256; nB += nColorOffset )
1778cdf0e10cSrcweir                         pIndexMap[ nIndex++ ] = (sal_uInt8) aNewPal.GetBestIndex( BitmapColor( (sal_uInt8) nR, (sal_uInt8) nG, (sal_uInt8) nB ) );
1779cdf0e10cSrcweir 
1780cdf0e10cSrcweir             if( pRAcc->HasPalette() )
1781cdf0e10cSrcweir             {
1782cdf0e10cSrcweir                 for( nY = 0L; nY < nHeight; nY++ )
1783cdf0e10cSrcweir                 {
1784cdf0e10cSrcweir                     for( nX = 0L; nX < nWidth; nX++ )
1785cdf0e10cSrcweir                     {
1786*87bc88d3SHerbert Dürr                         const BitmapColor& rCol = pRAcc->GetPaletteColor( pRAcc->GetPixelIndex( nY, nX ) );
1787cdf0e10cSrcweir                         aDstCol.SetIndex( pIndexMap[ ( ( ( (sal_uInt32) rCol.GetRed() ) >> nRightShiftBits ) << nLeftShiftBits2 ) |
1788cdf0e10cSrcweir                                                      ( ( ( (sal_uInt32) rCol.GetGreen() ) >> nRightShiftBits ) << nLeftShiftBits1 ) |
1789cdf0e10cSrcweir                                                      ( ( (sal_uInt32) rCol.GetBlue() ) >> nRightShiftBits ) ] );
1790cdf0e10cSrcweir                         pWAcc->SetPixel( nY, nX, aDstCol );
1791cdf0e10cSrcweir                     }
1792cdf0e10cSrcweir                 }
1793cdf0e10cSrcweir             }
1794cdf0e10cSrcweir             else
1795cdf0e10cSrcweir             {
1796cdf0e10cSrcweir                 for( nY = 0L; nY < nHeight; nY++ )
1797cdf0e10cSrcweir                 {
1798cdf0e10cSrcweir                     for( nX = 0L; nX < nWidth; nX++ )
1799cdf0e10cSrcweir                     {
1800cdf0e10cSrcweir                         const BitmapColor aCol( pRAcc->GetPixel( nY, nX ) );
1801cdf0e10cSrcweir                         aDstCol.SetIndex( pIndexMap[ ( ( ( (sal_uInt32) aCol.GetRed() ) >> nRightShiftBits ) << nLeftShiftBits2 ) |
1802cdf0e10cSrcweir                                                      ( ( ( (sal_uInt32) aCol.GetGreen() ) >> nRightShiftBits ) << nLeftShiftBits1 ) |
1803cdf0e10cSrcweir                                                      ( ( (sal_uInt32) aCol.GetBlue() ) >> nRightShiftBits ) ] );
1804cdf0e10cSrcweir                         pWAcc->SetPixel( nY, nX, aDstCol );
1805cdf0e10cSrcweir                     }
1806cdf0e10cSrcweir                 }
1807cdf0e10cSrcweir             }
1808cdf0e10cSrcweir 
1809cdf0e10cSrcweir             delete[] pIndexMap;
1810cdf0e10cSrcweir             aNewBmp.ReleaseAccess( pWAcc );
1811cdf0e10cSrcweir             bRet = sal_True;
1812cdf0e10cSrcweir         }
1813cdf0e10cSrcweir 
1814cdf0e10cSrcweir         delete[] pCountTable;
1815cdf0e10cSrcweir         ReleaseAccess( pRAcc );
1816cdf0e10cSrcweir 
1817cdf0e10cSrcweir         if( bRet )
1818cdf0e10cSrcweir         {
1819cdf0e10cSrcweir             const MapMode   aMap( maPrefMapMode );
1820cdf0e10cSrcweir             const Size      aSize( maPrefSize );
1821cdf0e10cSrcweir 
1822cdf0e10cSrcweir             *this = aNewBmp;
1823cdf0e10cSrcweir             maPrefMapMode = aMap;
1824cdf0e10cSrcweir             maPrefSize = aSize;
1825cdf0e10cSrcweir         }
1826cdf0e10cSrcweir     }
1827cdf0e10cSrcweir 
1828cdf0e10cSrcweir     return bRet;
1829cdf0e10cSrcweir }
1830cdf0e10cSrcweir 
1831cdf0e10cSrcweir // ------------------------------------------------------------------------
1832cdf0e10cSrcweir 
1833cdf0e10cSrcweir sal_Bool Bitmap::ImplReduceMedian( sal_uInt16 nColCount )
1834cdf0e10cSrcweir {
1835cdf0e10cSrcweir     BitmapReadAccess*   pRAcc = AcquireReadAccess();
1836cdf0e10cSrcweir     sal_uInt16              nBitCount;
1837cdf0e10cSrcweir     sal_Bool                bRet = sal_False;
1838cdf0e10cSrcweir 
1839cdf0e10cSrcweir     if( nColCount < 17 )
1840cdf0e10cSrcweir         nBitCount = 4;
1841cdf0e10cSrcweir     else if( nColCount < 257 )
1842cdf0e10cSrcweir         nBitCount = 8;
1843cdf0e10cSrcweir     else
1844cdf0e10cSrcweir     {
1845cdf0e10cSrcweir         DBG_ERROR( "Bitmap::ImplReduceMedian(): invalid color count!" );
1846cdf0e10cSrcweir         nBitCount = 8;
1847cdf0e10cSrcweir         nColCount = 256;
1848cdf0e10cSrcweir     }
1849cdf0e10cSrcweir 
1850cdf0e10cSrcweir     if( pRAcc )
1851cdf0e10cSrcweir     {
1852cdf0e10cSrcweir         Bitmap              aNewBmp( GetSizePixel(), nBitCount );
1853cdf0e10cSrcweir         BitmapWriteAccess*  pWAcc = aNewBmp.AcquireWriteAccess();
1854cdf0e10cSrcweir 
1855cdf0e10cSrcweir         if( pWAcc )
1856cdf0e10cSrcweir         {
1857cdf0e10cSrcweir             const sal_uLong nSize = 32768UL * sizeof( sal_uLong );
1858cdf0e10cSrcweir             sal_uLong*      pColBuf = (sal_uLong*) rtl_allocateMemory( nSize );
1859cdf0e10cSrcweir             const long  nWidth = pWAcc->Width();
1860cdf0e10cSrcweir             const long  nHeight = pWAcc->Height();
1861cdf0e10cSrcweir             long        nIndex = 0L;
1862cdf0e10cSrcweir 
1863cdf0e10cSrcweir             memset( (HPBYTE) pColBuf, 0, nSize );
1864cdf0e10cSrcweir 
1865cdf0e10cSrcweir             // create Buffer
1866cdf0e10cSrcweir             if( pRAcc->HasPalette() )
1867cdf0e10cSrcweir             {
1868cdf0e10cSrcweir                 for( long nY = 0L; nY < nHeight; nY++ )
1869cdf0e10cSrcweir                 {
1870cdf0e10cSrcweir                     for( long nX = 0L; nX < nWidth; nX++ )
1871cdf0e10cSrcweir                     {
1872*87bc88d3SHerbert Dürr                         const BitmapColor& rCol = pRAcc->GetPaletteColor( pRAcc->GetPixelIndex( nY, nX ) );
1873cdf0e10cSrcweir                         pColBuf[ RGB15( rCol.GetRed() >> 3, rCol.GetGreen() >> 3, rCol.GetBlue() >> 3 ) ]++;
1874cdf0e10cSrcweir                     }
1875cdf0e10cSrcweir                 }
1876cdf0e10cSrcweir             }
1877cdf0e10cSrcweir             else
1878cdf0e10cSrcweir             {
1879cdf0e10cSrcweir                 for( long nY = 0L; nY < nHeight; nY++ )
1880cdf0e10cSrcweir                 {
1881cdf0e10cSrcweir                     for( long nX = 0L; nX < nWidth; nX++ )
1882cdf0e10cSrcweir                     {
1883cdf0e10cSrcweir                         const BitmapColor aCol( pRAcc->GetPixel( nY, nX ) );
1884cdf0e10cSrcweir                         pColBuf[ RGB15( aCol.GetRed() >> 3, aCol.GetGreen() >> 3, aCol.GetBlue() >> 3 ) ]++;
1885cdf0e10cSrcweir                     }
1886cdf0e10cSrcweir                 }
1887cdf0e10cSrcweir             }
1888cdf0e10cSrcweir 
1889cdf0e10cSrcweir             // create palette via median cut
1890cdf0e10cSrcweir             BitmapPalette aPal( pWAcc->GetPaletteEntryCount() );
1891cdf0e10cSrcweir             ImplMedianCut( pColBuf, aPal, 0, 31, 0, 31, 0, 31,
1892cdf0e10cSrcweir                            nColCount, nWidth * nHeight, nIndex );
1893cdf0e10cSrcweir 
1894cdf0e10cSrcweir             // do mapping of colors to palette
1895cdf0e10cSrcweir             InverseColorMap aMap( aPal );
1896cdf0e10cSrcweir             pWAcc->SetPalette( aPal );
1897cdf0e10cSrcweir             for( long nY = 0L; nY < nHeight; nY++ )
1898cdf0e10cSrcweir                 for( long nX = 0L; nX < nWidth; nX++ )
1899*87bc88d3SHerbert Dürr                     pWAcc->SetPixelIndex( nY, nX, static_cast<sal_uInt8>( aMap.GetBestPaletteIndex( pRAcc->GetColor( nY, nX ) )) );
1900cdf0e10cSrcweir 
1901cdf0e10cSrcweir             rtl_freeMemory( pColBuf );
1902cdf0e10cSrcweir             aNewBmp.ReleaseAccess( pWAcc );
1903cdf0e10cSrcweir             bRet = sal_True;
1904cdf0e10cSrcweir         }
1905cdf0e10cSrcweir 
1906cdf0e10cSrcweir         ReleaseAccess( pRAcc );
1907cdf0e10cSrcweir 
1908cdf0e10cSrcweir         if( bRet )
1909cdf0e10cSrcweir         {
1910cdf0e10cSrcweir             const MapMode   aMap( maPrefMapMode );
1911cdf0e10cSrcweir             const Size      aSize( maPrefSize );
1912cdf0e10cSrcweir 
1913cdf0e10cSrcweir             *this = aNewBmp;
1914cdf0e10cSrcweir             maPrefMapMode = aMap;
1915cdf0e10cSrcweir             maPrefSize = aSize;
1916cdf0e10cSrcweir         }
1917cdf0e10cSrcweir     }
1918cdf0e10cSrcweir 
1919cdf0e10cSrcweir     return bRet;
1920cdf0e10cSrcweir }
1921cdf0e10cSrcweir 
1922cdf0e10cSrcweir // ------------------------------------------------------------------------
1923cdf0e10cSrcweir 
1924cdf0e10cSrcweir void Bitmap::ImplMedianCut( sal_uLong* pColBuf, BitmapPalette& rPal,
1925cdf0e10cSrcweir                             long nR1, long nR2, long nG1, long nG2, long nB1, long nB2,
1926cdf0e10cSrcweir                             long nColors, long nPixels, long& rIndex )
1927cdf0e10cSrcweir {
1928cdf0e10cSrcweir     if( !nPixels )
1929cdf0e10cSrcweir         return;
1930cdf0e10cSrcweir 
1931cdf0e10cSrcweir     BitmapColor aCol;
1932cdf0e10cSrcweir     const long  nRLen = nR2 - nR1;
1933cdf0e10cSrcweir     const long  nGLen = nG2 - nG1;
1934cdf0e10cSrcweir     const long  nBLen = nB2 - nB1;
1935cdf0e10cSrcweir     long        nR, nG, nB;
1936cdf0e10cSrcweir     sal_uLong*      pBuf = pColBuf;
1937cdf0e10cSrcweir 
1938cdf0e10cSrcweir     if( !nRLen && !nGLen && !nBLen )
1939cdf0e10cSrcweir     {
1940cdf0e10cSrcweir         if( pBuf[ RGB15( nR1, nG1, nB1 ) ] )
1941cdf0e10cSrcweir         {
1942cdf0e10cSrcweir             aCol.SetRed( (sal_uInt8) ( nR1 << 3 ) );
1943cdf0e10cSrcweir             aCol.SetGreen( (sal_uInt8) ( nG1 << 3 ) );
1944cdf0e10cSrcweir             aCol.SetBlue( (sal_uInt8) ( nB1 << 3 ) );
1945cdf0e10cSrcweir             rPal[ (sal_uInt16) rIndex++ ] = aCol;
1946cdf0e10cSrcweir         }
1947cdf0e10cSrcweir     }
1948cdf0e10cSrcweir     else
1949cdf0e10cSrcweir     {
1950cdf0e10cSrcweir         if( 1 == nColors || 1 == nPixels )
1951cdf0e10cSrcweir         {
1952cdf0e10cSrcweir             long nPixSum = 0, nRSum = 0, nGSum = 0, nBSum = 0;
1953cdf0e10cSrcweir 
1954cdf0e10cSrcweir             for( nR = nR1; nR <= nR2; nR++ )
1955cdf0e10cSrcweir             {
1956cdf0e10cSrcweir                 for( nG = nG1; nG <= nG2; nG++ )
1957cdf0e10cSrcweir                 {
1958cdf0e10cSrcweir                     for( nB = nB1; nB <= nB2; nB++ )
1959cdf0e10cSrcweir                     {
1960cdf0e10cSrcweir                         nPixSum = pBuf[ RGB15( nR, nG, nB ) ];
1961cdf0e10cSrcweir 
1962cdf0e10cSrcweir                         if( nPixSum )
1963cdf0e10cSrcweir                         {
1964cdf0e10cSrcweir                             nRSum += nR * nPixSum;
1965cdf0e10cSrcweir                             nGSum += nG * nPixSum;
1966cdf0e10cSrcweir                             nBSum += nB * nPixSum;
1967cdf0e10cSrcweir                         }
1968cdf0e10cSrcweir                     }
1969cdf0e10cSrcweir                 }
1970cdf0e10cSrcweir             }
1971cdf0e10cSrcweir 
1972cdf0e10cSrcweir             aCol.SetRed( (sal_uInt8) ( ( nRSum / nPixels ) << 3 ) );
1973cdf0e10cSrcweir             aCol.SetGreen( (sal_uInt8) ( ( nGSum / nPixels ) << 3 ) );
1974cdf0e10cSrcweir             aCol.SetBlue( (sal_uInt8) ( ( nBSum / nPixels ) << 3 ) );
1975cdf0e10cSrcweir             rPal[ (sal_uInt16) rIndex++ ] = aCol;
1976cdf0e10cSrcweir         }
1977cdf0e10cSrcweir         else
1978cdf0e10cSrcweir         {
1979cdf0e10cSrcweir             const long  nTest = ( nPixels >> 1 );
1980cdf0e10cSrcweir             long        nPixOld = 0;
1981cdf0e10cSrcweir             long        nPixNew = 0;
1982cdf0e10cSrcweir 
1983cdf0e10cSrcweir             if( nBLen > nGLen && nBLen > nRLen )
1984cdf0e10cSrcweir             {
1985cdf0e10cSrcweir                 nB = nB1 - 1;
1986cdf0e10cSrcweir 
1987cdf0e10cSrcweir                 while( nPixNew < nTest )
1988cdf0e10cSrcweir                 {
1989cdf0e10cSrcweir                     nB++, nPixOld = nPixNew;
1990cdf0e10cSrcweir                     for( nR = nR1; nR <= nR2; nR++ )
1991cdf0e10cSrcweir                         for( nG = nG1; nG <= nG2; nG++ )
1992cdf0e10cSrcweir                             nPixNew += pBuf[ RGB15( nR, nG, nB ) ];
1993cdf0e10cSrcweir                 }
1994cdf0e10cSrcweir 
1995cdf0e10cSrcweir                 if( nB < nB2 )
1996cdf0e10cSrcweir                 {
1997cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG2, nB1, nB, nColors >> 1, nPixNew, rIndex );
1998cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG2, nB + 1, nB2, nColors >> 1, nPixels - nPixNew, rIndex );
1999cdf0e10cSrcweir                 }
2000cdf0e10cSrcweir                 else
2001cdf0e10cSrcweir                 {
2002cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG2, nB1, nB - 1, nColors >> 1, nPixOld, rIndex );
2003cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG2, nB, nB2, nColors >> 1, nPixels - nPixOld, rIndex );
2004cdf0e10cSrcweir                 }
2005cdf0e10cSrcweir             }
2006cdf0e10cSrcweir             else if( nGLen > nRLen )
2007cdf0e10cSrcweir             {
2008cdf0e10cSrcweir                 nG = nG1 - 1;
2009cdf0e10cSrcweir 
2010cdf0e10cSrcweir                 while( nPixNew < nTest )
2011cdf0e10cSrcweir                 {
2012cdf0e10cSrcweir                     nG++, nPixOld = nPixNew;
2013cdf0e10cSrcweir                     for( nR = nR1; nR <= nR2; nR++ )
2014cdf0e10cSrcweir                         for( nB = nB1; nB <= nB2; nB++ )
2015cdf0e10cSrcweir                             nPixNew += pBuf[ RGB15( nR, nG, nB ) ];
2016cdf0e10cSrcweir                 }
2017cdf0e10cSrcweir 
2018cdf0e10cSrcweir                 if( nG < nG2 )
2019cdf0e10cSrcweir                 {
2020cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG, nB1, nB2, nColors >> 1, nPixNew, rIndex );
2021cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR2, nG + 1, nG2, nB1, nB2, nColors >> 1, nPixels - nPixNew, rIndex );
2022cdf0e10cSrcweir                 }
2023cdf0e10cSrcweir                 else
2024cdf0e10cSrcweir                 {
2025cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR2, nG1, nG - 1, nB1, nB2, nColors >> 1, nPixOld, rIndex );
2026cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR2, nG, nG2, nB1, nB2, nColors >> 1, nPixels - nPixOld, rIndex );
2027cdf0e10cSrcweir                 }
2028cdf0e10cSrcweir             }
2029cdf0e10cSrcweir             else
2030cdf0e10cSrcweir             {
2031cdf0e10cSrcweir                 nR = nR1 - 1;
2032cdf0e10cSrcweir 
2033cdf0e10cSrcweir                 while( nPixNew < nTest )
2034cdf0e10cSrcweir                 {
2035cdf0e10cSrcweir                     nR++, nPixOld = nPixNew;
2036cdf0e10cSrcweir                     for( nG = nG1; nG <= nG2; nG++ )
2037cdf0e10cSrcweir                         for( nB = nB1; nB <= nB2; nB++ )
2038cdf0e10cSrcweir                             nPixNew += pBuf[ RGB15( nR, nG, nB ) ];
2039cdf0e10cSrcweir                 }
2040cdf0e10cSrcweir 
2041cdf0e10cSrcweir                 if( nR < nR2 )
2042cdf0e10cSrcweir                 {
2043cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR, nG1, nG2, nB1, nB2, nColors >> 1, nPixNew, rIndex );
2044cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1 + 1, nR2, nG1, nG2, nB1, nB2, nColors >> 1, nPixels - nPixNew, rIndex );
2045cdf0e10cSrcweir                 }
2046cdf0e10cSrcweir                 else
2047cdf0e10cSrcweir                 {
2048cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR1, nR - 1, nG1, nG2, nB1, nB2, nColors >> 1, nPixOld, rIndex );
2049cdf0e10cSrcweir                     ImplMedianCut( pBuf, rPal, nR, nR2, nG1, nG2, nB1, nB2, nColors >> 1, nPixels - nPixOld, rIndex );
2050cdf0e10cSrcweir                 }
2051cdf0e10cSrcweir             }
2052cdf0e10cSrcweir         }
2053cdf0e10cSrcweir     }
2054cdf0e10cSrcweir }
2055cdf0e10cSrcweir 
2056cdf0e10cSrcweir // ------------------------------------------------------------------------
2057cdf0e10cSrcweir 
2058cdf0e10cSrcweir sal_Bool Bitmap::Vectorize( PolyPolygon& rPolyPoly, sal_uLong nFlags, const Link* pProgress )
2059cdf0e10cSrcweir {
2060cdf0e10cSrcweir     return ImplVectorizer().ImplVectorize( *this, rPolyPoly, nFlags, pProgress );
2061cdf0e10cSrcweir }
2062cdf0e10cSrcweir 
2063cdf0e10cSrcweir // ------------------------------------------------------------------------
2064cdf0e10cSrcweir 
2065cdf0e10cSrcweir sal_Bool Bitmap::Vectorize( GDIMetaFile& rMtf, sal_uInt8 cReduce, sal_uLong nFlags, const Link* pProgress )
2066cdf0e10cSrcweir {
2067cdf0e10cSrcweir     return ImplVectorizer().ImplVectorize( *this, rMtf, cReduce, nFlags, pProgress );
2068cdf0e10cSrcweir }
2069cdf0e10cSrcweir 
2070cdf0e10cSrcweir // ------------------------------------------------------------------------
2071cdf0e10cSrcweir 
2072cdf0e10cSrcweir sal_Bool Bitmap::Adjust( short nLuminancePercent, short nContrastPercent,
2073cdf0e10cSrcweir                      short nChannelRPercent, short nChannelGPercent, short nChannelBPercent,
2074cdf0e10cSrcweir                      double fGamma, sal_Bool bInvert )
2075cdf0e10cSrcweir {
2076cdf0e10cSrcweir     sal_Bool bRet = sal_False;
2077cdf0e10cSrcweir 
2078cdf0e10cSrcweir     // nothing to do => return quickly
2079cdf0e10cSrcweir     if( !nLuminancePercent && !nContrastPercent &&
2080cdf0e10cSrcweir         !nChannelRPercent && !nChannelGPercent && !nChannelBPercent &&
2081cdf0e10cSrcweir         ( fGamma == 1.0 ) && !bInvert )
2082cdf0e10cSrcweir     {
2083cdf0e10cSrcweir         bRet = sal_True;
2084cdf0e10cSrcweir     }
2085cdf0e10cSrcweir     else
2086cdf0e10cSrcweir     {
2087cdf0e10cSrcweir         BitmapWriteAccess* pAcc = AcquireWriteAccess();
2088cdf0e10cSrcweir 
2089cdf0e10cSrcweir         if( pAcc )
2090cdf0e10cSrcweir         {
2091cdf0e10cSrcweir             BitmapColor     aCol;
2092cdf0e10cSrcweir             const long      nW = pAcc->Width();
2093cdf0e10cSrcweir             const long      nH = pAcc->Height();
2094cdf0e10cSrcweir             sal_uInt8*          cMapR = new sal_uInt8[ 256 ];
2095cdf0e10cSrcweir             sal_uInt8*          cMapG = new sal_uInt8[ 256 ];
2096cdf0e10cSrcweir             sal_uInt8*          cMapB = new sal_uInt8[ 256 ];
2097cdf0e10cSrcweir             long            nX, nY;
2098cdf0e10cSrcweir             double          fM, fROff, fGOff, fBOff, fOff;
2099cdf0e10cSrcweir 
2100cdf0e10cSrcweir             // calculate slope
2101cdf0e10cSrcweir             if( nContrastPercent >= 0 )
2102cdf0e10cSrcweir                 fM = 128.0 / ( 128.0 - 1.27 * MinMax( nContrastPercent, 0L, 100L ) );
2103cdf0e10cSrcweir             else
2104cdf0e10cSrcweir                 fM = ( 128.0 + 1.27 * MinMax( nContrastPercent, -100L, 0L ) ) / 128.0;
2105cdf0e10cSrcweir 
2106cdf0e10cSrcweir             // total offset = luminance offset + contrast offset
2107cdf0e10cSrcweir             fOff = MinMax( nLuminancePercent, -100L, 100L ) * 2.55 + 128.0 - fM * 128.0;
2108cdf0e10cSrcweir 
2109cdf0e10cSrcweir             // channel offset = channel offset  + total offset
2110cdf0e10cSrcweir             fROff = nChannelRPercent * 2.55 + fOff;
2111cdf0e10cSrcweir             fGOff = nChannelGPercent * 2.55 + fOff;
2112cdf0e10cSrcweir             fBOff = nChannelBPercent * 2.55 + fOff;
2113cdf0e10cSrcweir 
2114cdf0e10cSrcweir             // calculate gamma value
2115cdf0e10cSrcweir             fGamma = ( fGamma <= 0.0 || fGamma > 10.0 ) ? 1.0 : ( 1.0 / fGamma );
2116cdf0e10cSrcweir             const sal_Bool bGamma = ( fGamma != 1.0 );
2117cdf0e10cSrcweir 
2118cdf0e10cSrcweir             // create mapping table
2119cdf0e10cSrcweir             for( nX = 0L; nX < 256L; nX++ )
2120cdf0e10cSrcweir             {
2121cdf0e10cSrcweir                 cMapR[ nX ] = (sal_uInt8) MinMax( FRound( nX * fM + fROff ), 0L, 255L );
2122cdf0e10cSrcweir                 cMapG[ nX ] = (sal_uInt8) MinMax( FRound( nX * fM + fGOff ), 0L, 255L );
2123cdf0e10cSrcweir                 cMapB[ nX ] = (sal_uInt8) MinMax( FRound( nX * fM + fBOff ), 0L, 255L );
2124cdf0e10cSrcweir 
2125cdf0e10cSrcweir                 if( bGamma )
2126cdf0e10cSrcweir                 {
2127cdf0e10cSrcweir                     cMapR[ nX ] = GAMMA( cMapR[ nX ], fGamma );
2128cdf0e10cSrcweir                     cMapG[ nX ] = GAMMA( cMapG[ nX ], fGamma );
2129cdf0e10cSrcweir                     cMapB[ nX ] = GAMMA( cMapB[ nX ], fGamma );
2130cdf0e10cSrcweir                 }
2131cdf0e10cSrcweir 
2132cdf0e10cSrcweir                 if( bInvert )
2133cdf0e10cSrcweir                 {
2134cdf0e10cSrcweir                     cMapR[ nX ] = ~cMapR[ nX ];
2135cdf0e10cSrcweir                     cMapG[ nX ] = ~cMapG[ nX ];
2136cdf0e10cSrcweir                     cMapB[ nX ] = ~cMapB[ nX ];
2137cdf0e10cSrcweir                 }
2138cdf0e10cSrcweir             }
2139cdf0e10cSrcweir 
2140cdf0e10cSrcweir             // do modifying
2141cdf0e10cSrcweir             if( pAcc->HasPalette() )
2142cdf0e10cSrcweir             {
2143cdf0e10cSrcweir                 BitmapColor aNewCol;
2144cdf0e10cSrcweir 
2145cdf0e10cSrcweir                 for( sal_uInt16 i = 0, nCount = pAcc->GetPaletteEntryCount(); i < nCount; i++ )
2146cdf0e10cSrcweir                 {
2147cdf0e10cSrcweir                     const BitmapColor& rCol = pAcc->GetPaletteColor( i );
2148cdf0e10cSrcweir                     aNewCol.SetRed( cMapR[ rCol.GetRed() ] );
2149cdf0e10cSrcweir                     aNewCol.SetGreen( cMapG[ rCol.GetGreen() ] );
2150cdf0e10cSrcweir                     aNewCol.SetBlue( cMapB[ rCol.GetBlue() ] );
2151cdf0e10cSrcweir                     pAcc->SetPaletteColor( i, aNewCol );
2152cdf0e10cSrcweir                 }
2153cdf0e10cSrcweir             }
2154cdf0e10cSrcweir             else if( pAcc->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_BGR )
2155cdf0e10cSrcweir             {
2156cdf0e10cSrcweir                 for( nY = 0L; nY < nH; nY++ )
2157cdf0e10cSrcweir                 {
2158cdf0e10cSrcweir                     Scanline pScan = pAcc->GetScanline( nY );
2159cdf0e10cSrcweir 
2160cdf0e10cSrcweir                     for( nX = 0L; nX < nW; nX++ )
2161cdf0e10cSrcweir                     {
2162cdf0e10cSrcweir                         *pScan = cMapB[ *pScan ]; pScan++;
2163cdf0e10cSrcweir                         *pScan = cMapG[ *pScan ]; pScan++;
2164cdf0e10cSrcweir                         *pScan = cMapR[ *pScan ]; pScan++;
2165cdf0e10cSrcweir                     }
2166cdf0e10cSrcweir                 }
2167cdf0e10cSrcweir             }
2168cdf0e10cSrcweir             else if( pAcc->GetScanlineFormat() == BMP_FORMAT_24BIT_TC_RGB )
2169cdf0e10cSrcweir             {
2170cdf0e10cSrcweir                 for( nY = 0L; nY < nH; nY++ )
2171cdf0e10cSrcweir                 {
2172cdf0e10cSrcweir                     Scanline pScan = pAcc->GetScanline( nY );
2173cdf0e10cSrcweir 
2174cdf0e10cSrcweir                     for( nX = 0L; nX < nW; nX++ )
2175cdf0e10cSrcweir                     {
2176cdf0e10cSrcweir                         *pScan = cMapR[ *pScan ]; pScan++;
2177cdf0e10cSrcweir                         *pScan = cMapG[ *pScan ]; pScan++;
2178cdf0e10cSrcweir                         *pScan = cMapB[ *pScan ]; pScan++;
2179cdf0e10cSrcweir                     }
2180cdf0e10cSrcweir                 }
2181cdf0e10cSrcweir             }
2182cdf0e10cSrcweir             else
2183cdf0e10cSrcweir             {
2184cdf0e10cSrcweir                 for( nY = 0L; nY < nH; nY++ )
2185cdf0e10cSrcweir                 {
2186cdf0e10cSrcweir                     for( nX = 0L; nX < nW; nX++ )
2187cdf0e10cSrcweir                     {
2188cdf0e10cSrcweir                         aCol = pAcc->GetPixel( nY, nX );
2189cdf0e10cSrcweir                         aCol.SetRed( cMapR[ aCol.GetRed() ] );
2190cdf0e10cSrcweir                         aCol.SetGreen( cMapG[ aCol.GetGreen() ] );
2191cdf0e10cSrcweir                         aCol.SetBlue( cMapB[ aCol.GetBlue() ] );
2192cdf0e10cSrcweir                         pAcc->SetPixel( nY, nX, aCol );
2193cdf0e10cSrcweir                     }
2194cdf0e10cSrcweir                 }
2195cdf0e10cSrcweir             }
2196cdf0e10cSrcweir 
2197cdf0e10cSrcweir             delete[] cMapR;
2198cdf0e10cSrcweir             delete[] cMapG;
2199cdf0e10cSrcweir             delete[] cMapB;
2200cdf0e10cSrcweir             ReleaseAccess( pAcc );
2201cdf0e10cSrcweir             bRet = sal_True;
2202cdf0e10cSrcweir         }
2203cdf0e10cSrcweir     }
2204cdf0e10cSrcweir 
2205cdf0e10cSrcweir     return bRet;
2206cdf0e10cSrcweir }
2207