Discussion:
[GM-help] writing a CCITT-G4 compressed TIFF image with Magick++
Fluid Code
2011-03-29 11:30:16 UTC
Permalink
Hi all,

I try to create an image filled in with my own data and save it as a
bi-level CCITT G4 TIFF image.

When I create the image as an [RGB] [Gray-level] [8-bit depth] image, the
TIFF image written
correctly shows a black square at the top left corner as expected.

However when I create it as a [TIFF] [monochrome] [bi-level] [1-bit
depth]image, the image written
in the file looks all white (no black square inside), what am I doing wrong
in my code below?
Am I missing some steps or settings? is the order of the steps the right
one?

Thank you very much for your help

----------------------------------------------------- begin cut here
---------------------------------------------------->
Image * image;
image = new Image(Geometry(100, 100), Color(MaxRGB, MaxRGB, MaxRGB));
image->compressType(MagickLib::Group4Compression); // alt.
Magick::NoCompression
image->type(BilevelType); // alt. GrayscaleType
image->magick("TIFF"); // alt. RGB
image->depth(1); // nb. of bits per pixel
image->monochrome(true);

image->modifyImage();
PixelPacket * packet = image->getPixels(0, 0, image->columns(),
image->rows());
PixelPacket * pixel;
for (unsigned int i=0; i < 20; ++i)
{
PixelPacket * pixelBaseInX = packet + i;

for (unsigned int j=0; j < 20; ++j)
{
pixel = pixelBaseInX + j * image->columns();

pixel->red = 0; // or 1 -- no difference in result image
pixel->green = 0; // or 1 -- no difference in result image
pixel->blue = 0; // or 1 -- no difference in result image
}
}
image->syncPixels();

image->write("image.tif");
----------------------------------------------------- end cut here
---------------------------------------------------->
Bob Friesenhahn
2011-03-30 14:03:12 UTC
Permalink
Hi all, 
I try to create an image filled in with my own data and save it as a bi-level CCITT G4 TIFF image.
When I create the image as an [RGB] [Gray-level] [8-bit depth] image, the TIFF image written  correctly
shows a black square at the top left corner as expected. 
However when I create it as a [TIFF] [monochrome] [bi-level] [1-bit depth] image, the image written 
in the file looks all white (no black square inside), what am I doing wrong in my code below?
Am I missing some steps or settings? is the order of the steps the right one?
Thank you very much for your help
I think that the problem is that this line

  image->type(BilevelType); // alt. GrayscaleType

is immediately changing the image storage class to PseudoClass
(colormap-based). The method you are using to update the image pixels
is for DirectClass. A PseudoClass image would need to be updated by
also updating the per-pixel colormap indexes (IndexPacket) data. It
is also necessary to simultaneously (or at least before passing the
Image to later routines) update the DirectClass representation
(PixelPacket) since it is often assumed that the two representations
are coherent. In this case you would want two colormap entries (for
black & white) and assure that the pixel colormap index refers to the
correct one. After the request for BilevelType this is likely what
you already have.

Try moving the lines which set compressType, type, magick, and
monochrome to just before the write operation since these are normally
intended to influence the format writer.

You can force the image storage class to DirectClass via

image->classType(DirectClass);

after which any existing colormap is ignored.

If you are typically dealing only with bilevel images, then editing
the image via colormap indexes (while assuring that the DirectClass
representation is up to date) is most efficient for the file writer.

Bob
--
Bob Friesenhahn
***@simple.dallas.tx.us, http://www.simplesystems.org/users/bfriesen/
GraphicsMagick Maintainer, http://www.GraphicsMagick.org/
Loading...