Re: [OpenJPEG] Reading and writing comments like Matlab does

120 views
Skip to first unread message

szuk...@arcor.de

unread,
Jun 28, 2014, 7:35:21 AM6/28/14
to open...@googlegroups.com
On Thu, 19 Jun 2014 11:01:47 +0200, Mario Emmenlauer wrote:

>I'm picking up on an older thread here: does OpenJPEG support >comments in the JPEG2000 file, in the same way that Matlab and >the LOCI BioFormats library support it? See below for a file >example:
>
>> I'm trying to read comment fields from a lossless compressed >>JPEG2000 file, created with Matlab (R2011b, in case it >>matters). Matlab supports setting a list of arbitrary comments >>in the file, by specifying a cell array of strings as a >>parameter to imwrite().

Both version 1.5.x and version 2.x.y do write only ONE comment.
Reading comments is skipped.

Look for "comment" in

* j2k_to_image.c
* opj_compress.c

and "read_com" resp. "write_com" in

* j2k.c

There is - until now - now array of comments to write.

The image you are pointing to:

http://data.marssoft.de/b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.jp2

is a 16-Bit GRAY image. All viewers I have used (gqview, iv,
gm animate,animate, gimp-2.8) show an image totally black.

Do you have an 8-Bit image of

'b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.jp2'?

winfried

szuk...@arcor.de

unread,
Jun 29, 2014, 4:15:33 AM6/29/14
to open...@googlegroups.com
On Sat, 28 Jun 2014 13:35:14 +0000, szukw000 wrote:

>The image you are pointing to:
>
>http://data.marssoft.de/b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.jp2
>
>is a 16-Bit GRAY image. All viewers I have use (gqview, iv,
>gm animate,animate, gimp-2.8) show an image totally black.
>
>Do you have an 8-Bit image of >'b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.jp2'?

The 16-Bit GRAY JP2 image can now be converted correctly to PNG: the byte order of pngtoimage() and of imagetopng() was incorrect.

See issue 365.

So from my side an 8-Bit JP2 image is no longer necessary.

winfried

Mario Emmenlauer

unread,
Jun 29, 2014, 5:22:12 AM6/29/14
to open...@googlegroups.com

Hi Winfried,

thanks for your response! Somehow my emails to open...@googlegroups.com
don't always go through, I will try re-sending from my gmail. Below more:

On 28.06.2014 15:35, szuk...@arcor.de wrote:
>> I'm picking up on an older thread here: does OpenJPEG support >comments in the JPEG2000 file, in the same way that Matlab and >the LOCI BioFormats library support it? See below for a file >example:
>>> I'm trying to read comment fields from a lossless compressed >>JPEG2000 file, created with Matlab (R2011b, in case it >>matters). Matlab supports setting a list of arbitrary comments >>in the file, by specifying a cell array of strings as a >>parameter to imwrite().
>
> Both version 1.5.x and version 2.x.y do write only ONE comment.
> Reading comments is skipped.
>
> Look for "comment" in
>
> * j2k_to_image.c
> * opj_compress.c
>
> and "read_com" resp. "write_com" in
>
> * j2k.c
>
> There is - until now - now array of comments to write.

I will check the corresponding code, thanks for the pointers! Did you
see if the comment is just a single string, possibly separated by newline,
or if its indeed an array of comments? I am no expert, I can just say that
in Matlab it "magically" works.


> The image you are pointing to:
>
> http://data.marssoft.de/b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.jp2
>
> is a 16-Bit GRAY image. All viewers I have used (gqview, iv,
> gm animate,animate, gimp-2.8) show an image totally black.
>
> Do you have an 8-Bit image of
>
> 'b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.jp2'?

Its a scientific microscope image, so our images are usually 12bit or
more. But I can create an 8bit version if it helps. However for the
comments, it should not make a difference? PS: a very nice open source
tools for scientific images is Fiji, http://fiji.sc/Fiji that will
read the image (and supports the Matlab-style comments based on my
request :-) ).

All the best,

Mario




--
Mario Emmenlauer BioDataAnalysis Mobil: +49-(0)151-68108489
Balanstrasse 43 mailto: mario.emmenlauer * unibas.ch
D-81669 München http://www.marioemmenlauer.de/

--
A: Yes.
> Q: Are you sure?
>> A: Because it reverses the logical flow of conversation.
>>> Q: Why is top posting annoying in email?

Mario Emmenlauer BioDataAnalysis Mobile: +49-(0)151-68108489
Balanstrasse 43 mailto: mario.emmenlauer * unibas.ch
D-81669 Munich http://www.marioemmenlauer.de/

szuk...@arcor.de

unread,
Jun 30, 2014, 7:35:35 AM6/30/14
to open...@googlegroups.com
On Sun, 29 Jun 2014 11:22:07 +0200, Mario Emmenlauer wrote:

>Did you see if the comment is just a single string, possibly >separated by newline, or if its indeed an array of comments?

It is simply a string. E.g.
openjpeg-1.5.x-branch-r2871/applications/codec/image_to_j2k.c:

main()
{

case 'C': /* add a comment */
{
parameters->cp_comment = (char*)malloc(strlen(opj_optarg) + 1);

if(parameters->cp_comment) {
strcpy(parameters->cp_comment, opj_optarg);
}
}
break;

}

void j2k_setup_encoder(opj_j2k_t *j2k,
opj_cparameters_t *parameters, opj_image_t *image)
{
if(parameters->cp_comment) {
cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
if(cp->comment) {
strcpy(cp->comment, parameters->cp_comment);
}
}

}

opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio,
opj_image_t *image, opj_codestream_info_t *cstr_info)
{

if (j2k->cp->comment != NULL) {
j2k_write_com(j2k);
}

}

static void j2k_write_com(opj_j2k_t *j2k) {
unsigned int i;
int lenp, len;

if(j2k->cp->comment) {
opj_cio_t *cio = j2k->cio;
char *comment = j2k->cp->comment;

cio_write(cio, J2K_MS_COM, 2);
lenp = cio_tell(cio);
cio_skip(cio, 2);
cio_write(cio, 1, 2); /* General use (IS 8859-15:1999
// ===
(Latin) values) */

for (i = 0; i < strlen(comment); i++) {
cio_write(cio, comment[i], 1);
}
len = cio_tell(cio) - lenp;
cio_seek(cio, lenp);
cio_write(cio, len, 2);
cio_seek(cio, lenp + len);

if(j2k->cstr_info)
j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);

}
}

And you can see: the underlined value is '1' only.

For an array one needs perhaps:

struct comment{ short type; char *text; };

short nr_of_comments;
struct comments[];

because we can have at least 3 types:

0: "General use (binary values)"
1: "General use (ISO 8859-1 (latin-1) values)"
2: "Reserved use"

Comments in your file:

[83]marker(0xff64)
com len(53)
R[1](General use (ISO 8859-1 (latin-1) values))
T(Filename=b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.tif)
[138]marker(0xff64)
com len(15)
R[1](General use (ISO 8859-1 (latin-1) values))
T(SiteIndex=1)
(...)

Do you know a viewer that does show the 16-Bit file

b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.jp2

not as a black image?

winfried

Mario Emmenlauer

unread,
Jun 30, 2014, 8:04:39 AM6/30/14
to open...@googlegroups.com

Hi Winfried,

thanks again for the pointers! I'll try to check the code this week.
Below more:

On 30.06.2014 15:35, szuk...@arcor.de wrote:
> Do you know a viewer that does show the 16-Bit file
>
> b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.jp2
>
> not as a black image?

Its a scientific microscope image, converted from MetaXpress TIFF
format to JPEG2000, using Matlab. A very nice open source freeware
viewer and graphics-processor for this kind of scientific images is
Fiji, from http://fiji.sc/Fiji
Fiji will read the image using the BioFormats Java library, a nice
library for proprietary biological image formats. See for details:
http://loci.wisc.edu/software/bio-formats

BioFormats also supports the array of Matlab-style comments in JP2,
based on my request :)

All the best,

Mario




--

szuk...@arcor.de

unread,
Jun 30, 2014, 11:03:50 AM6/30/14
to open...@googlegroups.com
On Mon, 30 Jun 2014 14:04:36 +0200, Mario Emmenlauer wrote:

>Its a scientific microscope image, converted from MetaXpress TIFF
>format to JPEG2000, using Matlab.

I have downloaded 'fiji-linux64-20140602'. Now I know the reason
for the black image:

<Pixels BigEndian="true" Interleaved="true"

BIG_ENDIAN and INTERLEAVED are unusual.

'libopenjpeg' uses LITTLE_ENDIAN and NON-INTERLEAVED.

winfried

szuk...@arcor.de

unread,
Jun 30, 2014, 2:39:26 PM6/30/14
to open...@googlegroups.com
Mario,

attached is a small test program. It writes several
comments; from one string as before.

Do you think this is usable and simple? If yes, one
can open an issue for writing COM markers.

winfried
comment.c.gz

Mario Emmenlauer

unread,
Sep 8, 2014, 6:04:14 AM9/8/14
to open...@googlegroups.com

Dear Winfried,
I'm very sorry that I overlooked your email! Your suggestion looks
very usable and good. One question: is your suggestion based on the
image I sent? Then its in any case perfect, because its based on
Matlab's implementation which I think is based on Kakadu's JPEG2000
library, so it would be compatible with other tools.

All the best,

Mario

szuk...@arcor.de

unread,
Sep 8, 2014, 6:52:48 AM9/8/14
to open...@googlegroups.com
On Mon, 08 Sep 2014 12:03:30 +0200, Mario Emmenlauer wrote:

>very usable and good. One question: is your suggestion based on the
>image I sent?

No. I have used several J2K/JP2 images.

winfried

Mario Emmenlauer

unread,
Apr 18, 2015, 4:09:00 PM4/18/15
to open...@googlegroups.com

Dear Winfried,

On 30.06.2014 22:39, szuk...@arcor.de wrote:
You test program is good. Could you open an issue for writing COM
markers, or help me do that, please? Do you think this could go
into development sometime soon?

Thanks a lot, and cheers,

Mario



--
Mario Emmenlauer BioDataAnalysis Mobil: +49-(0)151-68108489
Balanstrasse 43 mailto: mario.emmenlauer * unibas.ch
D-81669 München http://www.biodataanalysis.de/

Mario Emmenlauer

unread,
May 2, 2018, 8:17:54 AM5/2/18
to open...@googlegroups.com

Dear Winfried and all,

I'm getting back to a really old thread here... support for COM
markers. You have been so kind to help shed some light how Matlab
and Kakadu store comments in JP2. Could openJPEG be able to read
and write such comments too? See below for a demo image.

Thanks a lot for your help!

All the best,

Mario



On 18.04.2015 22:08, Mario Emmenlauer wrote:
>
> Dear Winfried,
>
> On 30.06.2014 22:39, szuk...@arcor.de wrote:
>> attached is a small test program. It writes several
>> comments; from one string as before.
>>
>> Do you think this is usable and simple? If yes, one
>> can open an issue for writing COM markers.
>
> You test program is good. Could you open an issue for writing COM
> markers, or help me do that, please? Do you think this could go
> into development sometime soon?
>
> Thanks a lot, and cheers,
>
> Mario


The correct URL for the file download is:
http://data.marssoft.de/b0bCDME-0007_wB05_s1_z1_t1_cGFP_u001.jp2




--
BioDataAnalysis GmbH, Mario Emmenlauer Tel. Buero: +49-89-74677203
Balanstr. 43 mailto: memmenlauer * biodataanalysis.de
D-81669 München http://www.biodataanalysis.de/
Reply all
Reply to author
Forward
0 new messages