2004-07-17 19:33:03 +08:00
|
|
|
/* bmpwrite.c Writes Bitmap files. Even RLE encoded ones. */
|
|
|
|
/* (Windows (TM) doesn't read all of those, but who */
|
|
|
|
/* cares? ;-) */
|
1998-04-07 11:41:22 +08:00
|
|
|
/* I changed a few things over the time, so perhaps */
|
|
|
|
/* it dos now, but now there's no Windows left on */
|
|
|
|
/* my computer... */
|
|
|
|
|
2004-07-17 19:33:03 +08:00
|
|
|
/* Alexander.Schulz@stud.uni-karlsruhe.de */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
/*
|
1999-12-19 20:20:38 +08:00
|
|
|
* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1999-05-29 09:28:24 +08:00
|
|
|
#include "config.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-06-13 22:37:00 +08:00
|
|
|
#include <errno.h>
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <string.h>
|
2005-03-04 21:23:32 +08:00
|
|
|
|
|
|
|
#include <glib/gstdio.h>
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <libgimp/gimp.h>
|
2000-02-13 01:39:21 +08:00
|
|
|
#include <libgimp/gimpui.h>
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include "bmp.h"
|
2000-02-13 01:39:21 +08:00
|
|
|
|
1999-05-30 00:35:47 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-03-04 23:12:29 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
static gint cur_progress = 0;
|
|
|
|
static gint max_progress = 0;
|
|
|
|
static gint encoded = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2003-11-25 21:49:16 +08:00
|
|
|
static void WriteImage (FILE *f,
|
|
|
|
guchar *src,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gint encoded,
|
|
|
|
gint channels,
|
|
|
|
gint bpp,
|
|
|
|
gint spzeile,
|
|
|
|
gint MapSize);
|
|
|
|
static gboolean save_dialog (void);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
static void
|
|
|
|
FromL (gint32 wert,
|
2003-01-05 23:48:32 +08:00
|
|
|
guchar *bopuffer)
|
|
|
|
{
|
|
|
|
bopuffer[0] = (wert & 0x000000ff)>>0x00;
|
|
|
|
bopuffer[1] = (wert & 0x0000ff00)>>0x08;
|
|
|
|
bopuffer[2] = (wert & 0x00ff0000)>>0x10;
|
|
|
|
bopuffer[3] = (wert & 0xff000000)>>0x18;
|
|
|
|
}
|
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
static void
|
|
|
|
FromS (gint16 wert,
|
2003-01-05 23:48:32 +08:00
|
|
|
guchar *bopuffer)
|
|
|
|
{
|
|
|
|
bopuffer[0] = (wert & 0x00ff)>>0x00;
|
|
|
|
bopuffer[1] = (wert & 0xff00)>>0x08;
|
|
|
|
}
|
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
static void
|
|
|
|
WriteColorMap (FILE *f,
|
2004-07-17 19:33:03 +08:00
|
|
|
gint red[MAXCOLORS],
|
|
|
|
gint green[MAXCOLORS],
|
|
|
|
gint blue[MAXCOLORS],
|
|
|
|
gint size)
|
2003-01-05 23:48:32 +08:00
|
|
|
{
|
|
|
|
gchar trgb[4];
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
size /= 4;
|
|
|
|
trgb[3] = 0;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
trgb[0] = (guchar) blue[i];
|
|
|
|
trgb[1] = (guchar) green[i];
|
|
|
|
trgb[2] = (guchar) red[i];
|
|
|
|
Write (f, trgb, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-22 11:27:14 +08:00
|
|
|
GimpPDBStatusType
|
2003-07-02 23:23:47 +08:00
|
|
|
WriteBMP (const gchar *filename,
|
2004-07-17 19:33:03 +08:00
|
|
|
gint32 image,
|
|
|
|
gint32 drawable_ID)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2003-11-25 21:49:16 +08:00
|
|
|
FILE *outfile;
|
|
|
|
gint Red[MAXCOLORS];
|
|
|
|
gint Green[MAXCOLORS];
|
|
|
|
gint Blue[MAXCOLORS];
|
|
|
|
guchar *cmap;
|
|
|
|
gint rows, cols, Spcols, channels, MapSize, SpZeile;
|
|
|
|
glong BitsPerPixel;
|
|
|
|
gint colors;
|
|
|
|
guchar *pixels;
|
|
|
|
GimpPixelRgn pixel_rgn;
|
2000-08-22 11:27:14 +08:00
|
|
|
GimpDrawable *drawable;
|
|
|
|
GimpImageType drawable_type;
|
2003-11-25 21:49:16 +08:00
|
|
|
guchar puffer[50];
|
|
|
|
gint i;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-25 21:49:16 +08:00
|
|
|
drawable = gimp_drawable_get (drawable_ID);
|
|
|
|
drawable_type = gimp_drawable_type (drawable_ID);
|
|
|
|
|
2000-02-13 01:39:21 +08:00
|
|
|
gimp_pixel_rgn_init (&pixel_rgn, drawable,
|
2004-07-17 19:33:03 +08:00
|
|
|
0, 0, drawable->width, drawable->height, FALSE, FALSE);
|
2003-11-25 21:49:16 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
switch (drawable_type)
|
|
|
|
{
|
2005-06-08 00:55:21 +08:00
|
|
|
case GIMP_RGBA_IMAGE:
|
|
|
|
colors = 0;
|
|
|
|
BitsPerPixel = 32;
|
|
|
|
MapSize = 0;
|
|
|
|
channels = 4;
|
|
|
|
break;
|
|
|
|
|
2000-08-22 11:27:14 +08:00
|
|
|
case GIMP_RGB_IMAGE:
|
2000-02-13 01:39:21 +08:00
|
|
|
colors = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
BitsPerPixel = 24;
|
2000-02-13 01:39:21 +08:00
|
|
|
MapSize = 0;
|
|
|
|
channels = 3;
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
2005-06-08 00:55:21 +08:00
|
|
|
|
2000-08-22 11:27:14 +08:00
|
|
|
case GIMP_GRAY_IMAGE:
|
2000-02-13 01:39:21 +08:00
|
|
|
colors = 256;
|
|
|
|
BitsPerPixel = 8;
|
|
|
|
MapSize = 1024;
|
|
|
|
channels = 1;
|
1997-11-25 06:05:25 +08:00
|
|
|
for (i = 0; i < colors; i++)
|
2004-07-17 19:33:03 +08:00
|
|
|
{
|
|
|
|
Red[i] = i;
|
|
|
|
Green[i] = i;
|
|
|
|
Blue[i] = i;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
2005-06-08 00:55:21 +08:00
|
|
|
|
|
|
|
case GIMP_GRAYA_IMAGE:
|
|
|
|
g_message (_("Cannot operate on grayscale images with alpha channel."));
|
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
|
2000-08-22 11:27:14 +08:00
|
|
|
case GIMP_INDEXED_IMAGE:
|
2004-11-02 20:00:25 +08:00
|
|
|
cmap = gimp_image_get_colormap (image, &colors);
|
2000-02-13 01:39:21 +08:00
|
|
|
MapSize = 4 * colors;
|
1997-11-25 06:05:25 +08:00
|
|
|
channels = 1;
|
2000-02-13 01:39:21 +08:00
|
|
|
|
|
|
|
if (colors > 16)
|
2004-07-17 19:33:03 +08:00
|
|
|
BitsPerPixel = 8;
|
2000-02-13 01:39:21 +08:00
|
|
|
else if (colors > 2)
|
2004-07-17 19:33:03 +08:00
|
|
|
BitsPerPixel = 4;
|
2000-02-13 01:39:21 +08:00
|
|
|
else
|
2004-07-17 19:33:03 +08:00
|
|
|
BitsPerPixel = 1;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
for (i = 0; i < colors; i++)
|
2004-07-17 19:33:03 +08:00
|
|
|
{
|
|
|
|
Red[i] = *cmap++;
|
|
|
|
Green[i] = *cmap++;
|
|
|
|
Blue[i] = *cmap++;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
2005-06-08 00:55:21 +08:00
|
|
|
|
|
|
|
case GIMP_INDEXEDA_IMAGE:
|
|
|
|
g_message (_("Cannot operate on indexed images with alpha channel."));
|
2000-08-22 11:27:14 +08:00
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
2005-06-08 00:55:21 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perhaps someone wants RLE encoded Bitmaps */
|
2000-02-13 01:39:21 +08:00
|
|
|
encoded = 0;
|
|
|
|
if ((BitsPerPixel == 8 || BitsPerPixel == 4) && interactive_bmp)
|
|
|
|
{
|
|
|
|
if (! save_dialog ())
|
2004-07-17 19:33:03 +08:00
|
|
|
return GIMP_PDB_CANCEL;
|
2000-02-13 01:39:21 +08:00
|
|
|
}
|
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
/* Let's take some file */
|
2005-03-04 21:23:32 +08:00
|
|
|
outfile = g_fopen (filename, "wb");
|
1997-11-25 06:05:25 +08:00
|
|
|
if (!outfile)
|
|
|
|
{
|
2003-11-15 21:53:33 +08:00
|
|
|
g_message (_("Could not open '%s' for writing: %s"),
|
2004-01-19 11:06:04 +08:00
|
|
|
gimp_filename_to_utf8 (filename), g_strerror (errno));
|
2000-08-22 11:27:14 +08:00
|
|
|
return GIMP_PDB_EXECUTION_ERROR;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2000-02-13 01:39:21 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* fetch the image */
|
2000-02-13 01:39:21 +08:00
|
|
|
pixels = g_new (guchar, drawable->width * drawable->height * channels);
|
|
|
|
gimp_pixel_rgn_get_rect (&pixel_rgn, pixels,
|
2004-07-17 19:33:03 +08:00
|
|
|
0, 0, drawable->width, drawable->height);
|
2000-02-13 01:39:21 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* And let's begin the progress */
|
2005-09-30 16:16:10 +08:00
|
|
|
gimp_progress_init_printf (_("Saving '%s'"),
|
2005-09-30 02:34:08 +08:00
|
|
|
gimp_filename_to_utf8 (filename));
|
2003-06-13 22:37:00 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
cur_progress = 0;
|
|
|
|
max_progress = drawable->height;
|
2000-02-13 01:39:21 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* Now, we need some further information ... */
|
|
|
|
cols = drawable->width;
|
|
|
|
rows = drawable->height;
|
2000-02-13 01:39:21 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* ... that we write to our headers. */
|
2005-06-08 00:55:21 +08:00
|
|
|
if ((BitsPerPixel < 24) &&
|
2000-02-13 01:39:21 +08:00
|
|
|
(cols % (8/BitsPerPixel)))
|
|
|
|
Spcols = (((cols / (8 / BitsPerPixel)) + 1) * (8 / BitsPerPixel));
|
|
|
|
else
|
|
|
|
Spcols = cols;
|
|
|
|
|
|
|
|
if ((((Spcols * BitsPerPixel) / 8) % 4) == 0)
|
|
|
|
SpZeile = ((Spcols * BitsPerPixel) / 8);
|
|
|
|
else
|
|
|
|
SpZeile = ((gint) (((Spcols * BitsPerPixel) / 8) / 4) + 1) * 4;
|
|
|
|
|
|
|
|
Bitmap_File_Head.bfSize = 0x36 + MapSize + (rows * SpZeile);
|
2000-05-06 09:38:32 +08:00
|
|
|
Bitmap_File_Head.zzHotX = 0;
|
|
|
|
Bitmap_File_Head.zzHotY = 0;
|
2000-02-13 01:39:21 +08:00
|
|
|
Bitmap_File_Head.bfOffs = 0x36 + MapSize;
|
2003-11-06 23:27:05 +08:00
|
|
|
Bitmap_File_Head.biSize = 40;
|
2000-02-13 01:39:21 +08:00
|
|
|
|
|
|
|
Bitmap_Head.biWidth = cols;
|
|
|
|
Bitmap_Head.biHeight = rows;
|
2000-05-21 00:11:29 +08:00
|
|
|
Bitmap_Head.biPlanes = 1;
|
2000-02-13 01:39:21 +08:00
|
|
|
Bitmap_Head.biBitCnt = BitsPerPixel;
|
|
|
|
|
|
|
|
if (encoded == 0)
|
|
|
|
Bitmap_Head.biCompr = 0;
|
|
|
|
else if (BitsPerPixel == 8)
|
|
|
|
Bitmap_Head.biCompr = 1;
|
|
|
|
else if (BitsPerPixel == 4)
|
|
|
|
Bitmap_Head.biCompr = 2;
|
|
|
|
else
|
|
|
|
Bitmap_Head.biCompr = 0;
|
|
|
|
|
|
|
|
Bitmap_Head.biSizeIm = SpZeile * rows;
|
|
|
|
|
1999-12-19 20:20:38 +08:00
|
|
|
{
|
2000-02-13 01:39:21 +08:00
|
|
|
gdouble xresolution;
|
|
|
|
gdouble yresolution;
|
1999-12-19 20:20:38 +08:00
|
|
|
gimp_image_get_resolution (image, &xresolution, &yresolution);
|
|
|
|
|
2000-02-13 01:39:21 +08:00
|
|
|
if (xresolution > GIMP_MIN_RESOLUTION &&
|
2004-07-17 19:33:03 +08:00
|
|
|
yresolution > GIMP_MIN_RESOLUTION)
|
2000-02-13 01:39:21 +08:00
|
|
|
{
|
2004-07-17 19:33:03 +08:00
|
|
|
/*
|
|
|
|
* xresolution and yresolution are in dots per inch.
|
|
|
|
* the BMP spec says that biXPels and biYPels are in
|
|
|
|
* pixels per meter as long ints (actually, "DWORDS"),
|
|
|
|
* so...
|
|
|
|
* n dots inch 100 cm m dots
|
|
|
|
* ------ * ------- * ------ = ------
|
|
|
|
* inch 2.54 cm m inch
|
2006-02-27 05:55:40 +08:00
|
|
|
*
|
|
|
|
* We add 0.5 for proper rounding.
|
2004-07-17 19:33:03 +08:00
|
|
|
*/
|
2006-02-27 05:55:40 +08:00
|
|
|
Bitmap_Head.biXPels = (long int) (xresolution * 100.0 / 2.54 + 0.5);
|
|
|
|
Bitmap_Head.biYPels = (long int) (yresolution * 100.0 / 2.54 + 0.5);
|
1999-12-19 20:20:38 +08:00
|
|
|
}
|
|
|
|
}
|
2001-05-22 08:36:38 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
if (BitsPerPixel < 24)
|
2000-02-13 01:39:21 +08:00
|
|
|
Bitmap_Head.biClrUsed = colors;
|
2003-11-06 23:27:05 +08:00
|
|
|
else
|
2000-02-13 01:39:21 +08:00
|
|
|
Bitmap_Head.biClrUsed = 0;
|
1999-12-19 20:20:38 +08:00
|
|
|
|
2000-02-13 01:39:21 +08:00
|
|
|
Bitmap_Head.biClrImp = Bitmap_Head.biClrUsed;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("\nSize: %u, Colors: %u, Bits: %u, Width: %u, Height: %u, Comp: %u, Zeile: %u\n",
|
1998-04-07 11:41:22 +08:00
|
|
|
(int)Bitmap_File_Head.bfSize,(int)Bitmap_Head.biClrUsed,Bitmap_Head.biBitCnt,(int)Bitmap_Head.biWidth,
|
|
|
|
(int)Bitmap_Head.biHeight, (int)Bitmap_Head.biCompr,SpZeile);
|
1997-11-25 06:05:25 +08:00
|
|
|
#endif
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* And now write the header and the colormap (if any) to disk */
|
2000-02-13 01:39:21 +08:00
|
|
|
|
|
|
|
Write (outfile, "BM", 2);
|
|
|
|
|
|
|
|
FromL (Bitmap_File_Head.bfSize, &puffer[0x00]);
|
2000-05-06 09:38:32 +08:00
|
|
|
FromS (Bitmap_File_Head.zzHotX, &puffer[0x04]);
|
|
|
|
FromS (Bitmap_File_Head.zzHotY, &puffer[0x06]);
|
2000-02-13 01:39:21 +08:00
|
|
|
FromL (Bitmap_File_Head.bfOffs, &puffer[0x08]);
|
|
|
|
FromL (Bitmap_File_Head.biSize, &puffer[0x0C]);
|
|
|
|
|
|
|
|
Write (outfile, puffer, 16);
|
|
|
|
|
|
|
|
FromL (Bitmap_Head.biWidth, &puffer[0x00]);
|
|
|
|
FromL (Bitmap_Head.biHeight, &puffer[0x04]);
|
|
|
|
FromS (Bitmap_Head.biPlanes, &puffer[0x08]);
|
|
|
|
FromS (Bitmap_Head.biBitCnt, &puffer[0x0A]);
|
|
|
|
FromL (Bitmap_Head.biCompr, &puffer[0x0C]);
|
|
|
|
FromL (Bitmap_Head.biSizeIm, &puffer[0x10]);
|
|
|
|
FromL (Bitmap_Head.biXPels, &puffer[0x14]);
|
|
|
|
FromL (Bitmap_Head.biYPels, &puffer[0x18]);
|
|
|
|
FromL (Bitmap_Head.biClrUsed, &puffer[0x1C]);
|
|
|
|
FromL (Bitmap_Head.biClrImp, &puffer[0x20]);
|
|
|
|
|
|
|
|
Write (outfile, puffer, 36);
|
|
|
|
WriteColorMap (outfile, Red, Green, Blue, MapSize);
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* After that is done, we write the image ... */
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2000-02-13 01:39:21 +08:00
|
|
|
WriteImage (outfile,
|
2004-07-17 19:33:03 +08:00
|
|
|
pixels, cols, rows,
|
|
|
|
encoded, channels, BitsPerPixel, SpZeile, MapSize);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* ... and exit normally */
|
2000-02-13 01:39:21 +08:00
|
|
|
|
|
|
|
fclose (outfile);
|
|
|
|
gimp_drawable_detach (drawable);
|
|
|
|
g_free (pixels);
|
|
|
|
|
2000-08-22 11:27:14 +08:00
|
|
|
return GIMP_PDB_SUCCESS;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2003-11-25 21:49:16 +08:00
|
|
|
static void
|
2003-11-06 23:27:05 +08:00
|
|
|
WriteImage (FILE *f,
|
2004-07-17 19:33:03 +08:00
|
|
|
guchar *src,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gint encoded,
|
|
|
|
gint channels,
|
|
|
|
gint bpp,
|
|
|
|
gint spzeile,
|
|
|
|
gint MapSize)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1998-04-07 11:41:22 +08:00
|
|
|
guchar buf[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0};
|
|
|
|
guchar puffer[8];
|
2000-02-13 01:39:21 +08:00
|
|
|
guchar *temp, v;
|
|
|
|
guchar *Zeile, *ketten;
|
|
|
|
gint xpos, ypos, i, j, rowstride, laenge, thiswidth;
|
2004-07-17 19:33:03 +08:00
|
|
|
gint breite, k;
|
|
|
|
guchar n;
|
2000-02-13 01:39:21 +08:00
|
|
|
|
|
|
|
xpos = 0;
|
|
|
|
rowstride = width * channels;
|
1998-04-07 11:41:22 +08:00
|
|
|
|
2005-06-08 00:55:21 +08:00
|
|
|
/* We'll begin with the 32 and 24 bit Bitmaps, they are easy :-) */
|
2000-02-13 01:39:21 +08:00
|
|
|
|
2005-06-08 00:55:21 +08:00
|
|
|
if (bpp == 32)
|
|
|
|
{
|
|
|
|
for (ypos = height - 1; ypos >= 0; ypos--) /* for each row */
|
|
|
|
{
|
|
|
|
for (i = 0; i < width; i++) /* for each pixel */
|
|
|
|
{
|
|
|
|
temp = src + (ypos * rowstride) + (xpos * channels);
|
|
|
|
buf[2] = (guchar) *temp;
|
|
|
|
temp++;
|
|
|
|
buf[1] = (guchar) *temp;
|
|
|
|
temp++;
|
|
|
|
buf[0] = (guchar) *temp;
|
|
|
|
temp++;
|
|
|
|
buf[3] = (guchar) *temp;
|
|
|
|
xpos++;
|
|
|
|
|
|
|
|
Write (f, buf, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
Write (f, &buf[3], spzeile - (width * 4));
|
|
|
|
|
|
|
|
cur_progress++;
|
|
|
|
if ((cur_progress % 5) == 0)
|
|
|
|
gimp_progress_update ((gdouble) cur_progress /
|
|
|
|
(gdouble) max_progress);
|
|
|
|
|
|
|
|
xpos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (bpp == 24)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-02-13 01:39:21 +08:00
|
|
|
for (ypos = height - 1; ypos >= 0; ypos--) /* for each row */
|
2004-07-17 19:33:03 +08:00
|
|
|
{
|
|
|
|
for (i = 0; i < width; i++) /* for each pixel */
|
|
|
|
{
|
|
|
|
temp = src + (ypos * rowstride) + (xpos * channels);
|
|
|
|
buf[2] = (guchar) *temp;
|
|
|
|
temp++;
|
|
|
|
buf[1] = (guchar) *temp;
|
|
|
|
temp++;
|
|
|
|
buf[0] = (guchar) *temp;
|
|
|
|
xpos++;
|
|
|
|
Write (f, buf, 3);
|
|
|
|
}
|
|
|
|
Write (f, &buf[3], spzeile - (width * 3));
|
|
|
|
|
|
|
|
cur_progress++;
|
|
|
|
if ((cur_progress % 5) == 0)
|
|
|
|
gimp_progress_update ((gdouble) cur_progress /
|
|
|
|
(gdouble) max_progress);
|
|
|
|
|
|
|
|
xpos = 0;
|
|
|
|
}
|
2000-02-13 01:39:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (encoded) /* now it gets more difficult */
|
2004-07-17 19:33:03 +08:00
|
|
|
{ /* uncompressed 1,4 and 8 bit */
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
thiswidth = (width / (8 / bpp));
|
|
|
|
if (width % (8 / bpp))
|
|
|
|
thiswidth++;
|
|
|
|
|
|
|
|
for (ypos = height - 1; ypos >= 0; ypos--) /* for each row */
|
|
|
|
{
|
|
|
|
for (xpos = 0; xpos < width;) /* for each _byte_ */
|
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
for (i = 1;
|
|
|
|
(i <= (8 / bpp)) && (xpos < width);
|
|
|
|
i++, xpos++) /* for each pixel */
|
|
|
|
{
|
|
|
|
temp = src + (ypos * rowstride) + (xpos * channels);
|
|
|
|
v=v | ((guchar) *temp << (8 - (i * bpp)));
|
|
|
|
}
|
|
|
|
Write (f, &v, 1);
|
|
|
|
}
|
|
|
|
Write (f, &buf[3], spzeile - thiswidth);
|
|
|
|
xpos = 0;
|
|
|
|
|
|
|
|
cur_progress++;
|
|
|
|
if ((cur_progress % 5) == 0)
|
|
|
|
gimp_progress_update ((gdouble) cur_progress /
|
|
|
|
(gdouble) max_progress);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{ /* Save RLE encoded file, quite difficult */
|
|
|
|
laenge = 0;
|
|
|
|
buf[12] = 0;
|
|
|
|
buf[13] = 1;
|
|
|
|
buf[14] = 0;
|
|
|
|
buf[15] = 0;
|
|
|
|
Zeile = (guchar *) g_malloc (width / (8 / bpp) + 10);
|
|
|
|
ketten = (guchar *) g_malloc (width / (8 / bpp) + 10);
|
|
|
|
for (ypos = height - 1; ypos >= 0; ypos--)
|
|
|
|
{ /* each row separately */
|
|
|
|
j = 0;
|
|
|
|
/* first copy the pixels to a buffer,
|
|
|
|
* making one byte from two 4bit pixels
|
|
|
|
*/
|
|
|
|
for (xpos = 0; xpos < width;)
|
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
for (i = 1;
|
|
|
|
(i <= (8 / bpp)) && (xpos < width);
|
|
|
|
i++, xpos++)
|
|
|
|
{ /* for each pixel */
|
|
|
|
temp = src + (ypos * rowstride) + (xpos * channels);
|
|
|
|
v = v | ((guchar) * temp << (8 - (i * bpp)));
|
|
|
|
}
|
|
|
|
Zeile[j++] = v;
|
|
|
|
}
|
|
|
|
breite = width / (8 / bpp);
|
|
|
|
if (width % (8 / bpp))
|
|
|
|
breite++;
|
|
|
|
/* then check for strings of equal bytes */
|
|
|
|
for (i = 0; i < breite;)
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
while ((i + j < breite) &&
|
|
|
|
(j < (255 / (8 / bpp))) &&
|
|
|
|
(Zeile[i + j] == Zeile[i]))
|
|
|
|
j++;
|
|
|
|
|
|
|
|
ketten[i] = j;
|
|
|
|
i += j;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* then write the strings and the other pixels to the file */
|
|
|
|
for (i = 0; i < breite;)
|
|
|
|
{
|
|
|
|
if (ketten[i] < 3)
|
|
|
|
/* strings of different pixels ... */
|
|
|
|
{
|
|
|
|
j = 0;
|
|
|
|
while ((i + j < breite) &&
|
|
|
|
(j < (255 / (8 / bpp))) &&
|
|
|
|
(ketten[i + j] < 3))
|
|
|
|
j += ketten[i + j];
|
|
|
|
|
|
|
|
/* this can only happen if j jumps over
|
|
|
|
* the end with a 2 in ketten[i+j]
|
|
|
|
*/
|
|
|
|
if (j > (255 / (8 / bpp)))
|
|
|
|
j -= 2;
|
|
|
|
/* 00 01 and 00 02 are reserved */
|
|
|
|
if (j > 2)
|
|
|
|
{
|
|
|
|
Write (f, &buf[12], 1);
|
|
|
|
n = j * (8 / bpp);
|
|
|
|
if (n + i * (8 / bpp) > width)
|
|
|
|
n--;
|
|
|
|
Write (f, &n, 1);
|
|
|
|
laenge += 2;
|
|
|
|
Write (f, &Zeile[i], j);
|
|
|
|
laenge += j;
|
|
|
|
if ((j) % 2)
|
|
|
|
{
|
|
|
|
Write (f, &buf[12], 1);
|
|
|
|
laenge++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (k = i; k < i + j; k++)
|
|
|
|
{
|
|
|
|
n = (8 / bpp);
|
|
|
|
if (n + i * (8 / bpp) > width)
|
|
|
|
n--;
|
|
|
|
Write (f, &n, 1);
|
|
|
|
Write (f, &Zeile[k], 1);
|
|
|
|
/*printf("%i.#|",n); */
|
|
|
|
laenge += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += j;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* strings of equal pixels */
|
|
|
|
{
|
|
|
|
n = ketten[i] * (8 / bpp);
|
|
|
|
if (n + i * (8 / bpp) > width)
|
|
|
|
n--;
|
|
|
|
Write (f, &n, 1);
|
|
|
|
Write (f, &Zeile[i], 1);
|
|
|
|
i += ketten[i];
|
|
|
|
laenge += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Write (f, &buf[14], 2); /* End of row */
|
|
|
|
laenge += 2;
|
|
|
|
|
|
|
|
cur_progress++;
|
|
|
|
if ((cur_progress % 5) == 0)
|
|
|
|
gimp_progress_update ((gdouble) cur_progress /
|
|
|
|
(gdouble) max_progress);
|
|
|
|
}
|
|
|
|
fseek (f, -2, SEEK_CUR); /* Overwrite last End of row ... */
|
|
|
|
Write (f, &buf[12], 2); /* ... with End of file */
|
|
|
|
|
|
|
|
fseek (f, 0x22, SEEK_SET); /* Write length of image */
|
|
|
|
FromL (laenge, puffer);
|
|
|
|
Write (f, puffer, 4);
|
|
|
|
fseek (f, 0x02, SEEK_SET); /* Write length of file */
|
|
|
|
laenge += (0x36 + MapSize);
|
|
|
|
FromL (laenge, puffer);
|
|
|
|
Write (f, puffer, 4);
|
|
|
|
g_free (ketten);
|
|
|
|
g_free (Zeile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2003-06-13 22:37:00 +08:00
|
|
|
|
|
|
|
gimp_progress_update (1);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2003-11-25 21:49:16 +08:00
|
|
|
static gboolean
|
2000-02-13 01:39:21 +08:00
|
|
|
save_dialog (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
GtkWidget *dlg;
|
|
|
|
GtkWidget *toggle;
|
|
|
|
GtkWidget *vbox;
|
2003-11-06 23:27:05 +08:00
|
|
|
gboolean run;
|
|
|
|
|
2000-02-13 01:39:21 +08:00
|
|
|
dlg = gimp_dialog_new (_("Save as BMP"), "bmp",
|
2003-11-06 23:27:05 +08:00
|
|
|
NULL, 0,
|
2004-07-17 19:33:03 +08:00
|
|
|
gimp_standard_help_func, "file-bmp-save",
|
2000-02-13 01:39:21 +08:00
|
|
|
|
2004-07-17 19:33:03 +08:00
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
2005-09-02 19:09:03 +08:00
|
|
|
GTK_STOCK_SAVE, GTK_RESPONSE_OK,
|
2001-12-29 21:26:29 +08:00
|
|
|
|
2004-07-17 19:33:03 +08:00
|
|
|
NULL);
|
2000-02-13 01:39:21 +08:00
|
|
|
|
2005-02-09 04:40:33 +08:00
|
|
|
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dlg),
|
2005-09-06 05:40:29 +08:00
|
|
|
GTK_RESPONSE_OK,
|
|
|
|
GTK_RESPONSE_CANCEL,
|
|
|
|
-1);
|
2005-02-09 04:40:33 +08:00
|
|
|
|
2004-05-20 23:51:25 +08:00
|
|
|
vbox = gtk_vbox_new (FALSE, 12);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
|
|
|
|
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
|
|
|
|
gtk_widget_show (vbox);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-09-06 05:40:29 +08:00
|
|
|
toggle = gtk_check_button_new_with_mnemonic (_("_Run-Length Encoded"));
|
2000-02-13 01:39:21 +08:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
|
1999-01-16 01:35:04 +08:00
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), encoded);
|
1997-11-25 06:05:25 +08:00
|
|
|
gtk_widget_show (toggle);
|
|
|
|
|
2003-01-07 14:16:02 +08:00
|
|
|
g_signal_connect (toggle, "toggled",
|
2001-12-29 21:26:29 +08:00
|
|
|
G_CALLBACK (gimp_toggle_button_update),
|
|
|
|
&encoded);
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
gtk_widget_show (dlg);
|
|
|
|
|
2003-11-12 02:11:56 +08:00
|
|
|
run = (gimp_dialog_run (GIMP_DIALOG (dlg)) == GTK_RESPONSE_OK);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
gtk_widget_destroy (dlg);
|
2000-02-13 01:39:21 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
return run;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|