如何使用ImageMagick C API的MagickGetImageHistogram

时间:2017-03-29 09:26:01

标签: c lua imagemagick ffi luajit

我一直在使用ImageMagick的C API,使用LuaJIT和FFI库以及synopsis of the LWP::UserAgent documentation lua模块。现在我想使用magick方法。因此,当传递参数时,请检查我的下面的代码。

timer.start(1)

所以我确信我的第一个论点是正确的但不确定第二个论点。 它将图像直方图作为PixelWand棒的数组返回。那么如何将其转换为LuaJIT结构?

1 个答案:

答案 0 :(得分:1)

我不确定问题的部分,但预期行为MagickGetImageHistogram如下。

  1. Method将返回一个像素指针数组。
  2. 参数size_t *number_colors将使用数组中的像素数进行更新。
  3. 数组中的每个像素都需要调用方法PixelGetColorCount来检索图像使用的像素总和。
  4. 这是C中的一个简单示例。

    #include <stdio.h>
    #include <wand/MagickWand.h>
    
    int main(int argc, const char * argv[]) {
        // Prototype vars
        MagickWand * wand;
        PixelWand ** histogram;
        size_t histogram_count = 0;
        // Boot environment.
        MagickWandGenesis();
        // Allocate & read image
        wand = NewMagickWand();
        MagickReadImage(wand, "rose:");
        // Get Histogram as array of pixels
        histogram = MagickGetImageHistogram(wand, &histogram_count);
        // Iterate over each pixel & dump info.
        for (int i = 0; i < histogram_count; ++i)
        {
            printf("%s => %zu\n",
                   PixelGetColorAsString(histogram[i]),
                   PixelGetColorCount(histogram[i]));
        }
        // Clean-up
        histogram = DestroyPixelWands(histogram, histogram_count);
        wand = DestroyMagickWand(wand);
        MagickWandTerminus();
        return 0;
    }
    

    此示例将输出预期文本...

    // ...
    srgb(48,45,43) => 1
    srgb(50,45,42) => 2
    srgb(50,44,43) => 5
    srgb(51,45,43) => 1
    // ...
    

    所以我猜你的lua脚本看起来像......

    ***image.lua***
    
    local tlen = ffi.new("size_t[1]")
    local t = lib.MagickGetImageHistogram(self.wand, tlen)
    for i=0,tlen[0] do
        handle_new_pixel(self, t[i], lib.PixelGetColorCount(t[i]))
    end