c - 如何使用 gimp_drawable_set_pixel()?

标签 c gimp

我想在 GIMP 中设置一个像素,但我不知道如何指定像素值。

根据GIMP plugin API它说:

gboolean            gimp_drawable_set_pixel             (gint32 drawable_ID,
                                                         gint x_coord,
                                                         gint y_coord,
                                                         gint num_channels,
                                                         const guint8 *pixel);

如果我例如做:

  const guint8 *pixel;
  pixel = (guint8 *) 0;
  gboolean s;
  s = gimp_drawable_set_pixel (layer, 5, 5, 3, pixel);
  printf ("Was the pixel set?: %i", s);

像素未设置,s为0。

谁能解决这个问题?

满满的爱, 路易丝

这是我使用 Makefile 的代码:

#include <libgimp/gimp.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


static void
query (void)
{
  static GimpParamDef args[] =
  {
    {
      GIMP_PDB_INT32,
      "run-mode",
      "Run mode"
    },
  };

  gimp_install_procedure (
    "plug-in-hello",
    "Hello, world!",
    "Displays \"Hello, world!\" in a dialog",
    "David Neary",
    "Copyright David Neary",
    "2004",
    "_Hello world...",
    NULL,
    GIMP_PLUGIN,
    G_N_ELEMENTS (args), 0,
    args, NULL);

  gimp_plugin_menu_register ("plug-in-hello",
                             "<Image>/Filters/Misc");
}

static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
  static GimpParam  values[1];
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  GimpRunMode       run_mode;
  gint32            image;
  gint32            layer;
  gint32            display;

  /* Setting mandatory output values */
  *nreturn_vals = 1;
  *return_vals  = values;

  values[0].type = GIMP_PDB_STATUS;
  values[0].data.d_status = status;

  /* Getting run_mode - we won't display a dialog if 
   * we are in NONINTERACTIVE mode */
  run_mode = param[0].data.d_int32;

  image = gimp_image_new (800, 600, GIMP_RGB);
  layer = gimp_layer_new (image,
                          "foo",
                          800, 600,
                          GIMP_RGBA_IMAGE,
                          100.0,
                          GIMP_NORMAL_MODE);
  gimp_image_add_layer (image, layer, 0);


  gboolean s;
  guint8 pixel[] = { 0xff, 0, 0, 0xff };
  s = gimp_drawable_set_pixel (layer, 5, 5, 4, (guint8 *)pixel );
  printf ("Was the pixel set?: %i", s);

  display = gimp_display_new (image);

  if (run_mode != GIMP_RUN_NONINTERACTIVE)
    g_message("Hello, world!\n");
}

GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,
  NULL,
  query,
  run
};

MAIN()

生成文件

CC = gcc
CFLAGS = -std=c99 -O2 -Wall \
        -I/usr/include/gimp-2.0 \
        -I/usr/include/glib-2.0 \
        -I/usr/lib64/glib-2.0/include \
        -I/usr/include/gtk-2.0 \
        -I/usr/lib64/gtk-2.0/include \
        -I/usr/include/atk-1.0 \
        -I/usr/include/cairo \
        -I/usr/include/pango-1.0 \
        -I/usr/include/pixman-1 \
        -I/usr/include/freetype2 \
        -I/usr/include/libpng12  

LDFLAGS = -lgimpui-2.0 -lgimpwidgets-2.0 -lgimpmodule-2.0 -lgimp-2.0 \
          -lgimpmath-2.0 -lgimpconfig-2.0 -lgimpcolor-2.0 \
          -lgimpbase-2.0 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 \
          -lgio-2.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lpangocairo-1.0 \
          -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 \
          -lgmodule-2.0 -lglib-2.0  

files = main.o

all: main

main: $(files)
        $(CC) $(CFLAGS) $(files) $(LDFLAGS) -o main

install:
        gimptool-2.0 --install main.c

%.o: %.c Makefile
        $(CC) -c $(CFLAGS) $<

clean:
        rm -f *.o
        rm -f *~
        rm -f main

.PHONY: all clean

更新:代码现在已根据评论进行了更正,因此现在可以使用了。它绘制了一个红色像素。

最佳答案

const guint8 *pixel;
pixel = (guint8 *) 0;

在第一行声明了一个指向guint8 的指针,它没有分配任何内存并且指针指向一些垃圾。在第二行,您将指针指向 NULL。您要么需要 malloc/free 像素缓冲区,要么更好,使用堆栈。

Malloc/免费

guint8 *pixel = malloc(sizeof(guint8) * num_channels);
/*    R             G             B             A */
pixel[0] = 0; pixel[1] = 0; pixel[2] = 0; pixel[3] = 0;
s = gimp_drawable_set_pixel (layer, 5, 5, 3, pixel);
free(pixel);

堆栈:

guint8 pixels[] = {0, 0, 0, 0};
s = gimp_drawable_set_pixel (layer, 5, 5, 3, pixel);

关于c - 如何使用 gimp_drawable_set_pixel()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1964132/

相关文章:

c++ - C++ 中 CPU 使用率降低 : declaring as unsigned int or not?

c - 如何在不堆叠旧数据的情况下接收UDP数据

c - 使用互斥体的 Pthread 同步未正确同步单词

c - 如何从此函数中删除多个返回

c - 在Linux(RedHat)中,C函数malloc_stats()与/proc/<pid>/stat常驻内存大小相比显示不同的值

python - Gimp,脚本-Fu : How can I set a value in the colormap directly

gimp - Script-fu 复制图像后无法缩放图像

python - GIMP 2.10 Python - 如何导入自定义模块?

image - 一种将 PSD 格式转换为 Gimp 可以读取的格式的方法

python - GIMP Python 无论我使用什么格式,我总是得到 "unable to parse color name"