Skip to main content

[JAI-IMAGEIO] Need help getting started: tiling a large image

8 replies [Last post]
Anonymous

I have a large number of jpeg files that I need to crop, rescale to a
smaller size, convert to a 256-colour palette, tile into a single image
and save as a PNG or TIFF file. I'm guessing I should probably be using
a mosaic as the images are far too big to fit into memory all at once,
but I'm not clear how best to go about it:

1. Should I crop and rescale all the images before I start?

2. I know there are classes for quantizing colours, but can I use them
across multiple images in a mozaic, to calculate the best overall
palette for all the images?

3. If I create a mosaic, can I save the resulting image without having
to load all the source images into memory at once? I already know they
won't all fit.

Any hints, tips etc gratefully accepted, I've never used JAI before and
it's proving to be a fairly steep learning curve!

Thanks.

--
Alan Burlison
--

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bpb
Offline
Joined: 2004-06-23

Yikes: I just wrote a response and accidentally closed the browser tab thereby losing it!

> I have a large number of jpeg files that I need to
> crop, rescale to a
> smaller size, convert to a 256-colour palette, tile
> into a single image
> and save as a PNG or TIFF file.

Based on your subsequent postings we also know that

- the output could be either PNG or TIFF
- if TIFF it need not be compressed
- the source images are known in advance, have the same format and dimensions, and all need
cropping and scaling identically, then laying out in a rectangular grid.

> I'm guessing I
> should probably be using
> a mosaic as the images are far too big to fit into
> memory all at once,
> but I'm not clear how best to go about it:
>
> 1. Should I crop and rescale all the images before I
> start?

I think something like this might be most effective

read -> crop -> scale -> mosaic -> write -> read -> color quantize -> write

although it requires creating an intermediate full color image. The intermediate image will be smaller than a mosaic of the original sources however as its component images will have been cropped and downscaled prior to assembly.

Note that for all but possibly the upper left source image the "Scale" operation should have non-zero translation parameters so that the source images are correctly positioned within the assemblage.

Note that color quantization could consist in using only the ColorQuantizer operation by itself or in conjunction with the ErrorDiffusion operation. The same lookup table would be used in both cases but in the former case the quantization would use the nearest color algorithm as opposed to the error diffusion algorithm.

> 2. I know there are classes for quantizing colours,
> but can I use them
> across multiple images in a mozaic, to calculate the
> best overall
> palette for all the images?

Unfortunately the ColorQuantizer operation which calculates the palette only accepts a single input. So unless you could select a single representative source you would need to compute the palette based on the mosaic. In the former case you could eliminate the aforementioned intermediate full color image.

> 3. If I create a mosaic, can I save the resulting
> image without having
> to load all the source images into memory at once? I
> already know they
> won't all fit.

You should be able to do this provided the mosaic is tiled and the output is either stripped or tiled both of which are supported by TIFF. I believe that the Java SE PNG writer also requests data line-by-line so it might work but I think that the Java Image I/O PNG write will request the entire source.

The key thing to ensure is that requests on the eventual sink (the final result) only result in requests for part of the source as this propagates back up the operation chain. This is only possible if the writer requests data in quanta and if the in-memory images are not monolithic in memory. So probably your original images may be retained as single-tile images with the mosaic being defined such that its tiles map one-to-one only (cropped and scaled) source image.

> Any hints, tips etc gratefully accepted, I've never
> used JAI before and it's proving to be a fairly steep learning curve!

Yeah, we still desperately some sort of startup guide ... sigh. Meanwhile there is always the forum and the mailing list ...

There is a lot of useful information in this subproject https://jaistuff.dev.java.net/ generously contributed by Rafael Santos.

Brian
Brian

Alan Burlison

Thanks for all the great tips Brian. I'm only doing this as a personal
experiment, so it might be a little while before I get something
written, but I'll let you know how I get on.

--
Alan Burlison
--

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

bpb
Offline
Joined: 2004-06-23

You're welcome, Alan, and yes, please let us know the outcome.

> Thanks for all the great tips Brian. I'm only doing
> this as a personal
> experiment, so it might be a little while before I
> get something
> written, but I'll let you know how I get on.

Brian Burkhalter

On Wed, 9 Jan 2008, Alan Burlison wrote:

> I have a large number of jpeg files that I need to crop, rescale to a smaller
> size, convert to a 256-colour palette, tile into a single image and save as a
> PNG or TIFF file.

OK, so we know you can use TIFF and it doesn't need to be compressed. That
might be helpful since if you use the JAI Image I/O
(https://jai-imageio.dev.java.net) TIFF plugins then you can "replace"
portions of an uncompressed image file which already exists. The layout cannot
change however.

> I'm guessing I should probably be using a mosaic as the
> images are far too big to fit into memory all at once, but I'm not clear how
> best to go about it:
>
> 1. Should I crop and rescale all the images before I start?

Depends on whether the operation chain can be managed to keep the scaling on
the fly.

> 2. I know there are classes for quantizing colours, but can I use them across
> multiple images in a mozaic, to calculate the best overall palette for all
> the images?

Possibly. Does each image have the same dimensions as all other images?

> 3. If I create a mosaic, can I save the resulting image without having to
> load all the source images into memory at once? I already know they won't
> all fit.

Possible. We'll see depending on the answers to the above.

Brian

> Any hints, tips etc gratefully accepted, I've never used JAI before and it's
> proving to be a fairly steep learning curve!
>
> Thanks.
>
> --
> Alan Burlison
> --
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

>^..^< >^..^<

Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

alanbur
Offline
Joined: 2005-11-20

I know all the images in advance, they are all the same format and dimensions, and all need cropping and scaling identically, then laying out in a rectangular grid.

Brian Burkhalter

OK. I will think about it a bit and post later ...

On Wed, 9 Jan 2008, jai-imageio@javadesktop.org wrote:

> I know all the images in advance, they are all the same format and dimensions, and all need cropping and scaling identically, then laying out in a rectangular grid.

>^..^< >^..^<

Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

bpb
Offline
Joined: 2004-06-23

Firstly, do you have a choice of output format or could it be either one?
Secondly, if you can use TIFF, does it have to be compressed?

Brian

alanbur
Offline
Joined: 2005-11-20

I can use TIFF, and it doesn't need to be compressed.