@ -106,7 +106,6 @@ public class ViewImage {
return null ;
return null ;
}
}
// get the image as stream
// get the image as stream
if ( MemoryControl . shortStatus ( ) ) {
if ( MemoryControl . shortStatus ( ) ) {
iconcache . clear ( ) ;
iconcache . clear ( ) ;
@ -131,7 +130,9 @@ public class ViewImage {
if ( resourceb = = null ) {
if ( resourceb = = null ) {
if ( urlString . endsWith ( ".ico" ) ) {
if ( urlString . endsWith ( ".ico" ) ) {
// load default favicon dfltfvcn.ico
// load default favicon dfltfvcn.ico
// Should not do this here : we can be displaying search image result of '.ico' type and do not want to display a default
// Should not do this here : we can be displaying search
// image result of '.ico' type and do not want to display a
// default
if ( defaulticonb = = null )
if ( defaulticonb = = null )
try {
try {
resourceb = FileUtils . read ( new File ( sb . getAppPath ( ) , defaulticon ) ) ;
resourceb = FileUtils . read ( new File ( sb . getAppPath ( ) , defaulticon ) ) ;
@ -148,13 +149,8 @@ public class ViewImage {
}
}
}
}
// gif images are not loaded because of an animated gif bug within
String urlExt = MultiProtocolURL . getFileExtension ( url . getFileName ( ) ) ;
// jvm which sends java into an endless loop with high CPU
if ( ext ! = null & & ext . equalsIgnoreCase ( urlExt ) & & isBrowserRendered ( urlExt ) ) {
if ( ext . equals ( "gif" ) & & "gif" . equals ( MultiProtocolURL . getFileExtension ( url . getFileName ( ) ) ) ) {
return new ByteArrayInputStream ( resourceb ) ;
} else if ( ext . equals ( "svg" ) & & "svg" . equals ( MultiProtocolURL . getFileExtension ( url . getFileName ( ) ) ) ) {
// svg images not supported by awt, but by most browser, deliver
// just content (without crop/scale)
return new ByteArrayInputStream ( resourceb ) ;
return new ByteArrayInputStream ( resourceb ) ;
}
}
@ -166,32 +162,57 @@ public class ViewImage {
}
}
/ * *
/ * *
* Process resourceb byte array to try to produce an Image instance eventually scaled and cropped depending on post parameters
* @param formatName
* @param post request post parameters . Must not be null .
* informal file format name . For example : "png" .
* @param auth true when access rigths are OK .
* @return true when image format is rendered by browser and not by
* @param urlString image source URL . Must not be null .
* ViewImage internals
* @param ext image file extension . May be null .
* /
* @param okToCache true when image can be cached
public static boolean isBrowserRendered ( String formatName ) {
* @param resourceb byte array . Must not be null .
/ *
* gif images are not loaded because of an animated gif bug within jvm
* which sends java into an endless loop with high CPU
* /
/ *
* svg images not supported by jdk , but by most browser , deliver just
* content ( without crop / scale )
* /
return ( "gif" . equalsIgnoreCase ( formatName ) | | "svg" . equalsIgnoreCase ( formatName ) ) ;
}
/ * *
* Process resourceb byte array to try to produce an Image instance
* eventually scaled and cropped depending on post parameters
*
* @param post
* request post parameters . Must not be null .
* @param auth
* true when access rigths are OK .
* @param urlString
* image source URL . Must not be null .
* @param ext
* image file extension . May be null .
* @param okToCache
* true when image can be cached
* @param resourceb
* byte array . Must not be null .
* @return an Image instance when parsing is OK , or null .
* @return an Image instance when parsing is OK , or null .
* /
* /
protected static EncodedImage parseAndScale ( serverObjects post , boolean auth , String urlString , String ext , boolean okToCache , byte [ ] resourceb ) {
protected static EncodedImage parseAndScale ( serverObjects post , boolean auth , String urlString , String ext ,
boolean okToCache , byte [ ] resourceb ) {
EncodedImage encodedImage = null ;
EncodedImage encodedImage = null ;
Image image = ImageParser . parse ( urlString , resourceb ) ;
Image image = ImageParser . parse ( urlString , resourceb ) ;
if ( image ! = null ) {
if ( image ! = null ) {
int width = post . getInt ( "width" , 0 ) ;
int height = post . getInt ( "height" , 0 ) ;
int maxwidth = post . getInt ( "maxwidth" , 0 ) ;
int maxwidth = post . getInt ( "maxwidth" , 0 ) ;
int maxheight = post . getInt ( "maxheight" , 0 ) ;
int maxheight = post . getInt ( "maxheight" , 0 ) ;
final boolean quadratic = post . containsKey ( "quadratic" ) ;
final boolean quadratic = post . containsKey ( "quadratic" ) ;
boolean isStatic = post . getBoolean ( "isStatic" ) ;
boolean isStatic = post . getBoolean ( "isStatic" ) ;
if ( ! auth | | ( width ! = 0 & & height ! = 0 ) | | maxwidth ! = 0 | | maxheight ! = 0 ) {
if ( ! auth | | maxwidth ! = 0 | | maxheight ! = 0 ) {
// find original size
// find original size
final int h = image . getHeight ( null ) ;
int h = image . getHeight ( null ) ;
final int w = image . getWidth ( null ) ;
int w = image . getWidth ( null ) ;
// in case of not-authorized access shrink the image to
// in case of not-authorized access shrink the image to
// prevent
// prevent
@ -203,9 +224,11 @@ public class ViewImage {
// quadratic shape
// quadratic shape
if ( quadratic & & w ! = h ) {
if ( quadratic & & w ! = h ) {
image = makeSquare ( image , h , w ) ;
image = makeSquare ( image , h , w ) ;
h = image . getHeight ( null ) ;
w = image . getWidth ( null ) ;
}
}
Dimension finalDimensions = calculateDimensions ( w , h , width, height , maxDimensions) ;
Dimension finalDimensions = calculateDimensions ( w , h , maxDimensions) ;
if ( w ! = finalDimensions . width & & h ! = finalDimensions . height ) {
if ( w ! = finalDimensions . width & & h ! = finalDimensions . height ) {
image = scale ( finalDimensions . width , finalDimensions . height , image ) ;
image = scale ( finalDimensions . width , finalDimensions . height , image ) ;
@ -230,10 +253,9 @@ public class ViewImage {
*
*
* @return dimensions to render image
* @return dimensions to render image
* /
* /
protected static Dimension calculateDimensions ( final int originWidth , final int originHeight , final int targetWidth ,
protected static Dimension calculateDimensions ( final int originWidth , final int originHeight , final Dimension max ) {
final int targetHeight , final Dimension max ) {
int resultWidth ;
int resultWidth = targetWidth ;
int resultHeight ;
int resultHeight = targetHeight ;
if ( max . width < originWidth | | max . height < originHeight ) {
if ( max . width < originWidth | | max . height < originHeight ) {
// scale image
// scale image
final double hs = ( originWidth < = max . width ) ? 1.0 : ( ( double ) max . width ) / ( ( double ) originWidth ) ;
final double hs = ( originWidth < = max . width ) ? 1.0 : ( ( double ) max . width ) / ( ( double ) originWidth ) ;
@ -335,13 +357,15 @@ public class ViewImage {
if ( w > h ) {
if ( w > h ) {
final BufferedImage dst = new BufferedImage ( h , h , BufferedImage . TYPE_INT_RGB ) ;
final BufferedImage dst = new BufferedImage ( h , h , BufferedImage . TYPE_INT_RGB ) ;
Graphics2D g = dst . createGraphics ( ) ;
Graphics2D g = dst . createGraphics ( ) ;
g . drawImage ( image , 0 , 0 , h - 1 , h - 1 , ( w - h ) / 2 , 0 , h + ( w - h ) / 2 , h - 1 , null ) ;
final int offset = ( w - h ) / 2 ;
g . drawImage ( image , 0 , 0 , h - 1 , h - 1 , offset , 0 , h + offset , h - 1 , null ) ;
g . dispose ( ) ;
g . dispose ( ) ;
image = dst ;
image = dst ;
} else {
} else {
final BufferedImage dst = new BufferedImage ( w , w , BufferedImage . TYPE_INT_RGB ) ;
final BufferedImage dst = new BufferedImage ( w , w , BufferedImage . TYPE_INT_RGB ) ;
Graphics2D g = dst . createGraphics ( ) ;
Graphics2D g = dst . createGraphics ( ) ;
g . drawImage ( image , 0 , 0 , w - 1 , w - 1 , 0 , ( h - w ) / 2 , w - 1 , w + ( h - w ) / 2 , null ) ;
final int offset = ( h - w ) / 2 ;
g . drawImage ( image , 0 , 0 , w - 1 , w - 1 , 0 , offset , w - 1 , w + offset , null ) ;
g . dispose ( ) ;
g . dispose ( ) ;
image = dst ;
image = dst ;
}
}