Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openbis
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sispub
openbis
Commits
bfea1c6e
Commit
bfea1c6e
authored
13 years ago
by
tpylak
Browse files
Options
Downloads
Patches
Plain Diff
LMS-2502 image transformations: fix pngj to produce grayscale images as well
SVN: 22929
parent
d156564b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtil.java
+40
-11
40 additions, 11 deletions
...emsx/cisd/openbis/dss/generic/shared/utils/ImageUtil.java
with
40 additions
and
11 deletions
datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/ImageUtil.java
+
40
−
11
View file @
bfea1c6e
...
...
@@ -24,7 +24,6 @@ import static ch.systemsx.cisd.common.utilities.DataTypeUtil.TIFF_FILE;
import
java.awt.Graphics2D
;
import
java.awt.RenderingHints
;
import
java.awt.image.BufferedImage
;
import
java.awt.image.ColorModel
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.OutputStream
;
...
...
@@ -38,6 +37,7 @@ import java.util.Set;
import
javax.imageio.ImageIO
;
import
org.apache.commons.io.FilenameUtils
;
import
org.apache.log4j.Logger
;
import
ar.com.hjg.pngj.ImageInfo
;
import
ar.com.hjg.pngj.ImageLine
;
...
...
@@ -54,6 +54,8 @@ import ch.systemsx.cisd.common.io.FileBasedContent;
import
ch.systemsx.cisd.common.io.IContent
;
import
ch.systemsx.cisd.common.io.hierarchical_content.HierarchicalNodeBasedContent
;
import
ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContentNode
;
import
ch.systemsx.cisd.common.logging.LogCategory
;
import
ch.systemsx.cisd.common.logging.LogFactory
;
import
ch.systemsx.cisd.common.utilities.DataTypeUtil
;
import
ch.systemsx.cisd.imagereaders.IImageReader
;
import
ch.systemsx.cisd.imagereaders.IReadParams
;
...
...
@@ -68,6 +70,8 @@ import ch.systemsx.cisd.imagereaders.ImageReaderFactory;
*/
public
class
ImageUtil
{
final
static
Logger
operationLog
=
LogFactory
.
getLogger
(
LogCategory
.
OPERATION
,
ImageUtil
.
class
);
private
static
final
Set
<
String
>
FILE_TYPES
=
Collections
.
unmodifiableSet
(
new
HashSet
<
String
>(
Arrays
.
asList
(
"gif"
,
"jpg"
,
"jpeg"
,
"png"
,
"tif"
,
"tiff"
)));
...
...
@@ -330,18 +334,21 @@ public class ImageUtil
final
int
cols
=
image
.
getWidth
();
final
int
rows
=
image
.
getHeight
();
int
bitDepth
=
image
.
getColorModel
().
getComponentSize
(
0
);
ImageInfo
imgInfo
=
new
ImageInfo
(
cols
,
rows
,
bitDepth
,
false
,
false
,
false
);
PngWriter
png
=
new
PngWriter
(
out
,
imgInfo
);
boolean
hasAlpha
=
false
;
// NOTE: it would be nice to support alpha channel
boolean
isGrayscale
=
isGrayscale
(
image
);
ImageInfo
imgInfo
=
new
ImageInfo
(
cols
,
rows
,
bitDepth
,
hasAlpha
,
isGrayscale
,
false
);
PngWriter
png
=
new
PngWriter
(
out
,
imgInfo
);
png
.
setFilterType
(
filterType
==
null
?
PngFilterType
.
FILTER_DEFAULT
:
filterType
);
png
.
setCompLevel
(
compressionLevel
==
-
1
?
6
:
compressionLevel
);
ImageLine
imageLine
=
new
ImageLine
(
imgInfo
);
for
(
int
row
=
0
;
row
<
rows
;
++
row
)
{
for
(
int
col
=
0
;
col
<
cols
;
++
col
)
if
(
isGrayscale
)
{
fillGrayscaleLine
(
image
,
cols
,
isGrayscale
,
imageLine
,
row
);
}
else
{
int
pixel
=
image
.
getRGB
(
col
,
row
);
ImageLineHelper
.
setPixelRGB8
(
imageLine
,
col
,
pixel
);
fillRGBLine
(
image
,
cols
,
isGrayscale
,
imageLine
,
row
);
}
imageLine
.
setRown
(
row
);
png
.
writeRow
(
imageLine
);
...
...
@@ -349,6 +356,25 @@ public class ImageUtil
png
.
end
();
}
private
static
void
fillGrayscaleLine
(
BufferedImage
image
,
final
int
cols
,
boolean
isGrayscale
,
ImageLine
imageLine
,
int
row
)
{
for
(
int
col
=
0
;
col
<
cols
;
++
col
)
{
imageLine
.
scanline
[
col
]
=
image
.
getRaster
().
getSample
(
col
,
row
,
0
);
}
}
private
static
void
fillRGBLine
(
BufferedImage
image
,
final
int
cols
,
boolean
isGrayscale
,
ImageLine
imageLine
,
int
row
)
{
for
(
int
col
=
0
;
col
<
cols
;
++
col
)
{
int
pixel
=
image
.
getRGB
(
col
,
row
);
ImageLineHelper
.
setPixelRGB8
(
imageLine
,
col
,
pixel
);
}
}
/**
* Parses specified string representation of an {@link ImageID}. If the argument is
* <code>null</code> {@link ImageID#NULL} will be returned.
...
...
@@ -460,11 +486,9 @@ public class ImageUtil
*/
public
static
BufferedImage
convertForDisplayIfNecessary
(
BufferedImage
image
)
{
ColorModel
colorModel
=
image
.
getColorModel
();
// is grayscale?
if
(
colorModel
.
getColorSpace
().
getNumComponents
()
==
1
)
if
(
isGrayscale
(
image
))
{
if
(
c
olorModel
.
getPixelSize
()
>
8
)
if
(
image
.
getC
olorModel
()
.
getPixelSize
()
>
8
)
{
Levels
intensityRange
=
IntensityRescaling
.
computeLevels
(
image
,
0
);
return
IntensityRescaling
.
rescaleIntensityLevelTo8Bits
(
image
,
intensityRange
);
...
...
@@ -473,6 +497,11 @@ public class ImageUtil
return
image
;
}
private
static
boolean
isGrayscale
(
BufferedImage
image
)
{
return
image
.
getColorModel
().
getColorSpace
().
getNumComponents
()
==
1
;
}
/**
* Re-scales the image to be the biggest one which fits into a (0,0,maxWidth, maxHeight)
* rectangle. Preserves the aspect ratio. If the rectangle is bigger than the image and
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment