Capturing multipe file with convert output to STDOUT

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
smalik

Capturing multipe file with convert output to STDOUT

Post by smalik »

First some background - I have an image (image.tif) in PTIF format that I got with the command:

Code: Select all

convert image.jpg ptif:image.tif[256x256]
When I run "identify" on the resulting file, I get the following:

Code: Select all

image.tif[0] TIFF 2826x4256 2826x4256+0+0 8-bit DirectClass 7.555MB 0.000u 0:00.001
image.tif[1] TIFF 1413x2128 1413x2128+0+0 8-bit DirectClass 7.555MB 0.016u 0:00.012
image.tif[2] TIFF 706x1064 706x1064+0+0 8-bit DirectClass 7.555MB 0.016u 0:00.022
image.tif[3] TIFF 353x532 353x532+0+0 8-bit DirectClass 7.555MB 0.016u 0:00.031
image.tif[4] TIFF 176x266 176x266+0+0 8-bit DirectClass 7.555MB 0.016u 0:00.043
image.tif[5] TIFF 88x133 88x133+0+0 8-bit DirectClass 7.555MB 0.016u 0:00.051
image.tif[6] TIFF 44x66 44x66+0+0 8-bit DirectClass 7.555MB 0.016u 0:00.059
All good so far, now if I want to get the 256x256 tiles from layer 3, I can do:

Code: Select all

convert image.tif[3] -crop 256x256 tile-%02d.jpg
... which will give me the following tile files:

Code: Select all

-rw-rw-rw-   1 user     group       17587 Mar 31 21:13 tile-03.jpg
-rw-rw-rw-   1 user     group        7631 Mar 31 21:13 tile-04.jpg
-rw-rw-rw-   1 user     group       27854 Mar 31 21:13 tile-05.jpg
-rw-rw-rw-   1 user     group       13476 Mar 31 21:13 tile-06.jpg
-rw-rw-rw-   1 user     group        3975 Mar 31 21:13 tile-07.jpg
-rw-rw-rw-   1 user     group        1810 Mar 31 21:13 tile-08.jpg
Now, my question: I am writing a java program that needs to spawn a process executing the convert command to do exactly what I did in the command above. I will be feeding that process the PTIF data via STDIN and want to capture the multiple tile files by capturing the process' STDOUT. The command I issue in the java process:

Code: Select all

/PATH_TO_CONVERT/convert -[3] -crop 256x256 jpg:-
When I run the command what I get back in STDOUT is just a bunch of bytes. Basically, all of the tiles combined serially into one stream of bytes. I need to know, what delimiter bytes I need to look for to see that one tile has ended and another has started. Any help would be appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Capturing multipe file with convert output to STDOUT

Post by fmw42 »

try this

lena.jpg
Image


convert lena.jpg ptif:lena_pyramid.tif[64x64]
convert lena_pyramid.tif[1] -crop 64x64 +repage lena_pyramid1_%d.jpg

Image
Image
Image
Image


convert lena_pyramid.tif[1] lena_pyramid.jpg
Image

This does the same thing:

convert lena.png ptif:-[64x64] | convert -[1] -crop 64x64 +repage Miff:- | convert - lena_tmp_%d.jpg

or

convert lena.png ptif:-[64x64] | convert -[1] -crop 64x64 +repage Tiff:- | convert - lena_tmp2_%d.jpg

MIFF:- or TIFF:- is needed as it must be a multiframe format
smalik

Re: Capturing multipe file with convert output to STDOUT

Post by smalik »

Thanks for the reply. But what I really want to do is to capture the multiple tiles images' bytes through my java process. My goal is not to end with up those tile files on disk ... my java program needs to store the bytes for each of those tiles in a DB for example - so I can later retreive the tiles individually from there without having to run convert for each tile. In any case, I want to somehow figure out where in the byte stream (STDOUT) one tile ends and where does the next one begin. If it's still not clear what I am trying to do - I can post my java code - it's only few lines. Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Capturing multipe file with convert output to STDOUT

Post by fmw42 »

feel free to post it, but coding is beyond me. however others are more adept at that and may be able to help you more than I.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Capturing multipe file with convert output to STDOUT

Post by snibgo »

I think your question boils down to: "My program receives a stream of concatenated JPEG files. How do I know where one ends and the next begins?"

The proper answer, I think, is that you should parse each one as it arrives.

The informal answer is that each file may finish with any byte sequence, but each should start with the following eleven bytes:

{ff}{d8}{ff}{e0}{00}{10}JFIF{00}

Those bytes might happen to also occur within the compressed image, so they don't guarantee to be the start of a new file.

See:
http://en.wikipedia.org/wiki/JPEG
http://www.w3.org/Graphics/JPEG/itu-t81.pdf
http://www.w3.org/Graphics/JPEG/jfif3.pdf
snibgo's IM pages: im.snibgo.com
smalik

Re: Capturing multipe file with convert output to STDOUT

Post by smalik »

Thanks - that is what I was looking for - let me see if this works in my program. I will report back if I was successful.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Capturing multipe file with convert output to STDOUT

Post by snibgo »

According to my flakey maths, in a series of random bytes I would expect to see those eleven bytes occuring by chance once every 256^10 bytes, which is 10^24 bytes, which is one tera-terabyte.

So I suggest you either

(a) do the messy parsing or

(b) calculate how many files you expect, then look for those eleven bytes, and throw an exception if you don't see the expected number of files.

Counting the files seems a good idea in any case.
snibgo's IM pages: im.snibgo.com
smalik

Re: Capturing multipe file with convert output to STDOUT

Post by smalik »

Thank you to all who tried to help.

Code: Select all

ffd8ffe000104A46494600
... is the HEX string that I needed to look for. Once I started to split out my byte array on these series of bytes, I could separate the image tiles from one another. Now I can generate all the tiles in a layer with one convert call.
Post Reply