public class Image 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
A textual description stored in the PNG image.
|
static int |
FADE_VALUE
Used in getFadedInstance().
|
protected int |
height |
double |
hwScaleH
Hardware accellerated scaling.
|
double |
hwScaleW
Hardware accellerated scaling.
|
static int |
NO_TRANSPARENT_COLOR
Dumb field to keep compilation compatibility with TC 1
|
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 |
---|
Image(byte[] fullDescription)
Parses an image from the given byte array.
|
Image(byte[] fullDescription,
int length)
Parses an image from the given byte array with the specified length.
|
Image(int width,
int height)
Creates an image of the specified width and height.
|
Image(Stream s)
Loads a BMP, JPEG, GIF or PNG image from a totalcross.io.Stream.
|
Image(java.lang.String path)
Loads and constructs an image from a file.
|
Modifier and Type | Method and Description |
---|---|
void |
applyChanges()
Applies any pending changes made in this image.
|
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)
Changes all the pixels of the image from one color to the other.
|
void |
createJpg(Stream s,
int quality)
Saves this image as a jpeg file to the given stream.
NOT supported on Blackberry. |
void |
createJpg4B(Stream s,
int quality) |
void |
createPng(Stream s)
Saves this image as a 24 BPP .png file format (if useAlpha is true, it saves as 32 BPP),
to the given stream.
|
boolean |
equals(java.lang.Object o)
Returns true if the given Image object has the same size and RGB pixels of this one.
|
Image |
getAlphaInstance(int delta)
Adds the given value to each pixel's alpha-channel of this image.
|
Image |
getClippedInstance(int x,
int y,
int w,
int h)
Returns a clipped image from the current position.
|
Image |
getCopy()
Gets a copy of this image; if the image is multi-framed, returns a copy of the first frame.
|
int |
getCurrentFrame()
Returns the current frame in a multi-frame image.
|
Image |
getFadedInstance()
Creates a faded instance of this image, decreasing the alpha-channel by 128 for all pixels.
|
Image |
getFadedInstance(int backColor)
Deprecated.
Use getFadedInstance() instead
|
int |
getFrameCount()
Returns the frame count of this image.
|
Image |
getFrameInstance(int frame)
In a multi-frame image, returns a copy of the given frame.
|
Graphics |
getGraphics()
Returns a new Graphics instance that can be used to drawing in this image.
|
int |
getHeight()
Returns the height of the image.
|
Image |
getHwScaledInstance(int width,
int height)
At non OpenGL devices, is the same of getSmoothScaledInstance;
At openGL ones, this method shares all image informations
while changing only the hwScaleW/hwScaleH parameters.
|
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()
Returns the path used to create the Image.
|
void |
getPixelRow(byte[] fillIn,
int y)
Used in saveTo method.
|
int[] |
getPixels()
Used only at desktop to get the image's pixels.
|
Image |
getRotatedScaledInstance(int scale,
int angle,
int fillColor)
Creates a rotated and/or scaled version of this Image.
|
Image |
getScaledInstance(int newWidth,
int newHeight)
Returns the scaled instance for this image.
|
Image |
getSmoothScaledInstance(int newWidth,
int newHeight)
Returns the scaled instance using the area averaging algorithm for this image.
|
Image |
getSmoothScaledInstance(int newWidth,
int newHeight,
int backColor)
Deprecated.
TotalCross 2 no longer uses the backColor parameter.
|
Image |
getTouchedUpInstance(byte brightness,
byte contrast)
Creates a touched-up version of this Image with the specified brightness and contrast.
|
int |
getWidth()
Returns the width of the image.
|
int |
getX()
Returns 0
|
int |
getY()
Returns 0
|
Image |
hwScaledBy(double scaleX,
double scaleY)
At non OpenGL devices, is the same of smoothScaledBy;
At openGL ones, this method shares all image informations
while changing only the hwScaleW/hwScaleH parameters.
|
Image |
hwScaledFixedAspectRatio(int newSize,
boolean isHeight)
At non OpenGL devices, is the same of smoothScaledFixedAspectRatio;
At openGL ones, this method shares all image informations
while changing only the hwScaleW/hwScaleH parameters.
|
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)
Loads an image from a PDB file, if it was previously saved using saveTo method.
|
void |
lockChanges()
In OpenGL platforms, apply changes to the current texture and
frees the memory used for the pixels in internal memory (the
image can, however, be drawn on screen because the texture will
be ready).
|
void |
nextFrame()
Move to next frame in a multi-frame image.
|
void |
prevFrame()
Move to the previous frame in a multi-frame image.
|
static void |
resizeJpeg(java.lang.String inputPath,
java.lang.String outputPath,
int maxPixelSize) |
void |
saveTo(PDBFile cat,
java.lang.String name)
Saves this image as a Windows .png file format to the given PDBFile.
|
Image |
scaledBy(double scaleX,
double scaleY)
Returns the scaled instance for this image, given the scale arguments.
|
void |
setCurrentFrame(int nr)
Move the contents of the given frame to the currently visible pixels.
|
void |
setFrameCount(int n)
Sets the frame count for this image.
|
void |
setHwScaleFixedAspectRatio(int newSize,
boolean isHeight)
Sets the hwScaleW and hwScaleH fields based on the given new size.
|
Image |
setTransparentColor(int color)
Deprecated.
use the alpha channel instead
|
Image |
smoothScaledBy(double scaleX,
double scaleY)
Returns the scaled instance for this image, given the scale arguments.
|
Image |
smoothScaledBy(double scaleX,
double scaleY,
int backColor)
Deprecated.
TotalCross 2 no longer uses the backColor parameter.
|
Image |
smoothScaledFixedAspectRatio(int newSize,
boolean isHeight)
Returns the scaled instance using fixed aspect ratio for this image, given the scale arguments.
|
Image |
smoothScaledFixedAspectRatio(int newSize,
boolean isHeight,
int backColor)
Deprecated.
TotalCross 2 no longer uses the backColor parameter.
|
Image |
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).
|
Image |
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 java.lang.String comment
public int transparentColor
public boolean useAlpha
public int alphaMask
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 Image(int width, int height) throws ImageException
Image img = new Image(100,100); Graphics g = img.getGraphics(); g.backColor = Color.WHITE; g.fillRect(25,25,50,50); ... Graphics screenG = getGraphics(); screenG.drawImage(img,CENTER,CENTER);
ImageException
public Image(java.lang.String path) throws ImageException, IOException
ImageException
- When the file was not found.IOException
public Image(Stream s) throws ImageException, IOException
IOException
ImageException
public Image(byte[] fullDescription) throws ImageException
// create the image and fill it with something Image img = new Image(160,160); Graphics g = img.getGraphics(); for (int i =0; i < 16; i++) { g.backColor = Color.getRGB(10*i,10*i,10*i); g.fillRect(i*10,0,10,160); } img.applyChanges(); // save the bmp in a byte stream ByteArrayStream bas = new ByteArrayStream(4096); DataStream ds = new DataStream(bas); int totalBytesWritten = img.createPng(ds); // parse the saved png Image im = new Image(bas.getBuffer()); // Caution! the buffer may be greater than totalBytesWritten, but when parsing theres no problem. if (im.getWidth() > 0) // successfully parsed? { getGraphics().drawImage(im,CENTER,CENTER); Vm.sleep(2000); }Caution: if reading a JPEG file, the original array contents will be changed!
ImageException
- Thrown when something was wrong with the image.public Image(byte[] fullDescription, int length) throws ImageException
// create the image and fill it with something Image img = new Image(160,160); Graphics g = img.getGraphics(); for (int i =0; i < 16; i++) { g.backColor = Color.getRGB(10*i,10*i,10*i); g.fillRect(i*10,0,10,160); } img.applyChanges(); // save the bmp in a byte stream ByteArrayStream bas = new ByteArrayStream(4096); DataStream ds = new DataStream(bas); int totalBytesWritten = img.createPng(ds); // parse the saved png Image im = new Image(bas.getBuffer()); // Caution! the buffer may be greater than totalBytesWritten, but when parsing theres no problem. if (im.getWidth() > 0) // successfully parsed? { getGraphics().drawImage(im,CENTER,CENTER); Vm.sleep(2000); }Caution: if reading a JPEG file, the original array contents will be changed!
ImageException
- Thrown when something was wrong with the image.public void setHwScaleFixedAspectRatio(int newSize, boolean isHeight)
public Image hwScaledFixedAspectRatio(int newSize, boolean isHeight) throws ImageException
ImageException
public Image getHwScaledInstance(int width, int height) throws ImageException
ImageException
public Image hwScaledBy(double scaleX, double scaleY) throws ImageException
ImageException
public int[] getPixels()
public java.lang.String getPath()
@Deprecated public Image setTransparentColor(int color)
public void setFrameCount(int n) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ImageException
java.lang.IllegalArgumentException
java.lang.IllegalStateException
ImageException
public int getFrameCount()
public final 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 lockChanges()
public final void changeColors(int from, int to)
applyColor(int)
,
applyColor2(int)
public void saveTo(PDBFile cat, java.lang.String name) throws ImageException, IOException
Here is a sample code:
// create the image and paint over it Image img = new Image(100,100); Graphics g = img.getGraphics(); g.backColor = Color.getRGB(100,150,200); g.fillRect(25,25,50,50); g.foreColor = Color.WHITE; g.drawCircle(50,50,20); // create the PDBFile to save the image. You must change CRTR to match your apps creator ID String pdbName = "images.CRTR.TYPE"; PDBFile pdb = new PDBFile(pdbName, PDBFile.CREATE); img.saveTo(pdb, "boxcircle.png"); pdb.close(); // load the previously created image PDBFile pdb = new PDBFile(pdbName, PDBFile.READ_WRITE); add(new ImageControl(Image.loadFrom(pdb,"boxcircle.png")),CENTER,CENTER); pdb.close();Here's a code that lists the images in a PDB (saved using this method).
public static String[] list(PDBFile cat) throws IOException { DataStream ds = new DataStream(cat); int n = cat.getRecordCount(); String[] names = new String[n]; for (int i =0; i < n; i++) { cat.setRecordPos(i); names[i] = ds.readString(); } return names; }
ImageException
IOException
createPng(totalcross.io.Stream)
,
loadFrom(PDBFile, String)
public static Image loadFrom(PDBFile cat, java.lang.String name) throws IOException, ImageException
IOException
ImageException
saveTo(PDBFile, String)
public void createJpg(Stream s, int quality) throws ImageException, IOException
s
- The output stream used to write the jpeg.quality
- The quality of the image; 100 = no compression, 90 = medium compression,
80 = high compression. Anything below 80 may greatly redude the image's quality. 85 is a common value. In JavaSE, the quality argument is ignored.ImageException
IOException
public void createJpg4B(Stream s, int quality) throws ImageException, IOException
ImageException
IOException
public void createPng(Stream s) throws ImageException, IOException
ImageException
IOException
saveTo(totalcross.io.PDBFile, java.lang.String)
public final void getPixelRow(byte[] fillIn, int y)
public Image getScaledInstance(int newWidth, int newHeight) throws ImageException
ImageException
public Image getSmoothScaledInstance(int newWidth, int newHeight) throws ImageException
Image img2 = img.getSmoothScaledInstance(200,200);In device and JavaSE it uses a Catmull-rom resampling, and in Blackberry it uses an area-average resampling. The reason is that the Catmull-rom consumes more memory and is also slower than the area-average, although the final result is much better.
ImageException
public Image scaledBy(double scaleX, double scaleY) throws ImageException
ImageException
public Image smoothScaledBy(double scaleX, double scaleY) throws ImageException
Image img2 = img.smoothScaledBy(0.75,0.75, getBackColor());
ImageException
public Image smoothScaledFixedAspectRatio(int newSize, boolean isHeight) throws ImageException
newSize
- The new size (width or height) for the imageisHeight
- If true, the newSize is considered as the new height of the image. If false, the newSize is considered the new width of the image.
Example: Image img2 = img.smoothScaledFixed(fmH, true, -1);
ImageException
public Image getRotatedScaledInstance(int scale, int angle, int fillColor) throws ImageException
Image
object is returned which will render
the image at the specified scale
ratio and
rotation angle
.
After rotation, the empty parts of the rectangular area of
the resulting image are filled with the fill
color.
If color
is -1
, then
the fill color is the transparent color, or white if none.
Notes
scale
- a number greater than or equal to 0 stating the percentage
of scaling to be performed.
100 is not scaling, 200 doubles the size, 50 shrinks the image by 2angle
- the rotation angle, expressed in trigonometric degreesfillColor
- the fill color; -1 indicates the transparent color of this image or
Color.WHITE if the transparentColor was not set; use 0 for a transparent background, or 0xFF000000 for the BLACK color.ImageException
@Deprecated public Image getFadedInstance(int backColor) throws ImageException
ImageException
getFadedInstance()
public Image getFadedInstance() throws ImageException
ImageException
FADE_VALUE
public Image getAlphaInstance(int delta) throws ImageException
ImageException
public Image getTouchedUpInstance(byte brightness, byte contrast) throws ImageException
Image
object is returned which will render the image at the specified brigthness
and the specified
contrast
.brightness
- a number between -128 and 127 stating the desired level of brightness. 127 is the highest
brightness level (white image), -128 is no brightness (darkest image).contrast
- a number between -128 and 127 stating the desired level of contrast. 127 is the highest contrast
level, -128 is no contrast.ImageException
public int getX()
getX
in class GfxSurface
public int getY()
getY
in class GfxSurface
public static boolean isSupported(java.lang.String filename)
public final Image getFrameInstance(int frame) throws ImageException
ImageException
public final void applyColor(int color)
color
- The color to be appliedpublic final Image 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 Image getSmoothScaledInstance(int newWidth, int newHeight, int backColor) throws ImageException
ImageException
@Deprecated public Image smoothScaledBy(double scaleX, double scaleY, int backColor) throws ImageException
ImageException
@Deprecated public Image smoothScaledFixedAspectRatio(int newSize, boolean isHeight, int backColor) throws ImageException
ImageException
@Deprecated public final Image 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 Image getCopy() throws ImageException
ImageException
public Image 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 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