1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
24 #include <basegfx/matrix/b2dhommatrix.hxx>
25 #include <basegfx/range/b2irange.hxx>
26 #include <comphelper/diagnose_ex.hxx>
28 #include "dx_bitmap.hxx"
29 #include "dx_graphicsprovider.hxx"
30 #include "dx_impltools.hxx"
32 using namespace ::com::sun::star
;
40 DXBitmap::DXBitmap( const BitmapSharedPtr
& rBitmap
,
42 mpGdiPlusUser( GDIPlusUser::createInstance() ),
43 maSize(rBitmap
->GetWidth(),rBitmap
->GetHeight()),
45 mpGraphics(tools::createGraphicsFromBitmap(mpBitmap
)),
50 DXBitmap::DXBitmap( const ::basegfx::B2ISize
& rSize
,
52 mpGdiPlusUser( GDIPlusUser::createInstance() ),
58 // create container for pixel data
61 mpBitmap
= std::make_shared
<Gdiplus::Bitmap
>(
64 PixelFormat32bppARGB
);
68 mpBitmap
= std::make_shared
<Gdiplus::Bitmap
>(
74 mpGraphics
= tools::createGraphicsFromBitmap(mpBitmap
);
77 BitmapSharedPtr
DXBitmap::getBitmap() const
82 GraphicsSharedPtr
DXBitmap::getGraphics()
87 ::basegfx::B2ISize
DXBitmap::getSize() const
92 bool DXBitmap::hasAlpha() const
97 uno::Sequence
< sal_Int8
> DXBitmap::getData( rendering::IntegerBitmapLayout
& /*bitmapLayout*/,
98 const geometry::IntegerRectangle2D
& rect
)
100 uno::Sequence
< sal_Int8
> aRes( (rect
.X2
-rect
.X1
)*(rect
.Y2
-rect
.Y1
)*4 ); // TODO(F1): Be format-agnostic here
102 const Gdiplus::Rect
aRect( tools::gdiPlusRectFromIntegerRectangle2D( rect
) );
104 Gdiplus::BitmapData aBmpData
;
105 aBmpData
.Width
= rect
.X2
-rect
.X1
;
106 aBmpData
.Height
= rect
.Y2
-rect
.Y1
;
107 aBmpData
.Stride
= 4*aBmpData
.Width
;
108 aBmpData
.PixelFormat
= PixelFormat32bppARGB
;
109 aBmpData
.Scan0
= aRes
.getArray();
111 // TODO(F1): Support more pixel formats natively
113 // read data from bitmap
114 if( Gdiplus::Ok
!= mpBitmap
->LockBits( &aRect
,
115 Gdiplus::ImageLockModeRead
| Gdiplus::ImageLockModeUserInputBuf
,
116 PixelFormat32bppARGB
, // TODO(F1): Adapt to
122 // failed to lock, bail out
123 return uno::Sequence
< sal_Int8
>();
126 mpBitmap
->UnlockBits( &aBmpData
);
131 void DXBitmap::setData( const uno::Sequence
< sal_Int8
>& data
,
132 const rendering::IntegerBitmapLayout
& /*bitmapLayout*/,
133 const geometry::IntegerRectangle2D
& rect
)
135 const Gdiplus::Rect
aRect( tools::gdiPlusRectFromIntegerRectangle2D( rect
) );
137 Gdiplus::BitmapData aBmpData
;
138 aBmpData
.Width
= rect
.X2
-rect
.X1
;
139 aBmpData
.Height
= rect
.Y2
-rect
.Y1
;
140 aBmpData
.Stride
= 4*aBmpData
.Width
;
141 aBmpData
.PixelFormat
= PixelFormat32bppARGB
;
142 aBmpData
.Scan0
= const_cast<sal_Int8
*>(data
.getConstArray());
143 // const_cast is safe, "Gdiplus::ImageLockModeWrite
144 // | Gdiplus::ImageLockModeUserInputBuf makes the data go from
145 // BitmapData into Bitmap", says Thorsten
147 // TODO(F1): Support more pixel formats natively
149 if( Gdiplus::Ok
!= mpBitmap
->LockBits( &aRect
,
150 Gdiplus::ImageLockModeWrite
| Gdiplus::ImageLockModeUserInputBuf
,
151 PixelFormat32bppARGB
, // TODO: Adapt to
157 throw uno::RuntimeException("Internal error while writing BitmapData into Bitmap");
160 // commit data to bitmap
161 mpBitmap
->UnlockBits( &aBmpData
);
164 void DXBitmap::setPixel( const uno::Sequence
< sal_Int8
>& color
,
165 const rendering::IntegerBitmapLayout
& /*bitmapLayout*/,
166 const geometry::IntegerPoint2D
& pos
)
168 const geometry::IntegerSize2D
aSize( maSize
.getWidth(),maSize
.getHeight() );
170 ENSURE_ARG_OR_THROW( pos
.X
>= 0 && pos
.X
< aSize
.Width
,
171 "CanvasHelper::setPixel: X coordinate out of bounds" );
172 ENSURE_ARG_OR_THROW( pos
.Y
>= 0 && pos
.Y
< aSize
.Height
,
173 "CanvasHelper::setPixel: Y coordinate out of bounds" );
174 ENSURE_ARG_OR_THROW( color
.getLength() > 3,
175 "CanvasHelper::setPixel: not enough color components" );
177 if( Gdiplus::Ok
!= mpBitmap
->SetPixel( pos
.X
, pos
.Y
,
178 Gdiplus::Color( tools::sequenceToArgb( color
))))
180 throw uno::RuntimeException("SetPixel called with invalid x,y points or color");
184 uno::Sequence
< sal_Int8
> DXBitmap::getPixel( rendering::IntegerBitmapLayout
& /*bitmapLayout*/,
185 const geometry::IntegerPoint2D
& pos
)
187 const geometry::IntegerSize2D
aSize( maSize
.getWidth(),maSize
.getHeight() );
189 ENSURE_ARG_OR_THROW( pos
.X
>= 0 && pos
.X
< aSize
.Width
,
190 "CanvasHelper::getPixel: X coordinate out of bounds" );
191 ENSURE_ARG_OR_THROW( pos
.Y
>= 0 && pos
.Y
< aSize
.Height
,
192 "CanvasHelper::getPixel: Y coordinate out of bounds" );
194 Gdiplus::Color aColor
;
196 if( Gdiplus::Ok
!= mpBitmap
->GetPixel( pos
.X
, pos
.Y
, &aColor
) )
197 return uno::Sequence
< sal_Int8
>();
199 return tools::argbToIntSequence(aColor
.GetValue());
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */