plug-ins: port file-pat to GEGL, as a file plug-in example

This commit is contained in:
Michael Natterer
2012-04-05 20:18:45 +02:00
parent 6c83b0327d
commit d180c894be
3 changed files with 67 additions and 37 deletions

View File

@ -1236,6 +1236,7 @@ file_pat_LDADD = \
$(libgimpcolor) \ $(libgimpcolor) \
$(libgimpbase) \ $(libgimpbase) \
$(GTK_LIBS) \ $(GTK_LIBS) \
$(GEGL_LIBS) \
$(RT_LIBS) \ $(RT_LIBS) \
$(INTLLIBS) \ $(INTLLIBS) \
$(file_pat_RC) $(file_pat_RC)

View File

@ -174,6 +174,8 @@ run (const gchar *name,
*nreturn_vals = 1; *nreturn_vals = 1;
*return_vals = values; *return_vals = values;
gegl_init (NULL, NULL);
values[0].type = GIMP_PDB_STATUS; values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR; values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
@ -319,13 +321,13 @@ load_image (const gchar *filename,
PatternHeader ph; PatternHeader ph;
gchar *name; gchar *name;
gchar *temp; gchar *temp;
guchar *buffer; guchar *buf;
gint32 image_ID; gint32 image_ID;
gint32 layer_ID; gint32 layer_ID;
GimpParasite *parasite; GimpParasite *parasite;
GimpDrawable *drawable; GeglBuffer *buffer;
const Babl *file_format;
gint line; gint line;
GimpPixelRgn pixel_rgn;
GimpImageBaseType base_type; GimpImageBaseType base_type;
GimpImageType image_type; GimpImageType image_type;
@ -390,18 +392,22 @@ load_image (const gchar *filename,
case 1: case 1:
base_type = GIMP_GRAY; base_type = GIMP_GRAY;
image_type = GIMP_GRAY_IMAGE; image_type = GIMP_GRAY_IMAGE;
file_format = babl_format ("Y' u8");
break; break;
case 2: case 2:
base_type = GIMP_GRAY; base_type = GIMP_GRAY;
image_type = GIMP_GRAYA_IMAGE; image_type = GIMP_GRAYA_IMAGE;
file_format = babl_format ("Y'A u8");
break; break;
case 3: case 3:
base_type = GIMP_RGB; base_type = GIMP_RGB;
image_type = GIMP_RGB_IMAGE; image_type = GIMP_RGB_IMAGE;
file_format = babl_format ("R'G'B' u8");
break; break;
case 4: case 4:
base_type = GIMP_RGB; base_type = GIMP_RGB;
image_type = GIMP_RGBA_IMAGE; image_type = GIMP_RGBA_IMAGE;
file_format = babl_format ("R'G'B'A u8");
break; break;
default: default:
g_message ("Unsupported pattern depth: %d\n" g_message ("Unsupported pattern depth: %d\n"
@ -437,30 +443,31 @@ load_image (const gchar *filename,
g_free (name); g_free (name);
drawable = gimp_drawable_get (layer_ID); buffer = gimp_drawable_get_buffer (layer_ID);
gimp_pixel_rgn_init (&pixel_rgn, drawable,
0, 0, drawable->width, drawable->height,
TRUE, FALSE);
/* this can't overflow because ph.width is <= GIMP_MAX_IMAGE_SIZE */ /* this can't overflow because ph.width is <= GIMP_MAX_IMAGE_SIZE */
buffer = g_malloc (ph.width * ph.bytes); buf = g_malloc (ph.width * ph.bytes);
for (line = 0; line < ph.height; line++) for (line = 0; line < ph.height; line++)
{ {
if (read (fd, buffer, ph.width * ph.bytes) != ph.width * ph.bytes) if (read (fd, buf, ph.width * ph.bytes) != ph.width * ph.bytes)
{ {
close (fd); close (fd);
g_free (buffer); g_free (buf);
g_object_unref (buffer);
return -1; return -1;
} }
gimp_pixel_rgn_set_row (&pixel_rgn, buffer, 0, line, ph.width); gegl_buffer_set (buffer, GEGL_RECTANGLE (0, line, ph.width, 1), 0,
file_format, buf, GEGL_AUTO_ROWSTRIDE);
gimp_progress_update ((gdouble) line / (gdouble) ph.height); gimp_progress_update ((gdouble) line / (gdouble) ph.height);
} }
gimp_progress_update (1.0); gimp_progress_update (1.0);
gimp_drawable_flush (drawable); g_free (buf);
g_object_unref (buffer);
return image_ID; return image_ID;
} }
@ -473,10 +480,13 @@ save_image (const gchar *filename,
{ {
gint fd; gint fd;
PatternHeader ph; PatternHeader ph;
guchar *buffer; GeglBuffer *buffer;
GimpDrawable *drawable; const Babl *file_format;
guchar *buf;
gint width;
gint height;
gint line_size;
gint line; gint line;
GimpPixelRgn pixel_rgn;
fd = g_open (filename, O_CREAT | O_TRUNC | O_WRONLY | _O_BINARY, 0666); fd = g_open (filename, O_CREAT | O_TRUNC | O_WRONLY | _O_BINARY, 0666);
@ -488,18 +498,40 @@ save_image (const gchar *filename,
return FALSE; return FALSE;
} }
switch (gimp_drawable_type (drawable_ID))
{
case GIMP_GRAY_IMAGE:
file_format = babl_format ("Y' u8");
break;
case GIMP_GRAYA_IMAGE:
file_format = babl_format ("Y'A u8");
break;
case GIMP_RGB_IMAGE:
file_format = babl_format ("R'G'B' u8");
break;
case GIMP_RGBA_IMAGE:
file_format = babl_format ("R'G'B'A u8");
break;
default:
g_message ("Unsupported image type: %d\n"
"GIMP Patterns must be GRAY or RGB",
gimp_drawable_type (drawable_ID));
return FALSE;
}
gimp_progress_init_printf (_("Saving '%s'"), gimp_progress_init_printf (_("Saving '%s'"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
drawable = gimp_drawable_get (drawable_ID); buffer = gimp_drawable_get_buffer (drawable_ID);
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width,
drawable->height, FALSE, FALSE); width = gegl_buffer_get_width (buffer);
height = gegl_buffer_get_height (buffer);
ph.header_size = g_htonl (sizeof (PatternHeader) + strlen (description) + 1); ph.header_size = g_htonl (sizeof (PatternHeader) + strlen (description) + 1);
ph.version = g_htonl (1); ph.version = g_htonl (1);
ph.width = g_htonl (drawable->width); ph.width = g_htonl (width);
ph.height = g_htonl (drawable->height); ph.height = g_htonl (height);
ph.bytes = g_htonl (drawable->bpp); ph.bytes = g_htonl (babl_format_get_bytes_per_pixel (file_format));
ph.magic_number = g_htonl (GPATTERN_MAGIC); ph.magic_number = g_htonl (GPATTERN_MAGIC);
if (write (fd, &ph, sizeof (PatternHeader)) != sizeof (PatternHeader)) if (write (fd, &ph, sizeof (PatternHeader)) != sizeof (PatternHeader))
@ -514,29 +546,26 @@ save_image (const gchar *filename,
return FALSE; return FALSE;
} }
line_size = width * babl_format_get_bytes_per_pixel (file_format);
/* this can't overflow because drawable->width is <= GIMP_MAX_IMAGE_SIZE */ /* this can't overflow because drawable->width is <= GIMP_MAX_IMAGE_SIZE */
buffer = g_malloc (drawable->width * drawable->bpp); buf = g_alloca (line_size);
if (buffer == NULL)
for (line = 0; line < height; line++)
{
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, line, width, 1), 1.0,
file_format, buf,
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
if (write (fd, buf, line_size) != line_size)
{ {
close (fd); close (fd);
return FALSE; return FALSE;
} }
for (line = 0; line < drawable->height; line++) gimp_progress_update ((gdouble) line / (gdouble) ph.height);
{
gimp_pixel_rgn_get_row (&pixel_rgn, buffer, 0, line, drawable->width);
if (write (fd, buffer, drawable->width * drawable->bpp) !=
drawable->width * drawable->bpp)
{
close (fd);
return FALSE;
} }
gimp_progress_update ((gdouble) line / (gdouble) drawable->height);
}
g_free (buffer);
close (fd); close (fd);
return TRUE; return TRUE;

View File

@ -59,7 +59,7 @@
'file-html-table' => { ui => 1 }, 'file-html-table' => { ui => 1 },
'file-jp2-load' => { optional => 1, libs => 'JP2_LIBS' }, 'file-jp2-load' => { optional => 1, libs => 'JP2_LIBS' },
'file-mng' => { ui => 1, optional => 1, libs => 'MNG_LIBS', cflags => 'MNG_CFLAGS' }, 'file-mng' => { ui => 1, optional => 1, libs => 'MNG_LIBS', cflags => 'MNG_CFLAGS' },
'file-pat' => { ui => 1 }, 'file-pat' => { ui => 1, gegl => 1 },
'file-pcx' => { ui => 1 }, 'file-pcx' => { ui => 1 },
'file-pix' => { ui => 1 }, 'file-pix' => { ui => 1 },
'file-png' => { ui => 1, optional => 1, libs => 'PNG_LIBS', cflags => 'PNG_CFLAGS' }, 'file-png' => { ui => 1, optional => 1, libs => 'PNG_LIBS', cflags => 'PNG_CFLAGS' },