gimp/plug-ins/common/header.c

259 lines
7.1 KiB
C
Raw Normal View History

1997-11-25 06:05:25 +08:00
#include <stdlib.h>
New file. * plug-ins/makefile.msc: New file. * plug-ins/*/*.c (Well, not really all files, but many): Portability fixes. Include config.h, and guard inclusion of POSIX and Unix headers like <unistd.h>, <dirent.h> and <sys/time.h>. Include <string.h> if functions from it are used. Use g_ntohl() and g_htonl() insteead of ntohl() and htonl(), thus no need to include <netinet/in.h>. Include <io.h> on Win32 when using open/read/write (which actually are defined as _open/_read/_write by glib.h). Define S_* macros if necessary on Win32. Define rint() and M_PI if necessary (these definitions should be factored out somewhere, no sense repeating them in lots of files). Open files in binary mode when needed. * plug-ins/FractalExplorer/FractalExplorer.c: Use g_malloc()/g_free(). Use g_strdup_printf(). * plug-ins/dbbrowser/dbbrowser.c: No need to include <X11/Xlib.h>. * plug-ins/destripe/destripe.c: Guard use of SIGBUS. * plug-ins/{flame/flame,hrz/hrz,pnm/pnm}.c: No need to check for NULL returns from g_malloc() and g_new() as they abort on failure. Use g_strdup_printf(). * plug-ins/gz/gz.c: Win32 version of running the subprocess. * plug-ins/hrz/hrz.c: Win32 version. Use more generic tests for non-Unix (OS/2 and Win32), for instance HAVE_MMAP. * plug-ins/script-fu/interp_slib.c: Win32 version of myruntime(). * plug-ins/script-fu/interp_sliba.c: Handle \\ (escaped backslahsh). * plug-ins/script-fu/script-fu-console.c: Bypass on Win32. * plug-ins/script-fu/script-fu-scripts.c: Portability fixes. Use g_strdup_printf() instead of separate malloc() and sprintf(). Use g_strescape() for strings being passed to Scheme. Some Win32-only stuff.
1999-05-29 09:28:24 +08:00
#include <string.h>
1997-11-25 06:05:25 +08:00
#include <stdio.h>
1999-10-10 03:06:14 +08:00
#include "gtk/gtk.h"
1997-11-25 06:05:25 +08:00
#include "libgimp/gimp.h"
1999-10-10 03:06:14 +08:00
#include "libgimp/gimpui.h"
1997-11-25 06:05:25 +08:00
/* Declare some local functions.
*/
static void query (void);
static void run (char *name,
int nparams,
GParam *param,
int *nreturn_vals,
GParam **return_vals);
1999-10-10 03:06:14 +08:00
static void init_gtk (void);
1997-11-25 06:05:25 +08:00
static int save_image (char *filename,
gint32 image_ID,
gint32 drawable_ID);
GPlugInInfo PLUG_IN_INFO =
{
1999-10-10 03:06:14 +08:00
NULL, /* init_proc */
NULL, /* quit_proc */
1997-11-25 06:05:25 +08:00
query, /* query_proc */
1999-10-10 03:06:14 +08:00
run, /* run_proc */
1997-11-25 06:05:25 +08:00
};
MAIN ()
static void
query ()
{
static GParamDef save_args[] =
{
{ PARAM_INT32, "run_mode", "Interactive, non-interactive" },
{ PARAM_IMAGE, "image", "Input image" },
{ PARAM_DRAWABLE, "drawable", "Drawable to save" },
{ PARAM_STRING, "filename", "The name of the file to save the image in" },
{ PARAM_STRING, "raw_filename", "The name of the file to save the image in" },
};
static int nsave_args = sizeof (save_args) / sizeof (save_args[0]);
gimp_install_procedure ("file_header_save",
"saves files as C unsigned character array",
"FIXME: write help",
"Spencer Kimball & Peter Mattis",
"Spencer Kimball & Peter Mattis",
"1997",
"<Save>/Header",
1999-10-10 03:06:14 +08:00
"INDEXED, RGB",
1997-11-25 06:05:25 +08:00
PROC_PLUG_IN,
nsave_args, 0,
save_args, NULL);
gimp_register_save_handler ("file_header_save", "h", "");
}
static void
run (char *name,
int nparams,
GParam *param,
int *nreturn_vals,
GParam **return_vals)
{
static GParam values[2];
GRunModeType run_mode;
1999-10-10 03:06:14 +08:00
gint32 image_ID;
gint32 drawable_ID;
GimpExportReturnType export = EXPORT_CANCEL;
1997-11-25 06:05:25 +08:00
run_mode = param[0].data.d_int32;
*nreturn_vals = 1;
*return_vals = values;
values[0].type = PARAM_STATUS;
values[0].data.d_status = STATUS_CALLING_ERROR;
if (strcmp (name, "file_header_save") == 0)
{
*nreturn_vals = 1;
1999-10-10 03:06:14 +08:00
image_ID = param[1].data.d_int32;
drawable_ID = param[2].data.d_int32;
/* eventually export the image */
switch (run_mode)
{
case RUN_INTERACTIVE:
case RUN_WITH_LAST_VALS:
init_gtk ();
export = gimp_export_image (&image_ID, &drawable_ID, "Header",
(CAN_HANDLE_RGB | CAN_HANDLE_INDEXED));
if (export == EXPORT_CANCEL)
{
values[0].data.d_status = STATUS_EXECUTION_ERROR;
return;
}
break;
default:
break;
}
if (save_image (param[3].data.d_string, image_ID, drawable_ID))
1997-11-25 06:05:25 +08:00
values[0].data.d_status = STATUS_SUCCESS;
else
values[0].data.d_status = STATUS_EXECUTION_ERROR;
1999-10-10 03:06:14 +08:00
if (export == EXPORT_EXPORT)
gimp_image_delete (image_ID);
1997-11-25 06:05:25 +08:00
}
}
1999-10-10 03:06:14 +08:00
static void
init_gtk ()
{
gchar **argv;
gint argc;
argc = 1;
argv = g_new (gchar *, 1);
argv[0] = g_strdup ("header");
gtk_init (&argc, &argv);
gtk_rc_parse (gimp_gtkrc ());
}
1997-11-25 06:05:25 +08:00
static int
save_image (char *filename,
gint32 image_ID,
gint32 drawable_ID)
{
GPixelRgn pixel_rgn;
GDrawable *drawable;
GDrawableType drawable_type;
FILE *fp;
int x, y, b, c;
gchar *backslash = "\\\\";
gchar *quote = "\\\"";
gchar *newline = "\"\n\t\"";
gchar buf[4];
1999-10-10 03:06:14 +08:00
guchar *d = NULL;
1997-11-25 06:05:25 +08:00
guchar *data;
unsigned char *cmap;
int colors;
1997-11-25 06:05:25 +08:00
if ((fp = fopen (filename, "w")) == NULL)
return FALSE;
drawable = gimp_drawable_get (drawable_ID);
drawable_type = gimp_drawable_type (drawable_ID);
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width, drawable->height, FALSE, FALSE);
fprintf (fp, "/* GIMP header image file format (%s): %s */\n\n",
RGB_IMAGE == drawable_type ? "RGB" : "INDEXED", filename);
1997-11-25 06:05:25 +08:00
fprintf (fp, "static unsigned int width = %d;\n", drawable->width);
fprintf (fp, "static unsigned int height = %d;\n\n", drawable->height);
fprintf (fp, "/* Call this macro repeatedly. After each use, the pixel data can be extracted */\n\n");
switch (drawable_type)
{
case RGB_IMAGE:
fprintf (fp, "#define HEADER_PIXEL(data,pixel) {\\\n pixel[0] = (((data[0] - 33) << 2) | ((data[1] - 33) >> 4)); \\\n pixel[1] = ((((data[1] - 33) & 0xF) << 4) | ((data[2] - 33) >> 2)); \\\n pixel[2] = ((((data[2] - 33) & 0x3) << 6) | ((data[3] - 33))); \\\n data += 4;\n}\n");
fprintf (fp, "static char *header_data =\n\t\"");
1997-11-25 06:05:25 +08:00
data = g_new (guchar, drawable->width * drawable->bpp);
1997-11-25 06:05:25 +08:00
c = 0;
for (y = 0; y < drawable->height; y++)
1997-11-25 06:05:25 +08:00
{
gimp_pixel_rgn_get_row (&pixel_rgn, data, 0, y, drawable->width);
for (x = 0; x < drawable->width; x++)
1997-11-25 06:05:25 +08:00
{
d = data + x * drawable->bpp;
buf[0] = ((d[0] >> 2) & 0x3F) + 33;
buf[1] = ((((d[0] & 0x3) << 4) | (d[1] >> 4)) & 0x3F) + 33;
buf[2] = ((((d[1] & 0xF) << 2) | (d[2] >> 6)) & 0x3F) + 33;
buf[3] = (d[2] & 0x3F) + 33;
for (b = 0; b < 4; b++)
if (buf[b] == '"')
fwrite (quote, 1, 2, fp);
else if (buf[b] == '\\')
fwrite (backslash, 1, 2, fp);
else
fwrite (buf + b, 1, 1, fp);
c++;
if (c >= 16)
{
fwrite (newline, 1, 4, fp);
c = 0;
}
1997-11-25 06:05:25 +08:00
}
}
fprintf (fp, "\";\n");
break;
case INDEXED_IMAGE:
fprintf (fp, "#define HEADER_PIXEL(data,pixel) {\\\n pixel[0] = header_data_cmap[(unsigned char)data[0]][0]; \\\n pixel[1] = header_data_cmap[(unsigned char)data[0]][1]; \\\n pixel[2] = header_data_cmap[(unsigned char)data[0]][2]; \\\n data ++; }\n\n");
/* save colormap */
cmap = gimp_image_get_cmap (image_ID, &colors);
fprintf (fp, "static char header_data_cmap[256][3] = {");
fprintf(fp, "\n\t{%3d,%3d,%3d}", (int)cmap[0], (int)cmap[1], (int)cmap[2]);
for (c = 1; c < colors; c++)
fprintf(fp, ",\n\t{%3d,%3d,%3d}", (int)cmap[3*c], (int)cmap[3*c+1], (int)cmap[3*c+2]);
/* fill the rest */
for ( ; c < 256; c++)
fprintf(fp, ",\n\t{255,255,255}");
/* close bracket */
fprintf(fp, "\n\t};\n");
g_free(cmap);
/* save image */
fprintf (fp, "static char header_data[] = {\n\t");
data = g_new (guchar, drawable->width * drawable->bpp);
c = 0;
for (y = 0; y < drawable->height; y++)
{
gimp_pixel_rgn_get_row (&pixel_rgn, data, 0, y, drawable->width);
for (x = 0; x < drawable->width-1; x++)
{
d = data + x * drawable->bpp;
fprintf(fp, "%d,", (int)d[0]);
c++;
if (c >= 16)
{
fprintf (fp, "\n\t");
c = 0;
}
}
if (y != drawable->height - 1)
fprintf(fp, "%d,\n\t", (int)d[1]);
else
fprintf(fp, "%d\n\t", (int)d[1]);
c = 0; /* reset line counter */
}
fprintf (fp, "};\n");
break;
default:
g_warning ("unhandled drawable type (%d)", drawable_type);
return FALSE;
} /* switch (drawable_type) */
1997-11-25 06:05:25 +08:00
fclose (fp);
g_free (data);
gimp_drawable_detach (drawable);
return TRUE;
}