Problem reading 8 bit per channel image

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
dmaitra

Problem reading 8 bit per channel image

Post by dmaitra »

Hi,

I'm new to MagickWand, and am trying to read an 8-bit per channel image using the code I wrote (see below). While it seems to be able to read 16-bits per channel images perfectly, it appears to convert the 8-bit data into 16-bits. Am I missing something?

I'm using a test image at http://www.astro.lsa.umich.edu/~dmaitra/tt.tif

Here's the convert output

Code: Select all

$ convert tt.tif txt:-
# ImageMagick pixel enumeration: 3,2,255,rgb
0,0: (  0,  0,  0)  #000000  black
1,0: (  1,  0,  0)  #010000  rgb(1,0,0)
2,0: (  2,  0,  0)  #020000  rgb(2,0,0)
0,1: (255,255,  0)  #FFFF00  yellow
1,1: (255,255,255)  #FFFFFF  white
2,1: (  0,  0,  0)  #000000  black
$
and here's the output of my code

Code: Select all

$ ./minimal tt.tif 
wd=3 ht=2 depth=8
0 0 => 0 0 0
1 0 => 257 0 0
2 0 => 514 0 0
0 1 => 65535 65535 0
1 1 => 65535 65535 65535
2 1 => 0 0 0
$
This is the code

Code: Select all

/*
   gcc `MagickWand-config --cflags --cppflags` -o minimal minimal.c    \
   `MagickWand-config --ldflags --libs` -lm -Wall -W -Wextra  -g \
*/

/* Includes and functions */ /*{{{*/
#include <math.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <wand/MagickWand.h>

/* For some reason this is not defined in the .h headers!!! */
void PixelGetMagickColor (const PixelWand *, MagickPixelPacket *);
/*}}}*/

int main(int argc,char **argv)/*{{{*/
{

/* Define ThrowWandException */ /*{{{*/
#define ThrowWandException(wand) \
{ \
  char \
    *description; \
 \
  ExceptionType \
    severity; \
 \
  description=MagickGetException(wand,&severity); \
  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
  description=(char *) MagickRelinquishMemory(description); \
  exit(-1); \
}/*}}}*/

/* Declare variables and stuff */ /*{{{*/
unsigned long wd, ht, x, y;
int dp;
MagickBooleanType status;
MagickPixelPacket pixel;
MagickWand *image_wand;
PixelIterator *iterator;
PixelWand **pixels;
/*}}}*/

if (argc != 2){/*{{{*/
	(void) fprintf(stdout,"Usage: %s image\n",argv[0]);
	exit(0);
}/*}}}*/

/* Read an image, get dimensions */ /*{{{*/
MagickWandGenesis();
image_wand=NewMagickWand();  
status=MagickReadImage(image_wand,argv[1]);
if (status == MagickFalse) ThrowWandException(image_wand);

wd = (unsigned long)MagickGetImageWidth(image_wand);
ht = (unsigned long)MagickGetImageHeight(image_wand);
dp = (int)MagickGetImageDepth(image_wand);
printf("wd=%lu ht=%lu depth=%d\n",wd,ht,dp);
/*}}}*/

/* Get pixel values */ /*{{{*/
iterator=NewPixelIterator(image_wand);
if (iterator == (PixelIterator *)NULL) ThrowWandException(image_wand);

for (y=0; y<ht; y++){
	pixels=PixelGetNextIteratorRow(iterator,&wd);
	if (pixels == (PixelWand **)NULL) break;
	for (x=0; x<wd; x++){
		PixelGetMagickColor(pixels[x],&pixel);
		printf("%lu %lu => %g %g %g\n",x,y,pixel.red,
				pixel.green,pixel.blue);
	}

}/*}}}*/

iterator=DestroyPixelIterator(iterator);

/* Clean up */ /*{{{*/
image_wand=DestroyMagickWand(image_wand);
MagickWandTerminus();
/*}}}*/

return 0;
}/*}}}*/

I'd appreciate any help.
Thanks,
Dipankar
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Problem reading 8 bit per channel image

Post by magick »

Internally, pixels are stored at the quantum depth specified when ImageMagick is compiled. Your version is Q16 or 16-bits per pixel component (0-65535). You can rebuild ImageMagick at 8-bits or divide the 16-bit pixels by 257 or use MagickExportImagePixels() to export an array of 8-bit pixel values.
dmaitra

Re: Problem reading 8 bit per channel image

Post by dmaitra »

Awesome! Thanks magick!

I had suspected something like that but couldn't convince myself that I hadn't made a coding error.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problem reading 8 bit per channel image

Post by anthony »

The only reason the txt: output image was 8 bit and not 16 bit, was that the input image was 8-bit
If you had reset the specific images depth using +depth the txt: image would have been 16 bit (or whatever the compile time quality of your IM library was).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply