write a pgm file from an short array with magick ++

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
seb_avisto

write a pgm file from an short array with magick ++

Post by seb_avisto »

Hi,

I use magick++ API and I have some problem to write a correct PGM file (gray image) of 14 bits depth from a short array.

The following steps describes what I do:
1- I read a PGM file1 (depth = 14 bits <=> range between 0 and 16535)
2- I write Y pixels into a short array
3- I read this short array through another image (Image2) with Image::read(width, height, "r",ShortPixel, buffer) method
4- then I write image 2 into a file2.pgm

result: a pgm file is created but depth = 16 instead depth = 14 ... so 0 < Y < 2^16 instead 0 < Y< 2^14 ...
I would that file1.pgm = file2.pgm (exactly)
and obviously, the display is different (image 2 is less dark than image 1)

Is someone know the solution ???

My src file:


//============================================================================
// Name : bidon.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>

#include "Magick++/Image.h"

using namespace std;
using namespace Magick;

int main() {

// 1 - I read a PGM file (depth = 14 bits <=> range between 0 and 15535)

cout << "IMAGE 1: " << endl;
Image image("/opt/workspace/generate/Sequence01/Sequence01_image_Y_00001.pgm");

cout << "CARACTERISTIQUES: " << endl;

int imgDepth = image.depth();
cout << "imgDepth = " << imgDepth << endl;

ImageType imgType = image.type();
cout << "imgType = " << imgType << endl;

std::string imgMAgick = image.magick();
cout << "imgMAgick = " << imgMAgick << endl;

ColorspaceType imgCS = image.colorSpace();
cout << "before imgCS = " << imgCS << endl;

ColorspaceType imgCsT = image.colorspaceType();
cout << "imgCsT = " << imgCsT << endl;

cout << "compress type = " << image.compressType() << endl;

//image.colorSpace(GRAYColorspace);
//imgCS = image.colorSpace();
//cout << " after imgCS = " << imgCS << endl;

// 2- I write Y pixels into a short array:

vector<unsigned short> imageBuffer;

imageBuffer.reserve(640*512);

image.write(0,0,image.columns(),image.rows(),"R",ShortPixel,&imageBuffer[0]);

// 3 - I read this short array through an other Image with Image::read(width, height, "r", buffer)

Image image2;

for(size_t i=0; i<imageBuffer.size(); ++i)
{
imageBuffer /= 4;
}

image2.read(image.columns(),image.rows(),"R",ShortPixel,&imageBuffer[0]);

cout << "IMAGE 2: " << endl;

image2.compressType(NoCompression);
image2.colorSpace(RGBColorspace);
image2.magick("PNM");
image2.depth(14);
image2.type(GrayscaleType);

cout << "CARACTERISTIQUES: " << endl;

imgDepth = image2.depth();
cout << "imgDepth = " << imgDepth << endl;

imgType = image2.type();
cout << "imgType = " << imgType << endl;

imgMAgick = image2.magick();
cout << "imgMAgick = " << imgMAgick << endl;

imgCS = image2.colorSpace();
cout << "before imgCS = " << imgCS << endl;

imgCsT = image2.colorspaceType();
cout << "imgCsT = " << imgCsT << endl;

cout << "compress type = " << image2.compressType() << endl;

image2.write("/tmp/exportedImage.pgm");

cout << "After compress type = " << image2.compressType() << endl;


return 0;
}
Post Reply