在 ImageMagick 7 的 API C++ 中从 PixelPacket 移植对像素的访问
Porting access to pixels from PixelPacket in API C++ of ImageMagick 7
我尝试使用 ImageMagick-7.0.0-Q16-HDRI 移植代码。
此代码适用于 ImageMagick-6.9.1-Q16:
QString ImageMagick::fromQImage(const QImage &qimage){
if (!qimage.size().isValid()) return "ImageMagick::fromQImage - Empty Qimage";
image = Magick::Image(Magick::Geometry(qimage.width(), qimage.height()), Magick::ColorRGB(0.5, 0.2, 0.3));
double scale = 1 / 255.0;
image.modifyImage();
Magick::PixelPacket *pixels;
Magick::ColorRGB mgc;
for (int y = 0; y < qimage.height(); y++) {
pixels = image.setPixels(0, y, image.columns(), 1);
for (int x = 0; x < qimage.width(); x++) {
QColor pix = qimage.pixel(x, y);
// *pixels++ = Magick::ColorRGB(256 * pix.red(), 256 * pix.green(), 256 * pix.blue());
mgc.red(scale *pix.red());
mgc.green(scale *pix.green());
mgc.blue(scale *pix.blue());
// *pixels++ = Magick::ColorRGB(scale *pix.red(), scale * pix.green(), scale * pix.blue());
pixels[x] = mgc;
}
image.syncPixels();
}
return "";
}
现在我必须使用 http://www.imagemagick.org/script/porting.php 而不是 PixelPacket
:
MagickCore::Quantum *pixels;
在某些情况下:
- 像素[0]-像素的红色通道
- 像素[1]-像素的绿色通道
- 像素[2]-像素的蓝色通道
如何使用 SetPixelRed()... 而不是 pixels[x] = mgc;
MagickCore::SetPixelRed((MagickCore::Image*)&image,scale*pix.red(),pixels+x*3);
MagickCore::SetPixelGreen((MagickCore::Image*)&image,scale*pix.green(),pixels+x*3);
MagickCore::SetPixelBlue((MagickCore::Image*)&image,scale*pix.blue(),pixels+x*3);
但现在是?
找到下一个解决方案:
size_t imageQuantum = MAGICKCORE_QUANTUM_DEPTH;
size_t toFloatQuantum=(pow(2,imageQuantum)-1);
size_t imageChanells=image.channels();
...
pixels[x*imageChanells]=mgc.red()*toFloatQuantum;
pixels[x*imageChanells+1]=mgc.green()*toFloatQuantum;
pixels[x*imageChanells+2]=mgc.blue()*toFloatQuantum;
这是一个非常好的问题,但我认为你从错误的角度来解决这个问题。
如果 Quantum
是从 Magick::Pixels.get
返回的,那么只需将 QColor
直接应用于该值,然后 sync
Magick::Image img(Magick::Geometry(2,2), Magick::Color("white"));
img.modifyImage();
Magick::Pixels view(img);
Magick::Quantum * pixels = view.get(0, 0, 2, 2);
int x,y;
for (y= 0; y < 2 ; y++ ) {
for (x = 0 ; x < 2 ; x++ ) {
// As we starting the pixels as white, we can
// leverage multiplication assignment (*=) operator.
*pixels++ *= pix.red() / 255.0; // Red
*pixels++ *= pix.green()/ 255.0; // Green
*pixels++ *= pix.blue() / 255.0; // Blue
}
}
view.sync()
无需从 MagickCore
调用任何额外的 class(es)
我尝试使用 ImageMagick-7.0.0-Q16-HDRI 移植代码。
此代码适用于 ImageMagick-6.9.1-Q16:
QString ImageMagick::fromQImage(const QImage &qimage){
if (!qimage.size().isValid()) return "ImageMagick::fromQImage - Empty Qimage";
image = Magick::Image(Magick::Geometry(qimage.width(), qimage.height()), Magick::ColorRGB(0.5, 0.2, 0.3));
double scale = 1 / 255.0;
image.modifyImage();
Magick::PixelPacket *pixels;
Magick::ColorRGB mgc;
for (int y = 0; y < qimage.height(); y++) {
pixels = image.setPixels(0, y, image.columns(), 1);
for (int x = 0; x < qimage.width(); x++) {
QColor pix = qimage.pixel(x, y);
// *pixels++ = Magick::ColorRGB(256 * pix.red(), 256 * pix.green(), 256 * pix.blue());
mgc.red(scale *pix.red());
mgc.green(scale *pix.green());
mgc.blue(scale *pix.blue());
// *pixels++ = Magick::ColorRGB(scale *pix.red(), scale * pix.green(), scale * pix.blue());
pixels[x] = mgc;
}
image.syncPixels();
}
return "";
}
现在我必须使用 http://www.imagemagick.org/script/porting.php 而不是 PixelPacket
:
MagickCore::Quantum *pixels;
在某些情况下:
- 像素[0]-像素的红色通道
- 像素[1]-像素的绿色通道
- 像素[2]-像素的蓝色通道
如何使用 SetPixelRed()... 而不是 pixels[x] = mgc;
MagickCore::SetPixelRed((MagickCore::Image*)&image,scale*pix.red(),pixels+x*3);
MagickCore::SetPixelGreen((MagickCore::Image*)&image,scale*pix.green(),pixels+x*3);
MagickCore::SetPixelBlue((MagickCore::Image*)&image,scale*pix.blue(),pixels+x*3);
但现在是?
找到下一个解决方案:
size_t imageQuantum = MAGICKCORE_QUANTUM_DEPTH;
size_t toFloatQuantum=(pow(2,imageQuantum)-1);
size_t imageChanells=image.channels();
...
pixels[x*imageChanells]=mgc.red()*toFloatQuantum;
pixels[x*imageChanells+1]=mgc.green()*toFloatQuantum;
pixels[x*imageChanells+2]=mgc.blue()*toFloatQuantum;
这是一个非常好的问题,但我认为你从错误的角度来解决这个问题。
如果 Quantum
是从 Magick::Pixels.get
返回的,那么只需将 QColor
直接应用于该值,然后 sync
Magick::Image img(Magick::Geometry(2,2), Magick::Color("white"));
img.modifyImage();
Magick::Pixels view(img);
Magick::Quantum * pixels = view.get(0, 0, 2, 2);
int x,y;
for (y= 0; y < 2 ; y++ ) {
for (x = 0 ; x < 2 ; x++ ) {
// As we starting the pixels as white, we can
// leverage multiplication assignment (*=) operator.
*pixels++ *= pix.red() / 255.0; // Red
*pixels++ *= pix.green()/ 255.0; // Green
*pixels++ *= pix.blue() / 255.0; // Blue
}
}
view.sync()
无需从 MagickCore