1*25ea7f45SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*25ea7f45SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*25ea7f45SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*25ea7f45SAndrew Rist * distributed with this work for additional information 6*25ea7f45SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*25ea7f45SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*25ea7f45SAndrew Rist * "License"); you may not use this file except in compliance 9*25ea7f45SAndrew Rist * with the License. You may obtain a copy of the License at 10*25ea7f45SAndrew Rist * 11*25ea7f45SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*25ea7f45SAndrew Rist * 13*25ea7f45SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*25ea7f45SAndrew Rist * software distributed under the License is distributed on an 15*25ea7f45SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*25ea7f45SAndrew Rist * KIND, either express or implied. See the License for the 17*25ea7f45SAndrew Rist * specific language governing permissions and limitations 18*25ea7f45SAndrew Rist * under the License. 19*25ea7f45SAndrew Rist * 20*25ea7f45SAndrew Rist *************************************************************/ 21*25ea7f45SAndrew Rist 22*25ea7f45SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_canvas.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "dx_bitmap.hxx" 28cdf0e10cSrcweir #include "dx_graphicsprovider.hxx" 29cdf0e10cSrcweir #include "dx_impltools.hxx" 30cdf0e10cSrcweir 31cdf0e10cSrcweir #include <canvas/debug.hxx> 32cdf0e10cSrcweir #include <tools/diagnose_ex.h> 33cdf0e10cSrcweir 34cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx> 35cdf0e10cSrcweir #include <basegfx/range/b2irange.hxx> 36cdf0e10cSrcweir 37cdf0e10cSrcweir #if defined(DX_DEBUG_IMAGES) 38cdf0e10cSrcweir # if OSL_DEBUG_LEVEL > 0 39cdf0e10cSrcweir # include <imdebug.h> 40cdf0e10cSrcweir # undef min 41cdf0e10cSrcweir # undef max 42cdf0e10cSrcweir # endif 43cdf0e10cSrcweir #endif 44cdf0e10cSrcweir 45cdf0e10cSrcweir using namespace ::com::sun::star; 46cdf0e10cSrcweir 47cdf0e10cSrcweir namespace dxcanvas 48cdf0e10cSrcweir { 49cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////////// 50cdf0e10cSrcweir // DXBitmap::DXBitmap 51cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////////// 52cdf0e10cSrcweir 53cdf0e10cSrcweir DXBitmap::DXBitmap( const BitmapSharedPtr& rBitmap, 54cdf0e10cSrcweir bool bWithAlpha ) : 55cdf0e10cSrcweir mpGdiPlusUser( GDIPlusUser::createInstance() ), 56cdf0e10cSrcweir maSize(rBitmap->GetWidth(),rBitmap->GetHeight()), 57cdf0e10cSrcweir mpBitmap(rBitmap), 58cdf0e10cSrcweir mpGraphics(tools::createGraphicsFromBitmap(mpBitmap)), 59cdf0e10cSrcweir mbAlpha(bWithAlpha) 60cdf0e10cSrcweir { 61cdf0e10cSrcweir } 62cdf0e10cSrcweir 63cdf0e10cSrcweir DXBitmap::DXBitmap( const ::basegfx::B2IVector& rSize, 64cdf0e10cSrcweir bool bWithAlpha ) : 65cdf0e10cSrcweir mpGdiPlusUser( GDIPlusUser::createInstance() ), 66cdf0e10cSrcweir maSize(rSize), 67cdf0e10cSrcweir mpBitmap(), 68cdf0e10cSrcweir mpGraphics(), 69cdf0e10cSrcweir mbAlpha(bWithAlpha) 70cdf0e10cSrcweir { 71cdf0e10cSrcweir // create container for pixel data 72cdf0e10cSrcweir if(mbAlpha) 73cdf0e10cSrcweir { 74cdf0e10cSrcweir mpBitmap.reset( 75cdf0e10cSrcweir new Gdiplus::Bitmap( 76cdf0e10cSrcweir maSize.getX(), 77cdf0e10cSrcweir maSize.getY(), 78cdf0e10cSrcweir PixelFormat32bppARGB)); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir else 81cdf0e10cSrcweir { 82cdf0e10cSrcweir mpBitmap.reset( 83cdf0e10cSrcweir new Gdiplus::Bitmap( 84cdf0e10cSrcweir maSize.getX(), 85cdf0e10cSrcweir maSize.getY(), 86cdf0e10cSrcweir PixelFormat24bppRGB)); 87cdf0e10cSrcweir } 88cdf0e10cSrcweir 89cdf0e10cSrcweir mpGraphics.reset( tools::createGraphicsFromBitmap(mpBitmap) ); 90cdf0e10cSrcweir } 91cdf0e10cSrcweir 92cdf0e10cSrcweir BitmapSharedPtr DXBitmap::getBitmap() const 93cdf0e10cSrcweir { 94cdf0e10cSrcweir return mpBitmap; 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir GraphicsSharedPtr DXBitmap::getGraphics() 98cdf0e10cSrcweir { 99cdf0e10cSrcweir return mpGraphics; 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir ::basegfx::B2IVector DXBitmap::getSize() const 103cdf0e10cSrcweir { 104cdf0e10cSrcweir return maSize; 105cdf0e10cSrcweir } 106cdf0e10cSrcweir 107cdf0e10cSrcweir bool DXBitmap::hasAlpha() const 108cdf0e10cSrcweir { 109cdf0e10cSrcweir return mbAlpha; 110cdf0e10cSrcweir } 111cdf0e10cSrcweir 112cdf0e10cSrcweir uno::Sequence< sal_Int8 > DXBitmap::getData( rendering::IntegerBitmapLayout& /*bitmapLayout*/, 113cdf0e10cSrcweir const geometry::IntegerRectangle2D& rect ) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir uno::Sequence< sal_Int8 > aRes( (rect.X2-rect.X1)*(rect.Y2-rect.Y1)*4 ); // TODO(F1): Be format-agnostic here 116cdf0e10cSrcweir 117cdf0e10cSrcweir const Gdiplus::Rect aRect( tools::gdiPlusRectFromIntegerRectangle2D( rect ) ); 118cdf0e10cSrcweir 119cdf0e10cSrcweir Gdiplus::BitmapData aBmpData; 120cdf0e10cSrcweir aBmpData.Width = rect.X2-rect.X1; 121cdf0e10cSrcweir aBmpData.Height = rect.Y2-rect.Y1; 122cdf0e10cSrcweir aBmpData.Stride = 4*aBmpData.Width; 123cdf0e10cSrcweir aBmpData.PixelFormat = PixelFormat32bppARGB; 124cdf0e10cSrcweir aBmpData.Scan0 = aRes.getArray(); 125cdf0e10cSrcweir 126cdf0e10cSrcweir // TODO(F1): Support more pixel formats natively 127cdf0e10cSrcweir 128cdf0e10cSrcweir // read data from bitmap 129cdf0e10cSrcweir if( Gdiplus::Ok != mpBitmap->LockBits( &aRect, 130cdf0e10cSrcweir Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeUserInputBuf, 131cdf0e10cSrcweir PixelFormat32bppARGB, // TODO(F1): Adapt to 132cdf0e10cSrcweir // Graphics native 133cdf0e10cSrcweir // format/change 134cdf0e10cSrcweir // getMemoryLayout 135cdf0e10cSrcweir &aBmpData ) ) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir // failed to lock, bail out 138cdf0e10cSrcweir return uno::Sequence< sal_Int8 >(); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir 141cdf0e10cSrcweir mpBitmap->UnlockBits( &aBmpData ); 142cdf0e10cSrcweir 143cdf0e10cSrcweir return aRes; 144cdf0e10cSrcweir } 145cdf0e10cSrcweir 146cdf0e10cSrcweir void DXBitmap::setData( const uno::Sequence< sal_Int8 >& data, 147cdf0e10cSrcweir const rendering::IntegerBitmapLayout& /*bitmapLayout*/, 148cdf0e10cSrcweir const geometry::IntegerRectangle2D& rect ) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir const Gdiplus::Rect aRect( tools::gdiPlusRectFromIntegerRectangle2D( rect ) ); 151cdf0e10cSrcweir 152cdf0e10cSrcweir Gdiplus::BitmapData aBmpData; 153cdf0e10cSrcweir aBmpData.Width = rect.X2-rect.X1; 154cdf0e10cSrcweir aBmpData.Height = rect.Y2-rect.Y1; 155cdf0e10cSrcweir aBmpData.Stride = 4*aBmpData.Width; 156cdf0e10cSrcweir aBmpData.PixelFormat = PixelFormat32bppARGB; 157cdf0e10cSrcweir aBmpData.Scan0 = (void*)data.getConstArray(); 158cdf0e10cSrcweir 159cdf0e10cSrcweir // TODO(F1): Support more pixel formats natively 160cdf0e10cSrcweir 161cdf0e10cSrcweir if( Gdiplus::Ok != mpBitmap->LockBits( &aRect, 162cdf0e10cSrcweir Gdiplus::ImageLockModeWrite | Gdiplus::ImageLockModeUserInputBuf, 163cdf0e10cSrcweir PixelFormat32bppARGB, // TODO: Adapt to 164cdf0e10cSrcweir // Graphics native 165cdf0e10cSrcweir // format/change 166cdf0e10cSrcweir // getMemoryLayout 167cdf0e10cSrcweir &aBmpData ) ) 168cdf0e10cSrcweir { 169cdf0e10cSrcweir throw uno::RuntimeException(); 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir // commit data to bitmap 173cdf0e10cSrcweir mpBitmap->UnlockBits( &aBmpData ); 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir void DXBitmap::setPixel( const uno::Sequence< sal_Int8 >& color, 177cdf0e10cSrcweir const rendering::IntegerBitmapLayout& /*bitmapLayout*/, 178cdf0e10cSrcweir const geometry::IntegerPoint2D& pos ) 179cdf0e10cSrcweir { 180cdf0e10cSrcweir const geometry::IntegerSize2D aSize( maSize.getX(),maSize.getY() ); 181cdf0e10cSrcweir 182cdf0e10cSrcweir ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aSize.Width, 183cdf0e10cSrcweir "CanvasHelper::setPixel: X coordinate out of bounds" ); 184cdf0e10cSrcweir ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aSize.Height, 185cdf0e10cSrcweir "CanvasHelper::setPixel: Y coordinate out of bounds" ); 186cdf0e10cSrcweir ENSURE_ARG_OR_THROW( color.getLength() > 3, 187cdf0e10cSrcweir "CanvasHelper::setPixel: not enough color components" ); 188cdf0e10cSrcweir 189cdf0e10cSrcweir if( Gdiplus::Ok != mpBitmap->SetPixel( pos.X, pos.Y, 190cdf0e10cSrcweir Gdiplus::Color( tools::sequenceToArgb( color )))) 191cdf0e10cSrcweir { 192cdf0e10cSrcweir throw uno::RuntimeException(); 193cdf0e10cSrcweir } 194cdf0e10cSrcweir } 195cdf0e10cSrcweir 196cdf0e10cSrcweir uno::Sequence< sal_Int8 > DXBitmap::getPixel( rendering::IntegerBitmapLayout& /*bitmapLayout*/, 197cdf0e10cSrcweir const geometry::IntegerPoint2D& pos ) 198cdf0e10cSrcweir { 199cdf0e10cSrcweir const geometry::IntegerSize2D aSize( maSize.getX(),maSize.getY() ); 200cdf0e10cSrcweir 201cdf0e10cSrcweir ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aSize.Width, 202cdf0e10cSrcweir "CanvasHelper::getPixel: X coordinate out of bounds" ); 203cdf0e10cSrcweir ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aSize.Height, 204cdf0e10cSrcweir "CanvasHelper::getPixel: Y coordinate out of bounds" ); 205cdf0e10cSrcweir 206cdf0e10cSrcweir Gdiplus::Color aColor; 207cdf0e10cSrcweir 208cdf0e10cSrcweir if( Gdiplus::Ok != mpBitmap->GetPixel( pos.X, pos.Y, &aColor ) ) 209cdf0e10cSrcweir return uno::Sequence< sal_Int8 >(); 210cdf0e10cSrcweir 211cdf0e10cSrcweir return tools::argbToIntSequence(aColor.GetValue()); 212cdf0e10cSrcweir } 213cdf0e10cSrcweir 214cdf0e10cSrcweir } 215cdf0e10cSrcweir 216