/* * This is a plugin for the GIMP. * This plugin can load Koala Paint images (file format used on Commodore 64) * * Copyright (C) 1999 Maurits Rijk * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Maurits Rijk * * http://home-2.consunet.nl/~cb007736 */ /* * Version Information: * * 0.90 12-Aug-1999 initial release * */ #include #include #include #define KOALA_WIDTH 320 #define KOALA_HEIGHT 200 #define KOALA_COLORS 16 typedef guchar byte; typedef byte FourPixels_t; typedef byte Color_t; typedef struct { byte unused[2]; FourPixels_t bitmap[8000]; Color_t mcolor[25][40]; Color_t color[25][40]; Color_t background; } Koala_t; #define LOW_NIBBLE(x) ((x) & 0x0f) #define HIGH_NIBBLE(x) (((x) >> 4) & 0x0f) static guchar get_color(Koala_t *data, int row, int col, byte index) { if (index == 0) return LOW_NIBBLE(data->background); else if (index == 1) return HIGH_NIBBLE(data->mcolor[row][col]); else if (index == 2) return LOW_NIBBLE(data->mcolor[row][col]); else return LOW_NIBBLE(data->color[row][col]); } static gint32 parse(Koala_t *data) { gint32 image_ID; gint32 layer_ID; GimpPixelRgn pixel_rgn; GimpDrawable *drawable; FourPixels_t *p; guchar *buf, *bufp; int row, col; guchar cmap[] = { 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x88, 0x00, 0x00, 0xaa, 0xff, 0xee, 0xcc, 0x44, 0xcc, 0x00, 0xcc, 0x55, 0x00, 0x00, 0xaa, 0xee, 0xee, 0x77, 0xdd, 0x88, 0x55, 0x66, 0x44, 0x00, 0xff, 0x77, 0x77, 0x33, 0x33, 0x33, 0x77, 0x77, 0x77, 0xaa, 0xff, 0x66, 0x00, 0x88, 0xff, 0xbb, 0xbb, 0xbb }; image_ID = gimp_image_new(KOALA_WIDTH, KOALA_HEIGHT, GIMP_INDEXED); gimp_image_set_cmap(image_ID, cmap, 16); layer_ID = gimp_layer_new(image_ID, "Background", KOALA_WIDTH, KOALA_HEIGHT, GIMP_INDEXED_IMAGE, 100, GIMP_NORMAL_MODE); gimp_image_add_layer(image_ID, layer_ID, 0); drawable = gimp_drawable_get(layer_ID); gimp_pixel_rgn_init(&pixel_rgn, drawable, 0, 0, KOALA_WIDTH, KOALA_HEIGHT, TRUE, FALSE); buf = g_new(guchar, KOALA_WIDTH * KOALA_HEIGHT); bufp = buf + 8; for (row = 0; row < 200; row++) { p = data->bitmap + (row / 8) * 320 + row % 8; for (col = 0; col < 40; col++) { int i; for (i = 0; i < 4; i++) { guchar index = get_color(data, row / 8, col, *p & 3); *--bufp = index; *--bufp = index; *p >>= 2; } bufp += 16; p += 8; } } gimp_pixel_rgn_set_rect(&pixel_rgn, buf, 0, 0, KOALA_WIDTH, KOALA_HEIGHT); g_free(buf); gimp_drawable_flush(drawable); return image_ID; } static gint32 load_koala_paint(char *filename) { gint32 image_ID; FILE *in; Koala_t data; in = fopen(filename, "r"); if (in) { fread(&data, sizeof(data), 1, in); fclose(in); image_ID = parse(&data); gimp_image_set_filename(image_ID, filename); } else { fprintf(stderr, "Koala: unable to open open %s\n", filename); gimp_quit(); } return image_ID; } /*********************************************************************** // run ************************************************************************/ static void run(char *name, int nparams, GimpParam *param, int *nreturn_vals, GimpParam **return_vals) { static GimpParam values[2]; GimpRunModeType run_mode; gint32 image_ID; *nreturn_vals = 1; *return_vals = values; values[0].type = GIMP_PDB_STATUS; values[0].data.d_status = GIMP_PDB_CALLING_ERROR; if (nparams < 2) return; run_mode = param[0].data.d_int32; if (strcmp(name, "file_koala_paint_load") == 0) { values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR; image_ID = load_koala_paint(param[1].data.d_string); if (image_ID != -1) { *nreturn_vals = 2; values[0].data.d_status = GIMP_PDB_SUCCESS; values[1].type = GIMP_PDB_IMAGE; values[1].data.d_image = image_ID; } } } /*********************************************************************** // ************************************************************************/ static void query(void) { static GimpParamDef load_args[] = { {GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive"}, {GIMP_PDB_STRING, "filename", "The name of the file to load"}, {GIMP_PDB_STRING, "raw_filename", "The name of the file to load"}, }; static GimpParamDef load_return_vals[] = { {GIMP_PDB_IMAGE, "image", "Output image"}, }; static int nload_args = sizeof(load_args) / sizeof(load_args[0]); static int nload_return_vals = sizeof(load_return_vals) / sizeof(load_return_vals[0]); gimp_install_procedure("file_koala_paint_load", "loads images of the Koala Paint file format", "This plug-in loads images of the Koala Paint file format.", "Maurits Rijk", "Maurits Rijk", "1999", "/Koala Paint", NULL, GIMP_PLUGIN, nload_args, nload_return_vals, load_args, load_return_vals); gimp_register_load_handler("file_koala_paint_load", "koa", ""); } /*********************************************************************** // ************************************************************************/ GimpPlugInInfo PLUG_IN_INFO = { NULL, /* init_proc */ NULL, /* quit_proc */ query, /* query_proc */ run, /* run_proc */ }; MAIN();