update dev300-m58
[ooovba.git] / canvas / source / java / CanvasBitmap.java
blob00b686f2711a0fd7868fcc066eeba0c6e12640ad
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: CanvasBitmap.java,v $
10 * $Revision: 1.8 $
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 // Canvas
32 import com.sun.star.rendering.*;
33 import com.sun.star.geometry.*;
35 public class CanvasBitmap
36 extends CanvasBase
37 implements com.sun.star.lang.XServiceInfo,
38 com.sun.star.rendering.XBitmapCanvas,
39 com.sun.star.rendering.XIntegerBitmap
41 private java.awt.image.BufferedImage bitmap;
42 private java.awt.Graphics2D graphics;
44 public CanvasBitmap( java.awt.image.BufferedImage _bitmap )
46 bitmap = _bitmap;
47 graphics = bitmap.createGraphics();
50 public CanvasBitmap( IntegerSize2D mySize )
52 bitmap = new java.awt.image.BufferedImage(mySize.Width, mySize.Height,
53 java.awt.image.BufferedImage.TYPE_4BYTE_ABGR);
54 graphics = bitmap.createGraphics();
57 public CanvasBitmap( RealSize2D newSize, boolean beFast, CanvasBitmap source )
59 // java.awt.geom.AffineTransform transform = new java.awt.geom.AffineTransform();
60 // transform.scale( newSize.width/size.Width, newSize.height/size.Height );
62 // // TODO: Maybe keep the image returned via
63 // // bitmap.getScaledInstance, and do scaling lazy.
64 // bitmap = new java.awt.image.BufferedImage((int)(newSize.width+.5),
65 // (int)(newSize.height+.5),
66 // java.awt.image.BufferedImage.TYPE_4BYTE_ABGR);
68 // java.awt.image.AffineTransformOp transformer =
69 // new java.awt.image.AffineTransformOp( transform,
70 // java.awt.image.AffineTransformOp.TYPE_BILINEAR);
72 // transformer.filter(source.getBufferedImage(), bitmap);
75 public synchronized java.awt.image.BufferedImage getBufferedImage()
77 return bitmap;
80 public java.awt.Graphics2D getGraphics()
82 return graphics;
86 // XBitmap implementation
87 // ======================
90 public synchronized IntegerSize2D getSize()
92 return new IntegerSize2D( bitmap.getWidth(),
93 bitmap.getHeight() );
96 //----------------------------------------------------------------------------------
98 public synchronized XBitmapCanvas queryBitmapCanvas()
100 return this;
103 //----------------------------------------------------------------------------------
105 public synchronized com.sun.star.rendering.XBitmap getScaledBitmap( RealSize2D newSize, boolean beFast ) throws com.sun.star.lang.IllegalArgumentException, VolatileContentDestroyedException
107 return new CanvasBitmap( newSize, beFast, this );
110 //----------------------------------------------------------------------------------
112 public synchronized boolean hasAlpha()
114 // TODO
115 return false;
118 //----------------------------------------------------------------------------------
121 // XBitmapCanvas impl
122 // ==================
125 public synchronized void copyRect( com.sun.star.rendering.XBitmapCanvas sourceCanvas,
126 com.sun.star.geometry.RealRectangle2D sourceRect,
127 com.sun.star.rendering.ViewState sourceViewState,
128 com.sun.star.rendering.RenderState sourceRenderState,
129 com.sun.star.geometry.RealRectangle2D destRect,
130 com.sun.star.rendering.ViewState destViewState,
131 com.sun.star.rendering.RenderState destRenderState )
133 CanvasUtils.printLog( "JavaCanvas.copyRect() called" );
135 // TODO: create temp image when transform is non-trivial
137 if( sourceCanvas == this )
139 // copy rectangle within the canvas
140 getGraphics().copyArea((int)sourceRect.X1,
141 (int)sourceRect.Y1,
142 (int)(sourceRect.X2 - sourceRect.X1),
143 (int)(sourceRect.Y2 - sourceRect.Y1),
144 (int)(destRect.X1 - sourceRect.X1),
145 (int)(destRect.Y1 - sourceRect.Y1) );
147 else
149 if( sourceCanvas instanceof JavaCanvas )
151 // cache
152 CanvasUtils.setupGraphicsState( getGraphics(), destViewState, destRenderState, CanvasUtils.alsoSetupPaint );
154 java.awt.Image backBuffer = ((JavaCanvas)sourceCanvas).backBuffer.getBackBuffer();
156 // TODO: really extract correct source rect here
157 getGraphics().drawImage( backBuffer, 0, 0, null);
158 CanvasUtils.postRenderImageTreatment( backBuffer );
160 // TODO: foreign canvas
164 //----------------------------------------------------------------------------------
167 // XIntegerBitmap implementation
168 // =============================
171 public synchronized byte[] getData( IntegerBitmapLayout[] bitmapLayout,
172 IntegerRectangle2D rect )
174 int [] pixelData = bitmap.getRGB( rect.X1, rect.Y1, rect.X2 - rect.X1, rect.Y1 - rect.Y2, null, 0, 0 );
176 return CanvasUtils.int2byte( pixelData );
179 //----------------------------------------------------------------------------------
181 public synchronized void setData( byte[] data, IntegerBitmapLayout bitmapLayout, com.sun.star.geometry.IntegerRectangle2D rect )
183 int [] pixelData = CanvasUtils.byte2int( data );
184 bitmap.setRGB( rect.X1, rect.Y1, rect.X2 - rect.X1, rect.Y2 - rect.Y1, pixelData, 0, bitmap.getWidth() );
187 //----------------------------------------------------------------------------------
189 public synchronized void setPixel( byte[] color, IntegerBitmapLayout bitmapLayout, com.sun.star.geometry.IntegerPoint2D pos )
191 if( color.length != 4 )
192 CanvasUtils.printLog( "CanvasBitmap.setPixel: Wrong color format" );
194 int pixel = color[0] + (color[1] + (color[2] + color[3]*256)*256)*256;
195 bitmap.setRGB( pos.X, pos.Y, pixel );
198 //----------------------------------------------------------------------------------
200 public synchronized byte[] getPixel( IntegerBitmapLayout[] bitmapLayout,
201 IntegerPoint2D pos )
203 int pixel = bitmap.getRGB( pos.X, pos.Y );
205 byte[] res = new byte[4];
206 res[0] = (byte)(pixel & 255);
207 res[1] = (byte)((pixel/256) & 255);
208 res[2] = (byte)((pixel/256/256) & 255);
209 res[3] = (byte)((pixel/256/256/256) & 255);
211 return res;
214 //----------------------------------------------------------------------------------
216 public synchronized XBitmapPalette getPalette()
218 return null;
221 //----------------------------------------------------------------------------------
223 public synchronized IntegerBitmapLayout getMemoryLayout()
225 // TODO: finish that one
226 IntegerBitmapLayout layout = new IntegerBitmapLayout();
228 return layout;
231 //----------------------------------------------------------------------------------
234 // XServiceInfo impl
235 // =================
238 private static final String s_implName = "XIntegerBitmap.java.impl";
239 private static final String s_serviceName = "com.sun.star.rendering.IntegerBitmap";
241 public String getImplementationName()
243 return s_implName;
246 public String [] getSupportedServiceNames()
248 return new String [] { s_serviceName };
251 public boolean supportsService( String serviceName )
253 return serviceName.equals( s_serviceName );