/src/libreoffice/vcl/source/bitmap/BitmapWriteAccess.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
11 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
12 | | * contributor license agreements. See the NOTICE file distributed |
13 | | * with this work for additional information regarding copyright |
14 | | * ownership. The ASF licenses this file to you under the Apache |
15 | | * License, Version 2.0 (the "License"); you may not use this file |
16 | | * except in compliance with the License. You may obtain a copy of |
17 | | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
18 | | */ |
19 | | |
20 | | #include <sal/config.h> |
21 | | #include <sal/log.hxx> |
22 | | #include <tools/debug.hxx> |
23 | | |
24 | | #include <vcl/BitmapWriteAccess.hxx> |
25 | | #include <bitmap/bmpfast.hxx> |
26 | | |
27 | | #include <algorithm> |
28 | | |
29 | | BitmapWriteAccess::BitmapWriteAccess(Bitmap& rBitmap) |
30 | 697k | : BitmapReadAccess(rBitmap, BitmapAccessMode::Write) |
31 | 697k | { |
32 | 697k | } |
33 | | |
34 | | BitmapWriteAccess::BitmapWriteAccess(AlphaMask& rBitmap) |
35 | 10.4k | : BitmapReadAccess(rBitmap, BitmapAccessMode::Write) |
36 | 10.4k | { |
37 | 10.4k | } |
38 | | |
39 | 707k | BitmapWriteAccess::~BitmapWriteAccess() {} |
40 | | |
41 | | void BitmapWriteAccess::CopyScanline(tools::Long nY, const BitmapReadAccess& rReadAcc) |
42 | 21.3M | { |
43 | 21.3M | assert(nY >= 0 && nY < mpBuffer->mnHeight && "y-coordinate in destination out of range!"); |
44 | 21.3M | SAL_WARN_IF(nY >= rReadAcc.Height(), "vcl", "y-coordinate in source out of range!"); |
45 | 21.3M | SAL_WARN_IF((!HasPalette() || !rReadAcc.HasPalette()) |
46 | 21.3M | && (HasPalette() || rReadAcc.HasPalette()), |
47 | 21.3M | "vcl", "No copying possible between palette bitmap and TC bitmap!"); |
48 | | |
49 | 21.3M | if ((GetScanlineFormat() == rReadAcc.GetScanlineFormat()) |
50 | 21.3M | && (GetScanlineSize() >= rReadAcc.GetScanlineSize())) |
51 | 21.3M | { |
52 | 21.3M | std::copy_n(rReadAcc.GetScanline(nY), rReadAcc.GetScanlineSize(), GetScanline(nY)); |
53 | 21.3M | } |
54 | 0 | else |
55 | 0 | { |
56 | 0 | tools::Long nWidth = std::min(mpBuffer->mnWidth, rReadAcc.Width()); |
57 | 0 | if (!ImplFastCopyScanline(nY, *ImplGetBitmapBuffer(), *rReadAcc.ImplGetBitmapBuffer())) |
58 | 0 | { |
59 | 0 | Scanline pScanline = GetScanline(nY); |
60 | 0 | Scanline pScanlineRead = rReadAcc.GetScanline(nY); |
61 | 0 | for (tools::Long nX = 0; nX < nWidth; nX++) |
62 | 0 | SetPixelOnData(pScanline, nX, rReadAcc.GetPixelFromData(pScanlineRead, nX)); |
63 | 0 | } |
64 | 0 | } |
65 | 21.3M | } |
66 | | |
67 | | void BitmapWriteAccess::CopyScanline(tools::Long nY, ConstScanline aSrcScanline, |
68 | | ScanlineFormat nSrcScanlineFormat, sal_uInt32 nSrcScanlineSize) |
69 | 38.4M | { |
70 | 38.4M | const ScanlineFormat eFormat = nSrcScanlineFormat; |
71 | | |
72 | 38.4M | assert(nY >= 0 && nY < mpBuffer->mnHeight && "y-coordinate in destination out of range!"); |
73 | 38.4M | DBG_ASSERT((HasPalette() && eFormat <= ScanlineFormat::N8BitPal) |
74 | 38.4M | || (!HasPalette() && eFormat > ScanlineFormat::N8BitPal), |
75 | 38.4M | "No copying possible between palette and non palette scanlines!"); |
76 | | |
77 | 38.4M | const sal_uInt32 nCount = std::min(GetScanlineSize(), nSrcScanlineSize); |
78 | | |
79 | 38.4M | if (!nCount) |
80 | 0 | return; |
81 | | |
82 | 38.4M | if (GetScanlineFormat() == eFormat) |
83 | 38.4M | std::copy_n(aSrcScanline, nCount, GetScanline(nY)); |
84 | 0 | else |
85 | 0 | { |
86 | 0 | if (ImplFastCopyScanline(nY, *ImplGetBitmapBuffer(), aSrcScanline, nSrcScanlineFormat, |
87 | 0 | nSrcScanlineSize)) |
88 | 0 | return; |
89 | | |
90 | 0 | FncGetPixel pFncGetPixel; |
91 | 0 | switch (eFormat) |
92 | 0 | { |
93 | 0 | case ScanlineFormat::N8BitPal: |
94 | 0 | pFncGetPixel = GetPixelForN8BitPal; |
95 | 0 | break; |
96 | 0 | case ScanlineFormat::N24BitTcBgr: |
97 | 0 | pFncGetPixel = GetPixelForN24BitTcBgr; |
98 | 0 | break; |
99 | 0 | case ScanlineFormat::N24BitTcRgb: |
100 | 0 | pFncGetPixel = GetPixelForN24BitTcRgb; |
101 | 0 | break; |
102 | 0 | case ScanlineFormat::N32BitTcAbgr: |
103 | 0 | pFncGetPixel = GetPixelForN32BitTcAbgr; |
104 | 0 | break; |
105 | 0 | case ScanlineFormat::N32BitTcXbgr: |
106 | 0 | pFncGetPixel = GetPixelForN32BitTcXbgr; |
107 | 0 | break; |
108 | 0 | case ScanlineFormat::N32BitTcArgb: |
109 | 0 | pFncGetPixel = GetPixelForN32BitTcArgb; |
110 | 0 | break; |
111 | 0 | case ScanlineFormat::N32BitTcXrgb: |
112 | 0 | pFncGetPixel = GetPixelForN32BitTcXrgb; |
113 | 0 | break; |
114 | 0 | case ScanlineFormat::N32BitTcBgra: |
115 | 0 | pFncGetPixel = GetPixelForN32BitTcBgra; |
116 | 0 | break; |
117 | 0 | case ScanlineFormat::N32BitTcBgrx: |
118 | 0 | pFncGetPixel = GetPixelForN32BitTcBgrx; |
119 | 0 | break; |
120 | 0 | case ScanlineFormat::N32BitTcRgba: |
121 | 0 | pFncGetPixel = GetPixelForN32BitTcRgba; |
122 | 0 | break; |
123 | 0 | case ScanlineFormat::N32BitTcRgbx: |
124 | 0 | pFncGetPixel = GetPixelForN32BitTcRgbx; |
125 | 0 | break; |
126 | | |
127 | 0 | default: |
128 | 0 | assert(false); |
129 | 0 | pFncGetPixel = nullptr; |
130 | 0 | break; |
131 | 0 | } |
132 | | |
133 | 0 | if (pFncGetPixel) |
134 | 0 | { |
135 | 0 | Scanline pScanline = GetScanline(nY); |
136 | 0 | for (tools::Long nX = 0, nWidth = mpBuffer->mnWidth; nX < nWidth; ++nX) |
137 | 0 | SetPixelOnData(pScanline, nX, pFncGetPixel(aSrcScanline, nX)); |
138 | 0 | } |
139 | 0 | } |
140 | 38.4M | } |
141 | | |
142 | | void BitmapWriteAccess::SetLineColor(const Color& rColor) |
143 | 0 | { |
144 | 0 | if (rColor.GetAlpha() == 0) |
145 | 0 | { |
146 | 0 | mpLineColor.reset(); |
147 | 0 | } |
148 | 0 | else |
149 | 0 | { |
150 | 0 | if (HasPalette()) |
151 | 0 | { |
152 | 0 | mpLineColor = BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rColor))); |
153 | 0 | } |
154 | 0 | else |
155 | 0 | { |
156 | 0 | mpLineColor = BitmapColor(rColor); |
157 | 0 | } |
158 | 0 | } |
159 | 0 | } |
160 | | |
161 | 0 | void BitmapWriteAccess::SetFillColor() { mpFillColor.reset(); } |
162 | | |
163 | | void BitmapWriteAccess::SetFillColor(const Color& rColor) |
164 | 0 | { |
165 | 0 | if (rColor.GetAlpha() == 0) |
166 | 0 | { |
167 | 0 | mpFillColor.reset(); |
168 | 0 | } |
169 | 0 | else |
170 | 0 | { |
171 | 0 | if (HasPalette()) |
172 | 0 | { |
173 | 0 | mpFillColor = BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rColor))); |
174 | 0 | } |
175 | 0 | else |
176 | 0 | { |
177 | 0 | mpFillColor = BitmapColor(rColor); |
178 | 0 | } |
179 | 0 | } |
180 | 0 | } |
181 | | |
182 | | void BitmapWriteAccess::Erase(const Color& rColor) |
183 | 84.8k | { |
184 | | // convert the color format from RGB to palette index if needed |
185 | | // TODO: provide and use Erase( BitmapColor& method) |
186 | 84.8k | BitmapColor aColor = rColor; |
187 | 84.8k | if (HasPalette()) |
188 | 80.8k | { |
189 | 80.8k | aColor = BitmapColor(static_cast<sal_uInt8>(GetBestPaletteIndex(rColor))); |
190 | 80.8k | } |
191 | | |
192 | | // try fast bitmap method first |
193 | 84.8k | if (ImplFastEraseBitmap(*mpBuffer, aColor)) |
194 | 83.3k | return; |
195 | | |
196 | 1.43k | tools::Rectangle aRect(Point(), maBitmap.GetSizePixel()); |
197 | 1.43k | if (aRect.IsEmpty()) |
198 | 0 | return; |
199 | | // clear the bitmap by filling the first line pixel by pixel, |
200 | | // then dup the first line over each other line |
201 | 1.43k | Scanline pFirstScanline = GetScanline(0); |
202 | 1.43k | const tools::Long nEndX = aRect.Right(); |
203 | 36.5k | for (tools::Long nX = 0; nX <= nEndX; ++nX) |
204 | 35.1k | SetPixelOnData(pFirstScanline, nX, rColor); |
205 | 1.43k | const auto nScanlineSize = GetScanlineSize(); |
206 | 1.43k | const tools::Long nEndY = aRect.Bottom(); |
207 | 20.2k | for (tools::Long nY = 1; nY <= nEndY; nY++) |
208 | 18.7k | { |
209 | 18.7k | Scanline pDestScanline = GetScanline(nY); |
210 | 18.7k | std::copy_n(pFirstScanline, nScanlineSize, pDestScanline); |
211 | 18.7k | } |
212 | 1.43k | } |
213 | | |
214 | | void BitmapWriteAccess::DrawLine(const Point& rStart, const Point& rEnd) |
215 | 0 | { |
216 | 0 | if (!mpLineColor) |
217 | 0 | return; |
218 | | |
219 | 0 | const BitmapColor& rLineColor = *mpLineColor; |
220 | 0 | tools::Long nX, nY; |
221 | |
|
222 | 0 | if (rStart.X() == rEnd.X()) |
223 | 0 | { |
224 | | // Vertical Line |
225 | 0 | const tools::Long nEndY = rEnd.Y(); |
226 | |
|
227 | 0 | nX = rStart.X(); |
228 | 0 | nY = rStart.Y(); |
229 | |
|
230 | 0 | if (nEndY > nY) |
231 | 0 | { |
232 | 0 | for (; nY <= nEndY; nY++) |
233 | 0 | SetPixel(nY, nX, rLineColor); |
234 | 0 | } |
235 | 0 | else |
236 | 0 | { |
237 | 0 | for (; nY >= nEndY; nY--) |
238 | 0 | SetPixel(nY, nX, rLineColor); |
239 | 0 | } |
240 | 0 | } |
241 | 0 | else if (rStart.Y() == rEnd.Y()) |
242 | 0 | { |
243 | | // Horizontal Line |
244 | 0 | const tools::Long nEndX = rEnd.X(); |
245 | |
|
246 | 0 | nX = rStart.X(); |
247 | 0 | nY = rStart.Y(); |
248 | |
|
249 | 0 | if (nEndX > nX) |
250 | 0 | { |
251 | 0 | for (; nX <= nEndX; nX++) |
252 | 0 | SetPixel(nY, nX, rLineColor); |
253 | 0 | } |
254 | 0 | else |
255 | 0 | { |
256 | 0 | for (; nX >= nEndX; nX--) |
257 | 0 | SetPixel(nY, nX, rLineColor); |
258 | 0 | } |
259 | 0 | } |
260 | 0 | else |
261 | 0 | { |
262 | 0 | const tools::Long nDX = std::abs(rEnd.X() - rStart.X()); |
263 | 0 | const tools::Long nDY = std::abs(rEnd.Y() - rStart.Y()); |
264 | 0 | tools::Long nX1; |
265 | 0 | tools::Long nY1; |
266 | 0 | tools::Long nX2; |
267 | 0 | tools::Long nY2; |
268 | |
|
269 | 0 | if (nDX >= nDY) |
270 | 0 | { |
271 | 0 | if (rStart.X() < rEnd.X()) |
272 | 0 | { |
273 | 0 | nX1 = rStart.X(); |
274 | 0 | nY1 = rStart.Y(); |
275 | 0 | nX2 = rEnd.X(); |
276 | 0 | nY2 = rEnd.Y(); |
277 | 0 | } |
278 | 0 | else |
279 | 0 | { |
280 | 0 | nX1 = rEnd.X(); |
281 | 0 | nY1 = rEnd.Y(); |
282 | 0 | nX2 = rStart.X(); |
283 | 0 | nY2 = rStart.Y(); |
284 | 0 | } |
285 | |
|
286 | 0 | const tools::Long nDYX = (nDY - nDX) << 1; |
287 | 0 | const tools::Long nDY2 = nDY << 1; |
288 | 0 | tools::Long nD = nDY2 - nDX; |
289 | 0 | bool bPos = nY1 < nY2; |
290 | |
|
291 | 0 | for (nX = nX1, nY = nY1; nX <= nX2; nX++) |
292 | 0 | { |
293 | 0 | SetPixel(nY, nX, rLineColor); |
294 | |
|
295 | 0 | if (nD < 0) |
296 | 0 | nD += nDY2; |
297 | 0 | else |
298 | 0 | { |
299 | 0 | nD += nDYX; |
300 | |
|
301 | 0 | if (bPos) |
302 | 0 | nY++; |
303 | 0 | else |
304 | 0 | nY--; |
305 | 0 | } |
306 | 0 | } |
307 | 0 | } |
308 | 0 | else |
309 | 0 | { |
310 | 0 | if (rStart.Y() < rEnd.Y()) |
311 | 0 | { |
312 | 0 | nX1 = rStart.X(); |
313 | 0 | nY1 = rStart.Y(); |
314 | 0 | nX2 = rEnd.X(); |
315 | 0 | nY2 = rEnd.Y(); |
316 | 0 | } |
317 | 0 | else |
318 | 0 | { |
319 | 0 | nX1 = rEnd.X(); |
320 | 0 | nY1 = rEnd.Y(); |
321 | 0 | nX2 = rStart.X(); |
322 | 0 | nY2 = rStart.Y(); |
323 | 0 | } |
324 | |
|
325 | 0 | const tools::Long nDYX = (nDX - nDY) << 1; |
326 | 0 | const tools::Long nDY2 = nDX << 1; |
327 | 0 | tools::Long nD = nDY2 - nDY; |
328 | 0 | bool bPos = nX1 < nX2; |
329 | |
|
330 | 0 | for (nX = nX1, nY = nY1; nY <= nY2; nY++) |
331 | 0 | { |
332 | 0 | SetPixel(nY, nX, rLineColor); |
333 | |
|
334 | 0 | if (nD < 0) |
335 | 0 | nD += nDY2; |
336 | 0 | else |
337 | 0 | { |
338 | 0 | nD += nDYX; |
339 | |
|
340 | 0 | if (bPos) |
341 | 0 | nX++; |
342 | 0 | else |
343 | 0 | nX--; |
344 | 0 | } |
345 | 0 | } |
346 | 0 | } |
347 | 0 | } |
348 | 0 | } |
349 | | |
350 | | void BitmapWriteAccess::FillRect(const tools::Rectangle& rRect) |
351 | 0 | { |
352 | 0 | if (!mpFillColor) |
353 | 0 | return; |
354 | | |
355 | 0 | const BitmapColor& rFillColor = *mpFillColor; |
356 | 0 | tools::Rectangle aRect(Point(), maBitmap.GetSizePixel()); |
357 | |
|
358 | 0 | aRect.Intersection(rRect); |
359 | |
|
360 | 0 | if (aRect.IsEmpty()) |
361 | 0 | return; |
362 | | |
363 | 0 | const tools::Long nStartX = rRect.Left(); |
364 | 0 | const tools::Long nStartY = rRect.Top(); |
365 | 0 | const tools::Long nEndX = rRect.Right(); |
366 | 0 | const tools::Long nEndY = rRect.Bottom(); |
367 | |
|
368 | 0 | for (tools::Long nY = nStartY; nY <= nEndY; nY++) |
369 | 0 | { |
370 | 0 | Scanline pScanline = GetScanline(nY); |
371 | 0 | for (tools::Long nX = nStartX; nX <= nEndX; nX++) |
372 | 0 | { |
373 | 0 | SetPixelOnData(pScanline, nX, rFillColor); |
374 | 0 | } |
375 | 0 | } |
376 | 0 | } |
377 | | |
378 | | void BitmapWriteAccess::DrawRect(const tools::Rectangle& rRect) |
379 | 0 | { |
380 | 0 | if (mpFillColor) |
381 | 0 | FillRect(rRect); |
382 | |
|
383 | 0 | if (mpLineColor && (!mpFillColor || (*mpFillColor != *mpLineColor))) |
384 | 0 | { |
385 | 0 | DrawLine(rRect.TopLeft(), rRect.TopRight()); |
386 | 0 | DrawLine(rRect.TopRight(), rRect.BottomRight()); |
387 | 0 | DrawLine(rRect.BottomRight(), rRect.BottomLeft()); |
388 | 0 | DrawLine(rRect.BottomLeft(), rRect.TopLeft()); |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |