xref: /trunk/main/canvas/source/directx/dx_bitmap.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_canvas.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "dx_bitmap.hxx"
32*cdf0e10cSrcweir #include "dx_graphicsprovider.hxx"
33*cdf0e10cSrcweir #include "dx_impltools.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <canvas/debug.hxx>
36*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
39*cdf0e10cSrcweir #include <basegfx/range/b2irange.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #if defined(DX_DEBUG_IMAGES)
42*cdf0e10cSrcweir # if OSL_DEBUG_LEVEL > 0
43*cdf0e10cSrcweir #  include <imdebug.h>
44*cdf0e10cSrcweir #  undef min
45*cdf0e10cSrcweir #  undef max
46*cdf0e10cSrcweir # endif
47*cdf0e10cSrcweir #endif
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir using namespace ::com::sun::star;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir namespace dxcanvas
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     //////////////////////////////////////////////////////////////////////////////////
54*cdf0e10cSrcweir     // DXBitmap::DXBitmap
55*cdf0e10cSrcweir     //////////////////////////////////////////////////////////////////////////////////
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     DXBitmap::DXBitmap( const BitmapSharedPtr& rBitmap,
58*cdf0e10cSrcweir                         bool                   bWithAlpha ) :
59*cdf0e10cSrcweir         mpGdiPlusUser( GDIPlusUser::createInstance() ),
60*cdf0e10cSrcweir         maSize(rBitmap->GetWidth(),rBitmap->GetHeight()),
61*cdf0e10cSrcweir         mpBitmap(rBitmap),
62*cdf0e10cSrcweir         mpGraphics(tools::createGraphicsFromBitmap(mpBitmap)),
63*cdf0e10cSrcweir         mbAlpha(bWithAlpha)
64*cdf0e10cSrcweir     {
65*cdf0e10cSrcweir     }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     DXBitmap::DXBitmap( const ::basegfx::B2IVector& rSize,
68*cdf0e10cSrcweir                         bool                        bWithAlpha ) :
69*cdf0e10cSrcweir         mpGdiPlusUser( GDIPlusUser::createInstance() ),
70*cdf0e10cSrcweir         maSize(rSize),
71*cdf0e10cSrcweir         mpBitmap(),
72*cdf0e10cSrcweir         mpGraphics(),
73*cdf0e10cSrcweir         mbAlpha(bWithAlpha)
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         // create container for pixel data
76*cdf0e10cSrcweir         if(mbAlpha)
77*cdf0e10cSrcweir         {
78*cdf0e10cSrcweir             mpBitmap.reset(
79*cdf0e10cSrcweir                 new Gdiplus::Bitmap(
80*cdf0e10cSrcweir                     maSize.getX(),
81*cdf0e10cSrcweir                     maSize.getY(),
82*cdf0e10cSrcweir                     PixelFormat32bppARGB));
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir         else
85*cdf0e10cSrcweir         {
86*cdf0e10cSrcweir             mpBitmap.reset(
87*cdf0e10cSrcweir                 new Gdiplus::Bitmap(
88*cdf0e10cSrcweir                     maSize.getX(),
89*cdf0e10cSrcweir                     maSize.getY(),
90*cdf0e10cSrcweir                     PixelFormat24bppRGB));
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir         mpGraphics.reset( tools::createGraphicsFromBitmap(mpBitmap) );
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     BitmapSharedPtr DXBitmap::getBitmap() const
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir         return mpBitmap;
99*cdf0e10cSrcweir     }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     GraphicsSharedPtr DXBitmap::getGraphics()
102*cdf0e10cSrcweir     {
103*cdf0e10cSrcweir         return mpGraphics;
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     ::basegfx::B2IVector DXBitmap::getSize() const
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         return maSize;
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     bool DXBitmap::hasAlpha() const
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir         return mbAlpha;
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     uno::Sequence< sal_Int8 > DXBitmap::getData( rendering::IntegerBitmapLayout&     /*bitmapLayout*/,
117*cdf0e10cSrcweir                                                  const geometry::IntegerRectangle2D& rect )
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir         uno::Sequence< sal_Int8 > aRes( (rect.X2-rect.X1)*(rect.Y2-rect.Y1)*4 ); // TODO(F1): Be format-agnostic here
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         const Gdiplus::Rect aRect( tools::gdiPlusRectFromIntegerRectangle2D( rect ) );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         Gdiplus::BitmapData aBmpData;
124*cdf0e10cSrcweir         aBmpData.Width       = rect.X2-rect.X1;
125*cdf0e10cSrcweir         aBmpData.Height      = rect.Y2-rect.Y1;
126*cdf0e10cSrcweir         aBmpData.Stride      = 4*aBmpData.Width;
127*cdf0e10cSrcweir         aBmpData.PixelFormat = PixelFormat32bppARGB;
128*cdf0e10cSrcweir         aBmpData.Scan0       = aRes.getArray();
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir         // TODO(F1): Support more pixel formats natively
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         // read data from bitmap
133*cdf0e10cSrcweir         if( Gdiplus::Ok != mpBitmap->LockBits( &aRect,
134*cdf0e10cSrcweir                                                       Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeUserInputBuf,
135*cdf0e10cSrcweir                                                       PixelFormat32bppARGB, // TODO(F1): Adapt to
136*cdf0e10cSrcweir                                                       // Graphics native
137*cdf0e10cSrcweir                                                       // format/change
138*cdf0e10cSrcweir                                                       // getMemoryLayout
139*cdf0e10cSrcweir                                                       &aBmpData ) )
140*cdf0e10cSrcweir         {
141*cdf0e10cSrcweir             // failed to lock, bail out
142*cdf0e10cSrcweir             return uno::Sequence< sal_Int8 >();
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         mpBitmap->UnlockBits( &aBmpData );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         return aRes;
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     void DXBitmap::setData( const uno::Sequence< sal_Int8 >&        data,
151*cdf0e10cSrcweir                             const rendering::IntegerBitmapLayout&   /*bitmapLayout*/,
152*cdf0e10cSrcweir                             const geometry::IntegerRectangle2D&     rect )
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         const Gdiplus::Rect aRect( tools::gdiPlusRectFromIntegerRectangle2D( rect ) );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         Gdiplus::BitmapData aBmpData;
157*cdf0e10cSrcweir         aBmpData.Width       = rect.X2-rect.X1;
158*cdf0e10cSrcweir         aBmpData.Height      = rect.Y2-rect.Y1;
159*cdf0e10cSrcweir         aBmpData.Stride      = 4*aBmpData.Width;
160*cdf0e10cSrcweir         aBmpData.PixelFormat = PixelFormat32bppARGB;
161*cdf0e10cSrcweir         aBmpData.Scan0       = (void*)data.getConstArray();
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         // TODO(F1): Support more pixel formats natively
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         if( Gdiplus::Ok != mpBitmap->LockBits( &aRect,
166*cdf0e10cSrcweir                                                       Gdiplus::ImageLockModeWrite | Gdiplus::ImageLockModeUserInputBuf,
167*cdf0e10cSrcweir                                                       PixelFormat32bppARGB, // TODO: Adapt to
168*cdf0e10cSrcweir                                                       // Graphics native
169*cdf0e10cSrcweir                                                       // format/change
170*cdf0e10cSrcweir                                                       // getMemoryLayout
171*cdf0e10cSrcweir                                                       &aBmpData ) )
172*cdf0e10cSrcweir         {
173*cdf0e10cSrcweir             throw uno::RuntimeException();
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         // commit data to bitmap
177*cdf0e10cSrcweir         mpBitmap->UnlockBits( &aBmpData );
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     void DXBitmap::setPixel( const uno::Sequence< sal_Int8 >&       color,
181*cdf0e10cSrcweir                              const rendering::IntegerBitmapLayout&  /*bitmapLayout*/,
182*cdf0e10cSrcweir                              const geometry::IntegerPoint2D&        pos )
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         const geometry::IntegerSize2D aSize( maSize.getX(),maSize.getY() );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aSize.Width,
187*cdf0e10cSrcweir                              "CanvasHelper::setPixel: X coordinate out of bounds" );
188*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aSize.Height,
189*cdf0e10cSrcweir                              "CanvasHelper::setPixel: Y coordinate out of bounds" );
190*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( color.getLength() > 3,
191*cdf0e10cSrcweir                              "CanvasHelper::setPixel: not enough color components" );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir         if( Gdiplus::Ok != mpBitmap->SetPixel( pos.X, pos.Y,
194*cdf0e10cSrcweir                                                       Gdiplus::Color( tools::sequenceToArgb( color ))))
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             throw uno::RuntimeException();
197*cdf0e10cSrcweir         }
198*cdf0e10cSrcweir     }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     uno::Sequence< sal_Int8 > DXBitmap::getPixel( rendering::IntegerBitmapLayout&   /*bitmapLayout*/,
201*cdf0e10cSrcweir                                                   const geometry::IntegerPoint2D&   pos )
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir         const geometry::IntegerSize2D aSize( maSize.getX(),maSize.getY() );
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aSize.Width,
206*cdf0e10cSrcweir                              "CanvasHelper::getPixel: X coordinate out of bounds" );
207*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aSize.Height,
208*cdf0e10cSrcweir                              "CanvasHelper::getPixel: Y coordinate out of bounds" );
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir         Gdiplus::Color aColor;
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir         if( Gdiplus::Ok != mpBitmap->GetPixel( pos.X, pos.Y, &aColor ) )
213*cdf0e10cSrcweir             return uno::Sequence< sal_Int8 >();
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir         return tools::argbToIntSequence(aColor.GetValue());
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220