public class Image4D extends GfxSurface
You can draw into an image and copy an image to a surface using a Graphics object. Images are always 24bpp, and TotalCross supports PNG and JPEG formats at device, and PNG/JPEG/GIF/BMP at desktop (the last two are converted to png when deploying). The deployed png may contain transparency information which is correctly handled.
This is a code sample of how to run a multi-framed image. Note that it will work in the same way in desktop or in device.
Image img = new Image("alligator.gif"); add(new ImageControl(img),LEFT,TOP,FILL,PREFERRED); for (int i = 0; i < 100; i++) { repaintNow(); Vm.sleep(200); img.nextFrame(); }Some transformation methods returns a new instance of this image and other apply to the current instance. To preserve an image with a single frame, use
getFrameInstance(0)
.
Note: TotalCross does not support grayscale PNG with alpha-channel. Convert the image to true-color with
alpha-channel and it will work fine (the only backdraw is that the new image will be bigger).
The hwScale methods should not be used in images that are shown using transition effects.Graphics
Modifier and Type | Field and Description |
---|---|
int |
alphaMask
A global alpha mask to be applied to the whole image when drawing it, ranging from 0 to 255.
|
java.lang.String |
comment |
static int |
FADE_VALUE |
protected int |
height |
double |
hwScaleH
Hardware accellerated scaling.
|
double |
hwScaleW
Hardware accellerated scaling.
|
int |
lastAccess |
static int |
NO_TRANSPARENT_COLOR
Dumb field to keep compilation compatibility with TC 1
|
protected int[] |
pixelsOfAllFrames |
int |
surfaceType |
int |
transparentColor
Dumb field to keep compilation compatibility with TC 1
|
boolean |
useAlpha
Dumb field to keep compilation compatibility with TC 1
|
protected int |
width |
Constructor and Description |
---|
Image4D(byte[] fullDescription) |
Image4D(byte[] fullDescription,
int len) |
Image4D(int width,
int height) |
Image4D(Stream s) |
Image4D(java.lang.String path) |
Modifier and Type | Method and Description |
---|---|
void |
applyChanges() |
void |
applyColor(int color)
Applies the given color r,g,b values to all pixels of this image,
preserving the transparent color and alpha channel, if set.
|
void |
applyColor2(int color)
Applies the given color r,g,b values to all pixels of this image,
preserving the transparent color and alpha channel, if set.
|
void |
applyFade(int fadeValue)
Applies the given fade value to r,g,b of this image while preserving the alpha value.
|
void |
changeColors(int from,
int to) |
void |
createJpg(Stream s,
int quality) |
void |
createPng(Stream s) |
boolean |
equals(java.lang.Object o)
Returns true if the given Image object has the same size and RGB pixels of this one.
|
void |
finalize() |
Image4D |
getAlphaInstance(int delta) |
Image4D |
getClippedInstance(int x,
int y,
int w,
int h)
Returns a clipped image from the current position.
|
Image4D |
getCopy()
Gets a copy of this image; if the image is multi-framed, returns a copy of the first frame.
|
int |
getCurrentFrame() |
Image4D |
getFadedInstance() |
Image4D |
getFadedInstance(int backColor) |
int |
getFrameCount() |
Image4D |
getFrameInstance(int frame)
In a multi-frame image, returns a copy of the given frame.
|
Graphics |
getGraphics() |
int |
getHeight() |
Image4D |
getHwScaledInstance(int width,
int height) |
static Image |
getJpegBestFit(java.lang.String path,
int targetWidth,
int targetHeight) |
static Image |
getJpegScaled(java.lang.String path,
int scaleNumerator,
int scaleDenominator) |
java.lang.String |
getPath() |
void |
getPixelRow(byte[] fillIn,
int y) |
Image4D |
getRotatedScaledInstance(int percScale,
int angle,
int fillColor) |
Image4D |
getScaledInstance(int newWidth,
int newHeight) |
Image4D |
getSmoothScaledInstance(int newWidth,
int newHeight) |
Image4D |
getSmoothScaledInstance(int newWidth,
int newHeight,
int backColor)
Deprecated.
TotalCross 2 no longer uses the backColor parameter.
|
Image4D |
getTouchedUpInstance(byte brightness,
byte contrast) |
int |
getWidth() |
int |
getX()
Returns 0
|
int |
getY()
Returns 0
|
Image4D |
hwScaledBy(double scaleX,
double scaleY) |
Image4D |
hwScaledFixedAspectRatio(int newSize,
boolean isHeight) |
static boolean |
isSupported(java.lang.String filename)
Returns true if the given filename is a supported image: Png or Jpeg.
|
static Image |
loadFrom(PDBFile cat,
java.lang.String name) |
void |
lockChanges() |
static void |
nativeResizeJpeg(java.lang.String inputPath,
java.lang.String outputPath,
int maxPixelSize) |
void |
nextFrame() |
void |
prevFrame() |
static void |
resizeJpeg(java.lang.String inputPath,
java.lang.String outputPath,
int maxPixelSize) |
void |
saveTo(PDBFile cat,
java.lang.String name) |
Image4D |
scaledBy(double scaleX,
double scaleY) |
void |
setCurrentFrame(int nr) |
void |
setFrameCount(int n) |
void |
setHwScaleFixedAspectRatio(int newSize,
boolean isHeight) |
Image |
setTransparentColor(int color) |
Image4D |
smoothScaledBy(double scaleX,
double scaleY) |
Image4D |
smoothScaledBy(double scaleX,
double scaleY,
int backColor)
Deprecated.
TotalCross 2 no longer uses the backColor parameter.
|
Image4D |
smoothScaledFixedAspectRatio(int newSize,
boolean isHeight) |
Image4D |
smoothScaledFixedAspectRatio(int newSize,
boolean isHeight,
int backColor)
Deprecated.
TotalCross 2 no longer uses the backColor parameter.
|
Image4D |
smoothScaledFromResolution(int originalRes)
Returns a smooth scaled instance of this image with a fixed aspect ratio
based on the given resolution (which is the resolution that you used to MAKE the image).
|
Image4D |
smoothScaledFromResolution(int originalRes,
int backColor)
Deprecated.
TotalCross 2 no longer uses the backColor parameter.
|
static void |
writeFrameCount(java.lang.String filePath,
int count)
Utility method used to change the frame count of an image.
|
public int surfaceType
protected int width
protected int height
public int transparentColor
public boolean useAlpha
public int alphaMask
public int lastAccess
protected int[] pixelsOfAllFrames
public java.lang.String comment
public double hwScaleW
Settings.isOpenGL
or on JavaSE.
If you set this in non-opengl environments, nothing will happen; you should use the
hwScaledBy, getHwScaledInstance and hwScaledFixedAspectRatio methods.
To apply the changes, just call repaint()
.setHwScaleFixedAspectRatio(int,boolean)
,
hwScaledBy(double, double)
,
hwScaledFixedAspectRatio(int, boolean)
,
getHwScaledInstance(int, int)
public double hwScaleH
Settings.isOpenGL
or on JavaSE.
If you set this in non-opengl environments, nothing will happen; you should use the
hwScaledBy, getHwScaledInstance and hwScaledFixedAspectRatio methods.
To apply the changes, just call repaint()
.setHwScaleFixedAspectRatio(int,boolean)
,
hwScaledBy(double, double)
,
hwScaledFixedAspectRatio(int, boolean)
,
getHwScaledInstance(int, int)
public static final int NO_TRANSPARENT_COLOR
public static int FADE_VALUE
public Image4D(int width, int height) throws ImageException
ImageException
public Image4D(java.lang.String path) throws ImageException
ImageException
public Image4D(Stream s) throws ImageException, IOException
ImageException
IOException
public Image4D(byte[] fullDescription) throws ImageException
ImageException
public Image4D(byte[] fullDescription, int len) throws ImageException
ImageException
public java.lang.String getPath()
public void setFrameCount(int n) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ImageException
java.lang.IllegalArgumentException
java.lang.IllegalStateException
ImageException
public int getFrameCount()
public void setCurrentFrame(int nr)
public int getCurrentFrame()
public void nextFrame()
public void prevFrame()
public int getHeight()
getHeight
in class GfxSurface
public int getWidth()
getWidth
in class GfxSurface
public Graphics getGraphics()
public void applyChanges()
public void changeColors(int from, int to)
public void saveTo(PDBFile cat, java.lang.String name) throws ImageException, IOException
ImageException
IOException
public static Image loadFrom(PDBFile cat, java.lang.String name) throws IOException, ImageException
IOException
ImageException
public void createPng(Stream s) throws ImageException, IOException
ImageException
IOException
public void getPixelRow(byte[] fillIn, int y)
public Image4D getFadedInstance(int backColor) throws ImageException
ImageException
public Image4D getFadedInstance() throws ImageException
ImageException
public Image4D getAlphaInstance(int delta) throws ImageException
ImageException
public Image4D smoothScaledFixedAspectRatio(int newSize, boolean isHeight) throws ImageException
ImageException
public Image4D getScaledInstance(int newWidth, int newHeight) throws ImageException
ImageException
public Image4D getSmoothScaledInstance(int newWidth, int newHeight) throws ImageException
ImageException
public Image4D getRotatedScaledInstance(int percScale, int angle, int fillColor) throws ImageException
ImageException
public Image4D getTouchedUpInstance(byte brightness, byte contrast) throws ImageException
ImageException
public Image4D scaledBy(double scaleX, double scaleY) throws ImageException
ImageException
public Image4D smoothScaledBy(double scaleX, double scaleY) throws ImageException
ImageException
public Image setTransparentColor(int color)
public void finalize()
finalize
in class java.lang.Object
public void lockChanges()
public void createJpg(Stream s, int quality) throws ImageException, IOException
ImageException
IOException
public void setHwScaleFixedAspectRatio(int newSize, boolean isHeight)
public Image4D hwScaledFixedAspectRatio(int newSize, boolean isHeight) throws ImageException
ImageException
public Image4D getHwScaledInstance(int width, int height) throws ImageException
ImageException
public Image4D hwScaledBy(double scaleX, double scaleY) throws ImageException
ImageException
public int getX()
getX
in class GfxSurface
public int getY()
getY
in class GfxSurface
public static boolean isSupported(java.lang.String filename)
public Image4D getFrameInstance(int frame) throws ImageException
ImageException
public final void applyColor(int color)
color
- The color to be appliedpublic final Image4D smoothScaledFromResolution(int originalRes) throws ImageException
image_size*min(screen_size)/original_resolution
originalRes
- The original resolution that the image was developed for. Its a good idea to create images for 320x320 and then scale them down.ImageException
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public final void applyColor2(int color)
color
- The color to be applied@Deprecated public Image4D getSmoothScaledInstance(int newWidth, int newHeight, int backColor) throws ImageException
ImageException
@Deprecated public Image4D smoothScaledBy(double scaleX, double scaleY, int backColor) throws ImageException
ImageException
@Deprecated public Image4D smoothScaledFixedAspectRatio(int newSize, boolean isHeight, int backColor) throws ImageException
ImageException
@Deprecated public final Image4D smoothScaledFromResolution(int originalRes, int backColor) throws ImageException
ImageException
public void applyFade(int fadeValue)
public static void writeFrameCount(java.lang.String filePath, int count)
Image.writeFrameCount("c:/project/src/images/people.png",2);Be careful that this must be done once only; this method does not exist in the device and will abort the vm if you try to call it there!
public Image4D getCopy() throws ImageException
ImageException
public Image4D getClippedInstance(int x, int y, int w, int h) throws ImageException
ImageException
public static void resizeJpeg(java.lang.String inputPath, java.lang.String outputPath, int maxPixelSize)
public static void nativeResizeJpeg(java.lang.String inputPath, java.lang.String outputPath, int maxPixelSize)
public static Image getJpegBestFit(java.lang.String path, int targetWidth, int targetHeight) throws java.io.IOException, ImageException
java.io.IOException
ImageException
public static Image getJpegScaled(java.lang.String path, int scaleNumerator, int scaleDenominator) throws java.io.IOException, ImageException
java.io.IOException
ImageException