merge the formfield patch from ooo-build
[ooovba.git] / canvas / source / directx / dx_bitmap.cxx
blob001d8a72c1c969bdf6ba206c76e4d4369c4abcf0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dx_bitmap.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_canvas.hxx"
34 #include "dx_bitmap.hxx"
35 #include "dx_graphicsprovider.hxx"
36 #include "dx_impltools.hxx"
38 #include <canvas/debug.hxx>
39 #include <tools/diagnose_ex.h>
41 #include <basegfx/matrix/b2dhommatrix.hxx>
42 #include <basegfx/range/b2irange.hxx>
44 #if defined(DX_DEBUG_IMAGES)
45 # if OSL_DEBUG_LEVEL > 0
46 # include <imdebug.h>
47 # undef min
48 # undef max
49 # endif
50 #endif
52 using namespace ::com::sun::star;
54 namespace dxcanvas
56 //////////////////////////////////////////////////////////////////////////////////
57 // DXBitmap::DXBitmap
58 //////////////////////////////////////////////////////////////////////////////////
60 DXBitmap::DXBitmap( const BitmapSharedPtr& rBitmap,
61 bool bWithAlpha ) :
62 mpGdiPlusUser( GDIPlusUser::createInstance() ),
63 maSize(rBitmap->GetWidth(),rBitmap->GetHeight()),
64 mpBitmap(rBitmap),
65 mpGraphics(tools::createGraphicsFromBitmap(mpBitmap)),
66 mbAlpha(bWithAlpha)
70 DXBitmap::DXBitmap( const ::basegfx::B2IVector& rSize,
71 bool bWithAlpha ) :
72 mpGdiPlusUser( GDIPlusUser::createInstance() ),
73 maSize(rSize),
74 mpBitmap(),
75 mpGraphics(),
76 mbAlpha(bWithAlpha)
78 // create container for pixel data
79 if(mbAlpha)
81 mpBitmap.reset(
82 new Gdiplus::Bitmap(
83 maSize.getX(),
84 maSize.getY(),
85 PixelFormat32bppARGB));
87 else
89 mpBitmap.reset(
90 new Gdiplus::Bitmap(
91 maSize.getX(),
92 maSize.getY(),
93 PixelFormat24bppRGB));
96 mpGraphics.reset( tools::createGraphicsFromBitmap(mpBitmap) );
99 BitmapSharedPtr DXBitmap::getBitmap() const
101 return mpBitmap;
104 GraphicsSharedPtr DXBitmap::getGraphics()
106 return mpGraphics;
109 ::basegfx::B2IVector DXBitmap::getSize() const
111 return maSize;
114 bool DXBitmap::hasAlpha() const
116 return mbAlpha;
119 uno::Sequence< sal_Int8 > DXBitmap::getData( rendering::IntegerBitmapLayout& /*bitmapLayout*/,
120 const geometry::IntegerRectangle2D& rect )
122 uno::Sequence< sal_Int8 > aRes( (rect.X2-rect.X1)*(rect.Y2-rect.Y1)*4 ); // TODO(F1): Be format-agnostic here
124 const Gdiplus::Rect aRect( tools::gdiPlusRectFromIntegerRectangle2D( rect ) );
126 Gdiplus::BitmapData aBmpData;
127 aBmpData.Width = rect.X2-rect.X1;
128 aBmpData.Height = rect.Y2-rect.Y1;
129 aBmpData.Stride = 4*aBmpData.Width;
130 aBmpData.PixelFormat = PixelFormat32bppARGB;
131 aBmpData.Scan0 = aRes.getArray();
133 // TODO(F1): Support more pixel formats natively
135 // read data from bitmap
136 if( Gdiplus::Ok != mpBitmap->LockBits( &aRect,
137 Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeUserInputBuf,
138 PixelFormat32bppARGB, // TODO(F1): Adapt to
139 // Graphics native
140 // format/change
141 // getMemoryLayout
142 &aBmpData ) )
144 // failed to lock, bail out
145 return uno::Sequence< sal_Int8 >();
148 mpBitmap->UnlockBits( &aBmpData );
150 return aRes;
153 void DXBitmap::setData( const uno::Sequence< sal_Int8 >& data,
154 const rendering::IntegerBitmapLayout& /*bitmapLayout*/,
155 const geometry::IntegerRectangle2D& rect )
157 const Gdiplus::Rect aRect( tools::gdiPlusRectFromIntegerRectangle2D( rect ) );
159 Gdiplus::BitmapData aBmpData;
160 aBmpData.Width = rect.X2-rect.X1;
161 aBmpData.Height = rect.Y2-rect.Y1;
162 aBmpData.Stride = 4*aBmpData.Width;
163 aBmpData.PixelFormat = PixelFormat32bppARGB;
164 aBmpData.Scan0 = (void*)data.getConstArray();
166 // TODO(F1): Support more pixel formats natively
168 if( Gdiplus::Ok != mpBitmap->LockBits( &aRect,
169 Gdiplus::ImageLockModeWrite | Gdiplus::ImageLockModeUserInputBuf,
170 PixelFormat32bppARGB, // TODO: Adapt to
171 // Graphics native
172 // format/change
173 // getMemoryLayout
174 &aBmpData ) )
176 throw uno::RuntimeException();
179 // commit data to bitmap
180 mpBitmap->UnlockBits( &aBmpData );
183 void DXBitmap::setPixel( const uno::Sequence< sal_Int8 >& color,
184 const rendering::IntegerBitmapLayout& /*bitmapLayout*/,
185 const geometry::IntegerPoint2D& pos )
187 const geometry::IntegerSize2D aSize( maSize.getX(),maSize.getY() );
189 ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aSize.Width,
190 "CanvasHelper::setPixel: X coordinate out of bounds" );
191 ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aSize.Height,
192 "CanvasHelper::setPixel: Y coordinate out of bounds" );
193 ENSURE_ARG_OR_THROW( color.getLength() > 3,
194 "CanvasHelper::setPixel: not enough color components" );
196 if( Gdiplus::Ok != mpBitmap->SetPixel( pos.X, pos.Y,
197 Gdiplus::Color( tools::sequenceToArgb( color ))))
199 throw uno::RuntimeException();
203 uno::Sequence< sal_Int8 > DXBitmap::getPixel( rendering::IntegerBitmapLayout& /*bitmapLayout*/,
204 const geometry::IntegerPoint2D& pos )
206 const geometry::IntegerSize2D aSize( maSize.getX(),maSize.getY() );
208 ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aSize.Width,
209 "CanvasHelper::getPixel: X coordinate out of bounds" );
210 ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aSize.Height,
211 "CanvasHelper::getPixel: Y coordinate out of bounds" );
213 Gdiplus::Color aColor;
215 if( Gdiplus::Ok != mpBitmap->GetPixel( pos.X, pos.Y, &aColor ) )
216 return uno::Sequence< sal_Int8 >();
218 return tools::argbToIntSequence(aColor.GetValue());