wjhk.jupload2.filedata.helper
Class ImageHelper

java.lang.Object
  extended by wjhk.jupload2.filedata.helper.ImageHelper
All Implemented Interfaces:
java.awt.image.ImageObserver

public class ImageHelper
extends java.lang.Object
implements java.awt.image.ImageObserver

Class that contains various utilities about picture, mainly about picture transformation.

Author:
etienne_sf

Field Summary
private  java.lang.Boolean hasToTransformPicture
          hasToTransformPicture indicates whether the picture should be transformed.
private  int maxHeight
          Maximum height for the current transformation
private  int maxWidth
          Maximum width for the current transformation
private  int nbPixelsRead
          Indicates the number of pixels that have been read.
private  int nbPixelsTotal
          Defines the number of pixel for the current picture.
private  PictureFileData pictureFileData
          The PictureFileData that this helper will have to help.
private  int progressBarBaseValue
          The value that has the progress bar when starting to load the picture.
private  int quarterRotation
          Current rotation of the picture: 0 to 3.
private  double scale
          Current scaling factor.
private  int scaledNonRotatedHeight
          Same as scaledNonRotatedWidth
private  int scaledNonRotatedWidth
          Width of picture, after rescaling but without rotation.
private  int scaledRotatedHeight
          Same as scaledRotatedWidth, for the height.
private  int scaledRotatedWidth
          Width of picture, after rescaling and rotation.
(package private)  PictureUploadPolicy uploadPolicy
          The current upload policy must be a PictureUploadPolicy
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
ImageHelper(PictureUploadPolicy uploadPolicy, PictureFileData pictureFileData, int targetMaxWidth, int targetMaxHeight, int quarterRotation)
          Standard constructor.
 
Method Summary
 java.awt.image.BufferedImage getBufferedImage(boolean highquality, java.awt.image.BufferedImage sourceBufferedImage)
          This function resizes the picture, if necessary, according to the maxWidth and maxHeight, given to the ImageHelper constructor.
 boolean hasToTransformPicture()
          This function indicate if the picture has to be modified.
 boolean imageUpdate(java.awt.Image img, int infoflags, int x, int y, int width, int height)
          Implementation of the ImageObserver interface.
private  void initScale()
          Intialization of scale factor, for the current picture state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hasToTransformPicture

private java.lang.Boolean hasToTransformPicture
hasToTransformPicture indicates whether the picture should be transformed. Null if unknown. This can happen (for instance) if no calcul where done (during initialization), or after rotating the picture back to the original orientation.
Note: this attribute is from the class Boolean (and not a simple boolean), to allow null value, meaning unknown.


pictureFileData

private PictureFileData pictureFileData
The PictureFileData that this helper will have to help.


quarterRotation

private int quarterRotation
Current rotation of the picture: 0 to 3.

See Also:
PictureFileData

maxWidth

private int maxWidth
Maximum width for the current transformation


maxHeight

private int maxHeight
Maximum height for the current transformation


nbPixelsTotal

private int nbPixelsTotal
Defines the number of pixel for the current picture. Used to update the progress bar.

See Also:
getBufferedImage(boolean, BufferedImage), imageUpdate(Image, int, int, int, int, int)

nbPixelsRead

private int nbPixelsRead
Indicates the number of pixels that have been read.

See Also:
nbPixelsTotal, imageUpdate(Image, int, int, int, int, int)

scaledNonRotatedWidth

private int scaledNonRotatedWidth
Width of picture, after rescaling but without rotation. It should be scale*originalWidth, but, due to rounding number, it can be transformed to scale*originalWidth-1.

See Also:
initScale()

scaledNonRotatedHeight

private int scaledNonRotatedHeight
Same as scaledNonRotatedWidth


progressBarBaseValue

private int progressBarBaseValue
The value that has the progress bar when starting to load the picture. The imageUpdate(Image, int, int, int, int, int) method will add from 0 to 100, to indicate progress with a percentage value of picture loading.


scale

private double scale
Current scaling factor. If less than 1, means a picture reduction.

See Also:
initScale()

scaledRotatedWidth

private int scaledRotatedWidth
Width of picture, after rescaling and rotation. It should be scale*originalWidth or scale*originalHeight (depending on the rotation). But, due to rounding number, it can be transformed to scale*originalWidth-1 or scale*originalHeight-1.

See Also:
initScale()

scaledRotatedHeight

private int scaledRotatedHeight
Same as scaledRotatedWidth, for the height.


uploadPolicy

PictureUploadPolicy uploadPolicy
The current upload policy must be a PictureUploadPolicy

Constructor Detail

ImageHelper

public ImageHelper(PictureUploadPolicy uploadPolicy,
                   PictureFileData pictureFileData,
                   int targetMaxWidth,
                   int targetMaxHeight,
                   int quarterRotation)
Standard constructor.

Parameters:
uploadPolicy - The current upload policy
pictureFileData - The picture file data to help
targetMaxWidth -
targetMaxHeight -
quarterRotation - Current quarter rotation (from 0 to 3)
Method Detail

initScale

private void initScale()
Intialization of scale factor, for the current picture state. The scale is based on the maximum width and height, the current rotation, and the picture size.


hasToTransformPicture

public boolean hasToTransformPicture()
                              throws JUploadException
This function indicate if the picture has to be modified. For instance : a maximum width, height, a target format...

Returns:
true if the picture must be transformed. false if the file can be directly transmitted.
Throws:
JUploadException - Contains any exception that could be thrown in this method

getBufferedImage

public java.awt.image.BufferedImage getBufferedImage(boolean highquality,
                                                     java.awt.image.BufferedImage sourceBufferedImage)
                                              throws JUploadException
This function resizes the picture, if necessary, according to the maxWidth and maxHeight, given to the ImageHelper constructor.
This function should only be called if isPicture is true. Otherwise, an exception is raised.
Note (Update given by David Gnedt): the highquality will condition the call of getScaledInstance, instead of a basic scale Transformation. The generated picture is of better quality, but this is longer, especially on 'small' CPU. Time samples, with one picture from my canon EOS20D, on a PII 500M:
~3s for the full screen preview with highquality to false, and a quarter rotation. 12s to 20s with highquality to true.
~5s for the first (small) preview of the picture, with both highquality to false or true.

Parameters:
highquality - (added by David Gnedt): if set to true, the BufferedImage.getScaledInstance() is called. This generates better image, but consumes more CPU.
sourceBufferedImage - The image to resize or rotate or both or no tranformation...
Returns:
A BufferedImage which contains the picture according to current parameters (resizing, rotation...), or null if this is not a picture.
Throws:
JUploadException - Contains any exception thrown from within this method.

imageUpdate

public boolean imageUpdate(java.awt.Image img,
                           int infoflags,
                           int x,
                           int y,
                           int width,
                           int height)
Implementation of the ImageObserver interface. Used to follow the drawImage progression, and update the applet progress bar.

Specified by:
imageUpdate in interface java.awt.image.ImageObserver
Parameters:
img -
infoflags -
x -
y -
width -
height -
Returns:
Whether or not the work must go on.