001: /*
002: * $Id: Bitmap.java,v 1.4 2002/06/24 13:53:36 awason Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.api.image;
052:
053: import java.io.*;
054: import java.awt.geom.*;
055: import org.openlaszlo.iv.flash.util.*;
056: import org.openlaszlo.iv.flash.cache.*;
057: import org.openlaszlo.iv.flash.url.*;
058: import org.openlaszlo.iv.flash.parser.*;
059: import org.openlaszlo.iv.flash.api.*;
060: import org.openlaszlo.iv.flash.api.shape.*;
061:
062: /**
063: * Abstract class which represent image
064: */
065: public abstract class Bitmap extends FlashDef {
066:
067: protected int tagCode;
068:
069: public int getTag() {
070: return tagCode;
071: }
072:
073: public void setTag(int tagCode) {
074: this .tagCode = tagCode;
075: }
076:
077: public boolean isConstant() {
078: return true;
079: }
080:
081: /**
082: * Return width of the bitmap in pixels
083: */
084: public int getWidth() {
085: return (int) getBounds().getWidth();
086: }
087:
088: /**
089: * Return height of the bitmap in pixels
090: */
091: public int getHeight() {
092: return (int) getBounds().getHeight();
093: }
094:
095: /**
096: * Return size of the image in bytes, which it's occupied in memory
097: */
098: public abstract int getSize();
099:
100: /**
101: * Create instance of this bitmap<br>
102: * No scaling, no translating
103: *
104: * @return newly created instance (can be added to a frame)
105: */
106: public Instance newInstance() {
107: return newInstance(0, 0, false, false);
108: }
109:
110: /**
111: * Create instance of this bitmap
112: *
113: * @param width if scale is true it's width of target image in twixels (there is no resampling, just transformation)
114: * @param height if scale is true it's height of target image in twixels (there is no resampling, just transformation)
115: * @param scale if true then transform (scale) image to fit width and height (there is no resampling, just transformation)
116: * @param center if true then transform (translate) image so that its origin in the center
117: * @return newly created instance (can be added to a frame)
118: */
119: public Instance newInstance(int width, int height, boolean scale,
120: boolean center) {
121:
122: int bmWidth = getWidth() * 20;
123: int bmHeight = getHeight() * 20;
124:
125: Shape shape = new Shape();
126: shape.setFillStyle0(0);
127: shape.setFillStyle1(FillStyle.newClippedBitmap(this ));
128:
129: shape.drawRectangle(0, 0, bmWidth, bmHeight);
130: shape.setBounds(GeomHelper
131: .newRectangle(0, 0, bmWidth, bmHeight));
132:
133: AffineTransform myMatrix;
134: if (scale) {
135: myMatrix = AffineTransform.getScaleInstance((double) width
136: / bmWidth, (double) height / bmHeight);
137: } else {
138: myMatrix = center ? new AffineTransform() : null;
139: }
140: if (center) {
141: myMatrix.translate(-bmWidth / 2, -bmHeight / 2);
142: }
143:
144: Instance inst = new Instance();
145: inst.def = shape;
146: inst.matrix = myMatrix;
147:
148: return inst;
149: }
150:
151: public static Bitmap newBitmap(String fileName) throws IVException,
152: IOException {
153: return newBitmap(IVUrl.newUrl(fileName));
154: }
155:
156: public static Bitmap newBitmap(IVUrl url) throws IVException,
157: IOException {
158: Bitmap bitmap = newBitmap(Util.readUrl(url));
159: if (bitmap != null)
160: return bitmap;
161: throw new IVException(Resource.UNSUPMEDIA, new Object[] { url
162: .getName() });
163: }
164:
165: /**
166: * Create bitmap from buffer
167: *
168: */
169: public static Bitmap newBitmap(FlashBuffer fb) throws IVException {
170: // check the type of the bitmap
171: fb.setPos(0);
172: int b0 = fb.getUByte();
173: int b1 = fb.getUByte();
174: int b2 = fb.getUByte();
175: int b3 = fb.getUByte();
176:
177: if (b0 == 0xff && b1 == 0xd8) { // jpeg: 0xff, 0xd8
178: return JPEGBitmap.newJPEGBitmap(fb);
179: } else if (b0 == 'G' && b1 == 'I' && b2 == 'F') { // gif: 'GIF'
180: return LLBitmap.newGIFBitmap(fb);
181: } else if (b0 == 0x89 && b1 == 0x50 && b2 == 0x4e && b3 == 0x47) { // png
182: return LLBitmap.newPNGBitmap(fb);
183: } else {
184: return null;
185: }
186: }
187:
188: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
189: super .copyInto(item, copier);
190: ((Bitmap) item).tagCode = tagCode;
191: return item;
192: }
193:
194: }
|