Source code: com/anotherbigidea/flash/movie/Image.java
1 /****************************************************************
2 * Copyright (c) 2001, David N. Main, All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the
6 * following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * 3. The name of the author may not be used to endorse or
18 * promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ****************************************************************/
34 package com.anotherbigidea.flash.movie;
35
36 import java.io.*;
37 import java.util.*;
38 import com.anotherbigidea.flash.interfaces.*;
39 import com.anotherbigidea.flash.writers.*;
40 import com.anotherbigidea.flash.readers.*;
41 import com.anotherbigidea.flash.structs.*;
42 import com.anotherbigidea.flash.SWFConstants;
43
44 /**
45 * Base class for Image symbols.
46 * Note that Images cannot be placed directly on the stage - they have to be
47 * used as image fills for shapes.
48 */
49 public abstract class Image extends Symbol
50 {
51 /**
52 * A lossless image (similar to PNG).
53 *
54 * There are 3 formats - 8, 16 and 32 bit. For 8 and 16 bit images
55 * there is a color table and the image data consists of either an 8 or
56 * 16 bit index into the table for each pixel.
57 *
58 * 32 bit images have no color table - each pixel consists of 4 bytes:
59 * (alpha,red,green,blue). If there is no alpha then the first byte will
60 * be 255.
61 *
62 * For all formats, the length of each row of pixel data must be a multiple
63 * of 32 bits. If the actual row data is smaller then it should be padded
64 * up to next multiple of 32 bits.
65 */
66 public static class Lossless extends Image
67 {
68 protected byte[] imageData;
69 protected Color[] colorTable;
70 protected double width;
71 protected double height;
72 protected boolean hasAlpha;
73 protected int format;
74
75
76 /**
77 * @param colorTable may be null for 32 bit bitmaps
78 * @param imageData the pixel data
79 * @param width in pixels
80 * @param height in pixels
81 * @param hasAlpha whether the image contains alpha values
82 * @param format one of: SWFConstants.BITMAP_FORMAT_8_BIT,
83 * SWFConstants.BITMAP_FORMAT_16_BIT,
84 * SWFConstants.BITMAP_FORMAT_32_BIT
85 */
86 public Lossless( Color[] colorTable, byte[] imageData, double width,
87 double height, boolean hasAlpha, int format )
88 {
89 this.colorTable = colorTable;
90 this.imageData = imageData;
91 this.width = width;
92 this.height = height;
93 this.hasAlpha = hasAlpha;
94 this.format = format;
95 }
96
97 public byte[] getImageData() { return imageData; }
98 public Color[] getColorTable() { return colorTable; }
99 public double getWidth() { return width; }
100 public double getHeight() { return height; }
101 public boolean hasAlpha() { return hasAlpha; }
102 public int getFormat() { return format; }
103
104 protected int defineSymbol( Movie movie,
105 SWFTagTypes timelineWriter,
106 SWFTagTypes definitionWriter )
107 throws IOException
108 {
109 int id = getNextId(movie);
110
111 if( hasAlpha )
112 {
113 definitionWriter.tagDefineBitsLossless2(
114 id, format, (int)width, (int)height,
115 colorTable, imageData );
116 }
117 else
118 {
119 definitionWriter.tagDefineBitsLossless(
120 id, format, (int)width, (int)height,
121 colorTable, imageData );
122 }
123
124 return id;
125 }
126 }
127
128 /**
129 * A JPEG Image that can be used as a fill for Shapes. The JPEG image must
130 * be "baseline" - a "progressive" JPEG will cause the Flash player to have
131 * runtime problems.
132 */
133 public static class JPEG extends Image
134 {
135 protected InputStream jpegIn;
136 protected byte[] jpegData;
137
138 /**
139 * A JPEG image that will read from an input stream.
140 */
141 public JPEG( InputStream jpegImage )
142 {
143 jpegIn = jpegImage;
144 }
145
146 /**
147 * Construct a JPEG image from byte data. Note that the
148 * data must include the JPEG header ( 0xff,0xd9,0xff,0xd8 ).
149 */
150 public JPEG( byte[] imageData )
151 {
152 jpegData = imageData;
153 }
154
155 /**
156 * Get the raw image data. This will include the JPEG stream header(s)
157 * ( 0xff,0xd9,0xff,0xd8 ).
158 */
159 public byte[] getImageData() { return jpegData; }
160
161 protected int defineSymbol( Movie movie,
162 SWFTagTypes timelineWriter,
163 SWFTagTypes definitionWriter )
164 throws IOException
165 {
166 int id = getNextId(movie);
167
168 if( jpegData != null )
169 {
170 definitionWriter.tagDefineBitsJPEG2( id, jpegData );
171 }
172 else if( jpegIn != null )
173 {
174 definitionWriter.tagDefineBitsJPEG2( id, jpegIn );
175 }
176
177 return id;
178 }
179 }
180
181 }