📄 fsimageconstructor.java
字号:
*
* <tr>
* <td>FSDefineJPEGImage2</td>
* <td>The image format is JPEG and the level of transparency is not defined.</td>
* </tr>
*
* <tr>
* <td>FSDefineImage</td>
* <td>The image format is IDX8, RGB5 or RGB8.</td>
* </tr>
*
* <tr>
* <td>FSDefineImage2</td>
* <td>The image format is IDXA, RGBA.</td>
* </tr>
*
* </table>
*
* @param identifier an unique identifier that is used to reference the image definition in a Flash movie.
*/
public FSDefineObject defineImage(int identifier)
{
FSDefineObject object = null;
switch (format)
{
case JPEG: object = new FSDefineJPEGImage2(identifier, jpegImage); break;
case IDX8: object = new FSDefineImage(identifier, width, height, colourTable.length, zip(getImage(), colourTable, false)); break;
case IDXA: object = new FSDefineImage2(identifier, width, height, colourTable.length, zip(getImage(), colourTable, true)); break;
case RGB5: object = new FSDefineImage(identifier, width, height, zip(getImage()), 16); break;
case RGB8: object = new FSDefineImage(identifier, width, height, zip(getImage()), 24); break;
case RGBA: object = new FSDefineImage2(identifier, width, height, zip(getImage())); break;
}
return object;
}
private byte[] getImage()
{
int row = 0;
int col = 0;
int index = 0;
int scanLength = 0;
byte[] formattedImage = null;
switch (format)
{
case IDX8:
case IDXA:
scanLength = (width + 3) & ~3;
formattedImage = new byte[scanLength*height];
for (row=0; row<height; row++)
{
for (col=0; col<width; col++)
formattedImage[index++] = indexedImage[row][col];
for (; col<scanLength; col++)
formattedImage[index++] = 0;
}
break;
case RGB5:
scanLength = width + (width & 1);
formattedImage = new byte[scanLength*height*2];
for (row=0; row<height; row++)
{
for (col=0; col<width; col++)
{
int red = (colorImage[row][col][0] & 0xF8) << 7;
int green = (colorImage[row][col][1] & 0xF8) << 2;
int blue = (colorImage[row][col][2] & 0xF8) >> 3;
int colour = (red | green | blue) & 0x7FFF;
formattedImage[index++] = (byte) (colour >> 8);
formattedImage[index++] = (byte) colour;
}
for (; col<scanLength; col++)
{
formattedImage[index++] = 0;
formattedImage[index++] = 0;
}
}
break;
case RGB8:
formattedImage = new byte[width*height*4];
for (row=0; row<height; row++)
{
for (col=0; col<width; col++)
{
formattedImage[index++] = (byte)0xFF;
formattedImage[index++] = colorImage[row][col][0];
formattedImage[index++] = colorImage[row][col][1];
formattedImage[index++] = colorImage[row][col][2];
}
}
break;
case RGBA:
formattedImage = new byte[width*height*4];
for (row=0; row<height; row++)
{
for (col=0; col<width; col++)
{
int alpha = colorImage[row][col][3] & 0xFF;
int red = ((colorImage[row][col][0] & 0xFF) * alpha) / 255;
int green = ((colorImage[row][col][1] & 0xFF) * alpha) / 255;
int blue = ((colorImage[row][col][2] & 0xFf) * alpha) / 255;
formattedImage[index++] = (byte)alpha;
formattedImage[index++] = (byte)red;
formattedImage[index++] = (byte)green;
formattedImage[index++] = (byte)blue;
}
}
break;
}
return formattedImage;
}
/**
* Generates the shape definition object that is required to display an image in a Flash movie.
* The shape is generated with a single fill style (FSBitmapFill object). The origin of the shape
* is specified relative to the top left corner of the image.
*
* The borderStyle argument specifies a border that will be drawn around the image. The style
* may be set to null is no border is drawn.
*
* @param shapeIdentifier an unique identifier that is used to reference the shape definition in a
* Flash movie.
*
* @param imageIdentifier the unique identifier of the image generated using the defineImage() method.
*
* @param xOrigin the offset in pixels along the x-axis, relative to the top left corner of
* the image, where the origin (0,0) of the shape will be located.
*
* @param yOrigin the offset in pixels along the y-axis, relative to the top left corner of
* the image, where the origin (0,0) of the shape will be located.
*
* @param borderStyle the style drawn around the border of the image. May be null if no
* border is drawn.
*/
public FSDefineShape3 defineEnclosingShape(int shapeIdentifier, int imageIdentifier, int xOrigin, int yOrigin, FSSolidLine borderStyle)
{
int lineWidth = 0;
if (borderStyle != null)
lineWidth = borderStyle.getWidth() / 2;
FSBounds bounds = new FSBounds(-xOrigin*20-lineWidth, -yOrigin*20-lineWidth,
(width-xOrigin)*20+lineWidth, (height-yOrigin)*20+lineWidth);
FSShape shape = new FSShape();
shape.add(new FSShapeStyle((borderStyle != null) ? 1 : 0, 1, 0, -xOrigin*20, -yOrigin*20));
shape.add(new FSLine(width*20, 0));
shape.add(new FSLine(0, height*20));
shape.add(new FSLine(-width*20, 0));
shape.add(new FSLine(0, -height*20));
FSDefineShape3 definition = new FSDefineShape3(shapeIdentifier, bounds, new ArrayList(), new ArrayList(), shape);
FSCoordTransform transform = new FSCoordTransform(-xOrigin*20, -yOrigin*20, 20.0, 20.0);
if (borderStyle != null)
definition.add(borderStyle);
definition.add(new FSBitmapFill(FSFillStyle.Clipped, imageIdentifier, transform));
return definition;
}
private boolean jpegInfo()
{
FSCoder coder = new FSCoder(FSCoder.BIG_ENDIAN, jpegImage);
if (coder.readWord(2, false) != 0xffd8)
return false;
while (true)
{
int marker = coder.readWord(2, false);
int size = coder.readWord(2, false);
if ((marker & 0xff00) != 0xff00)
return false;
if (marker >= 0xffc0 && marker <= 0xffcf && marker != 0xffc4 && marker != 0xffc8)
{
coder.readWord(1, false);
height = coder.readWord(2, false);
width = coder.readWord(2, false);
return true;
}
else
{
coder.adjustPointer((size - 2) << 3);
}
}
}
private void decodeJPEG(byte[] bytes) throws DataFormatException
{
format = JPEG;
jpegImage = bytes;
if (jpegInfo() == false)
throw new DataFormatException();
}
private void decodeBMP(byte[] bytes) throws DataFormatException
{
FSCoder coder = new FSCoder(FSCoder.LITTLE_ENDIAN, bytes);
for (int i=0; i<2; i++)
{
if (coder.readWord(1, false) != bmpSignature[i])
throw new DataFormatException("Not a valid BMP file");
}
coder.readWord(4, false); // fileSize
coder.readWord(4, false); // reserved
int offset = coder.readWord(4, false);
int headerSize = coder.readWord(4, false);
int bitsPerPixel = 0;
int coloursUsed = 0;
switch (headerSize)
{
case 12:
width = coder.readWord(2, false);
height = coder.readWord(2, false);
coder.readWord(2, false); // bitPlanes
bitsPerPixel = coder.readWord(2, false);
break;
case 40:
width = coder.readWord(4, false);
height = coder.readWord(4, false);
coder.readWord(2, false); // bitPlanes
bitsPerPixel = coder.readWord(2, false);
attributes[COMPRESSION_METHOD] = coder.readWord(4, false);
coder.readWord(4, false); //imageSize
coder.readWord(4, false); // horizontalResolution
coder.readWord(4, false); // verticalResolution
coloursUsed = coder.readWord(4, false);
coder.readWord(4, false); // importantColours
break;
default:
break;
}
if (attributes[COMPRESSION_METHOD] == BI_BITFIELDS)
{
attributes[RED_MASK] = coder.readWord(4, false);
attributes[GREEN_MASK] = coder.readWord(4, false);
attributes[BLUE_MASK] = coder.readWord(4, false);
}
switch (bitsPerPixel)
{
case 1: format = IDX8; attributes[BIT_DEPTH] = 1; attributes[COLOUR_COMPONENTS] = 1; break;
case 2: format = IDX8; attributes[BIT_DEPTH] = 2; attributes[COLOUR_COMPONENTS] = 1; break;
case 4: format = IDX8; attributes[BIT_DEPTH] = 4; attributes[COLOUR_COMPONENTS] = 1; break;
case 8: format = IDX8; attributes[BIT_DEPTH] = 8; attributes[COLOUR_COMPONENTS] = 1; break;
case 16: format = RGB5; attributes[BIT_DEPTH] = 5; attributes[COLOUR_COMPONENTS] = 3; break;
case 24: format = RGB8; attributes[BIT_DEPTH] = 8; attributes[COLOUR_COMPONENTS] = 3; break;
case 32: format = RGBA; attributes[BIT_DEPTH] = 8; attributes[COLOUR_COMPONENTS] = 4; break;
}
if (format == IDX8)
{
coloursUsed = 1 << bitsPerPixel;
colourTable = new byte[coloursUsed][4];
indexedImage = new byte[height][width];
if (headerSize == 12)
{
for (int i=0; i < coloursUsed; i++)
{
colourTable[i][3] = (byte)0xFF;
colourTable[i][2] = (byte)coder.readWord(1, false);
colourTable[i][1] = (byte)coder.readWord(1, false);
colourTable[i][0] = (byte)coder.readWord(1, false);
}
}
else
{
for (int i=0; i < coloursUsed; i++)
{
colourTable[i][0] = (byte)coder.readWord(1, false);
colourTable[i][1] = (byte)coder.readWord(1, false);
colourTable[i][2] = (byte)coder.readWord(1, false);
colourTable[i][3] = (byte)(coder.readWord(1, false) | 0xFF);
}
}
coder.setPointer(offset<<3);
switch (attributes[COMPRESSION_METHOD])
{
case BI_RGB: decodeIDX8(coder); break;
case BI_RLE8: decodeRLE8(coder); break;
case BI_RLE4: decodeRLE4(coder); break;
}
}
else
{
colorImage = new byte[height][width][4];
coder.setPointer(offset<<3);
switch (format)
{
case RGB5: decodeRGB5(coder); break;
case RGB8: decodeRGB8(coder); break;
case RGBA: decodeRGBA(coder); break;
}
}
}
private void decodeIDX8(FSCoder coder)
{
int h = 0;
int w = 0;
int bitsRead = 0;
for (h=height-1; h>0; h--)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -