1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dx_bitmap.cxx,v $
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
52 using namespace ::com::sun::star
;
56 //////////////////////////////////////////////////////////////////////////////////
58 //////////////////////////////////////////////////////////////////////////////////
60 DXBitmap::DXBitmap( const BitmapSharedPtr
& rBitmap
,
62 mpGdiPlusUser( GDIPlusUser::createInstance() ),
63 maSize(rBitmap
->GetWidth(),rBitmap
->GetHeight()),
65 mpGraphics(tools::createGraphicsFromBitmap(mpBitmap
)),
70 DXBitmap::DXBitmap( const ::basegfx::B2IVector
& rSize
,
72 mpGdiPlusUser( GDIPlusUser::createInstance() ),
78 // create container for pixel data
85 PixelFormat32bppARGB
));
93 PixelFormat24bppRGB
));
96 mpGraphics
.reset( tools::createGraphicsFromBitmap(mpBitmap
) );
99 BitmapSharedPtr
DXBitmap::getBitmap() const
104 GraphicsSharedPtr
DXBitmap::getGraphics()
109 ::basegfx::B2IVector
DXBitmap::getSize() const
114 bool DXBitmap::hasAlpha() const
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
144 // failed to lock, bail out
145 return uno::Sequence
< sal_Int8
>();
148 mpBitmap
->UnlockBits( &aBmpData
);
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
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());