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 #ifndef INCLUDED_VCL_BITMAPREADACCESS_HXX 21 #define INCLUDED_VCL_BITMAPREADACCESS_HXX 22 23 #include <vcl/dllapi.h> 24 #include <vcl/bitmap.hxx> 25 #include <vcl/Scanline.hxx> 26 #include <vcl/BitmapBuffer.hxx> 27 #include <vcl/BitmapColor.hxx> 28 #include <vcl/BitmapAccessMode.hxx> 29 #include <vcl/BitmapInfoAccess.hxx> 30 31 class VCL_DLLPUBLIC BitmapReadAccess : public BitmapInfoAccess 32 { 33 friend class BitmapWriteAccess; 34 35 public: 36 BitmapReadAccess(Bitmap& rBitmap, BitmapAccessMode nMode = BitmapAccessMode::Read); 37 virtual ~BitmapReadAccess() override; 38 GetBuffer() const39 Scanline GetBuffer() const 40 { 41 assert(mpBuffer && "Access is not valid!"); 42 43 return mpBuffer ? mpBuffer->mpBits : nullptr; 44 } 45 GetScanline(tools::Long nY) const46 Scanline GetScanline(tools::Long nY) const 47 { 48 assert(mpBuffer && "Access is not valid!"); 49 assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!"); 50 51 if (mpBuffer->mnFormat & ScanlineFormat::TopDown) 52 { 53 return mpBuffer->mpBits + (nY * mpBuffer->mnScanlineSize); 54 } 55 return mpBuffer->mpBits + ((mpBuffer->mnHeight - 1 - nY) * mpBuffer->mnScanlineSize); 56 } 57 GetPixelFromData(const sal_uInt8 * pData,tools::Long nX) const58 BitmapColor GetPixelFromData(const sal_uInt8* pData, tools::Long nX) const 59 { 60 assert(pData && "Access is not valid!"); 61 62 return mFncGetPixel(pData, nX, maColorMask); 63 } 64 GetIndexFromData(const sal_uInt8 * pData,tools::Long nX) const65 sal_uInt8 GetIndexFromData(const sal_uInt8* pData, tools::Long nX) const 66 { 67 return GetPixelFromData(pData, nX).GetIndex(); 68 } 69 SetPixelOnData(sal_uInt8 * pData,tools::Long nX,const BitmapColor & rBitmapColor)70 void SetPixelOnData(sal_uInt8* pData, tools::Long nX, const BitmapColor& rBitmapColor) 71 { 72 assert(pData && "Access is not valid!"); 73 74 mFncSetPixel(pData, nX, rBitmapColor, maColorMask); 75 } 76 GetPixel(tools::Long nY,tools::Long nX) const77 BitmapColor GetPixel(tools::Long nY, tools::Long nX) const 78 { 79 assert(mpBuffer && "Access is not valid!"); 80 assert(nX < mpBuffer->mnWidth && "x-coordinate out of range!"); 81 assert(nY < mpBuffer->mnHeight && "y-coordinate out of range!"); 82 83 return mFncGetPixel(GetScanline(nY), nX, maColorMask); 84 } 85 GetPixel(const Point & point) const86 BitmapColor GetPixel(const Point& point) const { return GetPixel(point.Y(), point.X()); } 87 GetColor(tools::Long nY,tools::Long nX) const88 BitmapColor GetColor(tools::Long nY, tools::Long nX) const 89 { 90 if (HasPalette()) 91 { 92 const BitmapBuffer* pBuffer = mpBuffer; 93 return pBuffer->maPalette[GetPixelIndex(nY, nX)]; 94 } 95 else 96 return GetPixel(nY, nX); 97 } 98 GetColor(const Point & point) const99 BitmapColor GetColor(const Point& point) const { return GetColor(point.Y(), point.X()); } 100 GetPixelIndex(tools::Long nY,tools::Long nX) const101 sal_uInt8 GetPixelIndex(tools::Long nY, tools::Long nX) const 102 { 103 return GetPixel(nY, nX).GetIndex(); 104 } 105 GetPixelIndex(const Point & point) const106 sal_uInt8 GetPixelIndex(const Point& point) const 107 { 108 return GetPixelIndex(point.Y(), point.X()); 109 } 110 111 /** Get the interpolated color at coordinates fY, fX; if outside, return rFallback */ 112 BitmapColor GetInterpolatedColorWithFallback(double fY, double fX, 113 const BitmapColor& rFallback) const; 114 115 /** Get the color at coordinates fY, fX; if outside, return rFallback. Automatically does the correct 116 inside/outside checks, e.g. static_cast< sal_uInt32 >(-0.25) *is* 0, not -1 and has to be outside */ 117 BitmapColor GetColorWithFallback(double fY, double fX, const BitmapColor& rFallback) const; 118 119 private: 120 BitmapReadAccess(const BitmapReadAccess&) = delete; 121 BitmapReadAccess& operator=(const BitmapReadAccess&) = delete; 122 123 protected: 124 FncGetPixel mFncGetPixel; 125 FncSetPixel mFncSetPixel; 126 127 public: ImplGetBitmapBuffer() const128 SAL_DLLPRIVATE BitmapBuffer* ImplGetBitmapBuffer() const { return mpBuffer; } 129 130 static BitmapColor GetPixelForN1BitMsbPal(ConstScanline pScanline, tools::Long nX, 131 const ColorMask& rMask); 132 static BitmapColor GetPixelForN1BitLsbPal(ConstScanline pScanline, tools::Long nX, 133 const ColorMask& rMask); 134 static BitmapColor GetPixelForN8BitPal(ConstScanline pScanline, tools::Long nX, 135 const ColorMask& rMask); 136 static BitmapColor GetPixelForN24BitTcBgr(ConstScanline pScanline, tools::Long nX, 137 const ColorMask& rMask); 138 static BitmapColor GetPixelForN24BitTcRgb(ConstScanline pScanline, tools::Long nX, 139 const ColorMask& rMask); 140 static BitmapColor GetPixelForN32BitTcAbgr(ConstScanline pScanline, tools::Long nX, 141 const ColorMask& rMask); 142 static BitmapColor GetPixelForN32BitTcXbgr(ConstScanline pScanline, tools::Long nX, 143 const ColorMask& rMask); 144 static BitmapColor GetPixelForN32BitTcArgb(ConstScanline pScanline, tools::Long nX, 145 const ColorMask& rMask); 146 static BitmapColor GetPixelForN32BitTcXrgb(ConstScanline pScanline, tools::Long nX, 147 const ColorMask& rMask); 148 static BitmapColor GetPixelForN32BitTcBgra(ConstScanline pScanline, tools::Long nX, 149 const ColorMask& rMask); 150 static BitmapColor GetPixelForN32BitTcBgrx(ConstScanline pScanline, tools::Long nX, 151 const ColorMask& rMask); 152 static BitmapColor GetPixelForN32BitTcRgba(ConstScanline pScanline, tools::Long nX, 153 const ColorMask& rMask); 154 static BitmapColor GetPixelForN32BitTcRgbx(ConstScanline pScanline, tools::Long nX, 155 const ColorMask& rMask); 156 static BitmapColor GetPixelForN32BitTcMask(ConstScanline pScanline, tools::Long nX, 157 const ColorMask& rMask); 158 159 static void SetPixelForN1BitMsbPal(Scanline pScanline, tools::Long nX, 160 const BitmapColor& rBitmapColor, const ColorMask& rMask); 161 static void SetPixelForN1BitLsbPal(Scanline pScanline, tools::Long nX, 162 const BitmapColor& rBitmapColor, const ColorMask& rMask); 163 static void SetPixelForN8BitPal(Scanline pScanline, tools::Long nX, 164 const BitmapColor& rBitmapColor, const ColorMask& rMask); 165 static void SetPixelForN24BitTcBgr(Scanline pScanline, tools::Long nX, 166 const BitmapColor& rBitmapColor, const ColorMask& rMask); 167 static void SetPixelForN24BitTcRgb(Scanline pScanline, tools::Long nX, 168 const BitmapColor& rBitmapColor, const ColorMask& rMask); 169 static void SetPixelForN32BitTcAbgr(Scanline pScanline, tools::Long nX, 170 const BitmapColor& rBitmapColor, const ColorMask& rMask); 171 static void SetPixelForN32BitTcXbgr(Scanline pScanline, tools::Long nX, 172 const BitmapColor& rBitmapColor, const ColorMask& rMask); 173 static void SetPixelForN32BitTcArgb(Scanline pScanline, tools::Long nX, 174 const BitmapColor& rBitmapColor, const ColorMask& rMask); 175 static void SetPixelForN32BitTcXrgb(Scanline pScanline, tools::Long nX, 176 const BitmapColor& rBitmapColor, const ColorMask& rMask); 177 static void SetPixelForN32BitTcBgra(Scanline pScanline, tools::Long nX, 178 const BitmapColor& rBitmapColor, const ColorMask& rMask); 179 static void SetPixelForN32BitTcBgrx(Scanline pScanline, tools::Long nX, 180 const BitmapColor& rBitmapColor, const ColorMask& rMask); 181 static void SetPixelForN32BitTcRgba(Scanline pScanline, tools::Long nX, 182 const BitmapColor& rBitmapColor, const ColorMask& rMask); 183 static void SetPixelForN32BitTcRgbx(Scanline pScanline, tools::Long nX, 184 const BitmapColor& rBitmapColor, const ColorMask& rMask); 185 static void SetPixelForN32BitTcMask(Scanline pScanline, tools::Long nX, 186 const BitmapColor& rBitmapColor, const ColorMask& rMask); 187 188 static FncGetPixel GetPixelFunction(ScanlineFormat nFormat); 189 static FncSetPixel SetPixelFunction(ScanlineFormat nFormat); 190 }; 191 192 #endif // INCLUDED_VCL_BITMAPREADACCESS_HXX 193 194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 195