I'm having trouble with replacing pixel values of a graylevel TIF image and save them as a three-channel RGB image. There are several problems I noticed: First, the image is processed exactly by half (link). Second, if you look (or zoom) close enough, you will also notice the brown 1-px-border on the left side of each object (shouldn't happen). And then there is the histogram mismatch: when I compare the histogram printed by identify and the value of hist, I get:
identify:
4547344: ( 0, 0, 0) #000000 gray(0)
35724: ( 63, 63, 63) #3F3F3F gray(63)
73622: (127,127,127) #7F7F7F gray(127)
69047: (191,191,191) #BFBFBF gray(191)
119867: (255,255,255) #FFFFFF gray(255)
which can't be correct. Why do I have this difference? Another strange thing is that validPixels.size() == 328484 whereas according to identify there should be only 298260 zero-values. I also noticed that the number of colors was reduced, probably as a result of bit depth reduction. If so, then how can I keep the original bit depth?Debugger:
({0x00d88610 {blue=0 green=0 red=0, opacity=0}, 3131961357),
({0x00d88630 {blue=16 green=99 red=69, opacity=0}, 1061109567),
({0x00c310d8 {blue=13 green=240 red=173, opacity=186}, 35724),
({0x00c310f8 {blue=127 green=127 red=127, opacity=127}, 3131961357),
({0x00c31078 {blue=150 green=31 red=1, opacity=0}, 3217014719))
What am I doing wrong. What is the source of all those errors?
Im using IM 6.8.6. Here's the code:
Code: Select all
Magick::InitializeMagick(*argv);
Magick::Image input("Sk_clipped.tif");
std::vector<std::pair<Magick::Color, size_t> > hist; // histogram here
Magick::colorHistogram (&hist, input);
const unsigned char NODATAVALUE = 0;
std::vector<Magick::PixelPacket*> validPixels;
Magick::Geometry inputGeom = input.size ();
// read all pixels to the cache:
//Magick::PixelPacket *pixels = input.getPixels(0, 0, inputGeom.width(), inputGeom.height());
Magick::Pixels view (input);
Magick::PixelPacket *pixels = view.get(0, 0, inputGeom.width(), inputGeom.height());
for (unsigned i = 0; i < inputGeom.width(); ++i) //colomn loop
for (unsigned j = 0; j < inputGeom.height(); ++j) //row loop
{
++pixels;
if (*pixels == Magick::ColorGray(NODATAVALUE))
continue;
validPixels.push_back(pixels);
}
input.modifyImage();
Magick::ColorRGB newColor(0.5, 0.36, 0.2);
for (unsigned i = 0; i < validPixels.size(); ++i)
*(validPixels[i]) = newColor; //Magick::ColorRGB(100, 50, 150);
view.sync();
input.write("test.png");