Commit b8e13405 authored by Sven Neumann's avatar Sven Neumann Committed by Sven Neumann

plug-ins/bmp/bmp.[ch] plug-ins/bmp/bmpread.c some code cleanup (what a

2003-11-25  Sven Neumann  <sven@gimp.org>

	* plug-ins/bmp/bmp.[ch]
	* plug-ins/bmp/bmpread.c
	* plug-ins/bmp/bmpwrite.c: some code cleanup (what a mess...).
parent 80fbda05
2003-11-25 Sven Neumann <sven@gimp.org>
* plug-ins/bmp/bmp.[ch]
* plug-ins/bmp/bmpread.c
* plug-ins/bmp/bmpwrite.c: some code cleanup (what a mess...).
2003-11-25 Michael Natterer <mitch@gimp.org>
* app/core/gimpobject.[ch]: removed gimp_g_object_get_memsize()...
......@@ -12,11 +18,11 @@
* app/vectors/gimpstroke.c (GimpObject::get_memsize): use the new
functions.
2003-11-22 Dave Neary <bolsh@gimp.org>
2003-11-25 Dave Neary <bolsh@gimp.org>
* libgimp/Makefile.am: Applied patch from deaddog@deaddog.org (Abel
Cheung) to make sure dependencies are built before we build libgimp and
libgimpui. Fixes bug #127863.
* libgimp/Makefile.am: Applied patch from deaddog@deaddog.org
(Abel Cheung) to make sure dependencies are built before we build
libgimp and libgimpui. Fixes bug #127863.
2003-11-25 Maurits Rijk <m.rijk@chello.nl>
......@@ -63,7 +63,6 @@
#include "libgimp/stdplugins-intl.h"
const gchar *prog_name = "bmp";
const gchar *filename = NULL;
gboolean interactive_bmp;
......
#include <libgimp/gimp.h>
/*
* 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.
*/
#define MAXCOLORS 256
#define Image gint32
#define MAXCOLORS 256
#define BitSet(byte, bit) (((byte) & (bit)) == (bit))
#define BitSet(byte, bit) (((byte) & (bit)) == (bit))
#define ReadOK(file,buffer,len) (fread(buffer, len, 1, file) != 0)
#define Write(file,buffer,len) fwrite(buffer, len, 1, file)
#define WriteOK(file,buffer,len) (Write(buffer, len, file) != 0)
extern gint32 ReadBMP (const gchar *filename);
extern GimpPDBStatusType WriteBMP (const gchar *filename,
gint32,
gint32);
extern Image ReadImage (FILE *,
gint,
gint,
guchar[256][3],
gint,
gint,
gint,
gint,
gint);
extern void WriteImage (FILE *,
guchar *,
gint,
gint,
gint,
gint,
gint,
gint,
gint);
gint32 ReadBMP (const gchar *filename);
GimpPDBStatusType WriteBMP (const gchar *filename,
gint32 image,
gint32 drawable_ID);
extern gboolean interactive_bmp;
extern const gchar *prog_name;
extern const gchar *filename;
extern gboolean interactive_bmp;
extern const gchar *filename;
extern struct Bitmap_File_Head_Struct
{
......
......@@ -43,31 +43,43 @@
#define BI_BITFIELDS 3
#endif
static gboolean using565;
static gboolean using565 = FALSE;
static gint32 ReadImage (FILE *fd,
gint width,
gint height,
guchar cmap[256][3],
gint ncols,
gint bpp,
gint compression,
gint rowbytes,
gboolean grey);
static gint32
ToL (guchar *puffer)
ToL (const guchar *puffer)
{
return (puffer[0] | puffer[1]<<8 | puffer[2]<<16 | puffer[3]<<24);
return (puffer[0] | puffer[1] << 8 | puffer[2] << 16 | puffer[3] << 24);
}
static gint16
ToS (guchar *puffer)
ToS (const guchar *puffer)
{
return (puffer[0] | puffer[1]<<8);
return (puffer[0] | puffer[1] << 8);
}
static gboolean
ReadColorMap (FILE *fd,
guchar buffer[256][3],
gint number,
gint size,
gint *grey)
ReadColorMap (FILE *fd,
guchar buffer[256][3],
gint number,
gint size,
gboolean *grey)
{
gint i;
gint i;
guchar rgb[4];
*grey=(number>2);
*grey = (number > 2);
for (i = 0; i < number ; i++)
{
if (!ReadOK (fd, rgb, size))
......@@ -89,13 +101,14 @@ ReadColorMap (FILE *fd,
gint32
ReadBMP (const gchar *name)
{
FILE *fd;
gchar *temp_buf;
guchar buffer[64];
gint ColormapSize, rowbytes, Maps, Grey;
guchar ColorMap[256][3];
gint32 image_ID;
guchar magick[2];
FILE *fd;
gchar *temp_buf;
guchar buffer[64];
gint ColormapSize, rowbytes, Maps;
gboolean Grey;
guchar ColorMap[256][3];
gint32 image_ID;
guchar magick[2];
filename = name;
fd = fopen (filename, "rb");
......@@ -113,10 +126,10 @@ ReadBMP (const gchar *name)
/* It is a File. Now is it a Bitmap? Read the shortest possible header */
if (!ReadOK(fd, magick, 2) || !(!strncmp(magick,"BA",2) ||
!strncmp(magick,"BM",2) || !strncmp(magick,"IC",2) ||
!strncmp(magick,"PI",2) || !strncmp(magick,"CI",2) ||
!strncmp(magick,"CP",2)))
if (!ReadOK (fd, magick, 2) || !(!strncmp (magick,"BA",2) ||
!strncmp (magick,"BM",2) || !strncmp (magick,"IC",2) ||
!strncmp (magick,"PI",2) || !strncmp (magick,"CI",2) ||
!strncmp (magick,"CP",2)))
{
g_message (_("'%s' is not a valid BMP file"), filename);
return -1;
......@@ -124,19 +137,19 @@ ReadBMP (const gchar *name)
while (!strncmp(magick,"BA",2))
{
if (!ReadOK(fd, buffer, 12))
if (!ReadOK (fd, buffer, 12))
{
g_message (_("'%s' is not a valid BMP file"), filename);
return -1;
}
if (!ReadOK(fd, magick, 2))
if (!ReadOK (fd, magick, 2))
{
g_message (_("'%s' is not a valid BMP file"), filename);
return -1;
}
}
if (!ReadOK(fd, buffer, 12))
if (!ReadOK (fd, buffer, 12))
{
g_message (_("'%s' is not a valid BMP file"), filename);
return -1;
......@@ -149,7 +162,7 @@ ReadBMP (const gchar *name)
Bitmap_File_Head.zzHotY = ToS (&buffer[0x06]);
Bitmap_File_Head.bfOffs = ToL (&buffer[0x08]);
if (!ReadOK(fd, buffer, 4))
if (!ReadOK (fd, buffer, 4))
{
g_message (_("'%s' is not a valid BMP file"), filename);
return -1;
......@@ -167,13 +180,13 @@ ReadBMP (const gchar *name)
return -1;
}
Bitmap_Head.biWidth =ToS (&buffer[0x00]); /* 12 */
Bitmap_Head.biHeight =ToS (&buffer[0x02]); /* 14 */
Bitmap_Head.biPlanes =ToS (&buffer[0x04]); /* 16 */
Bitmap_Head.biBitCnt =ToS (&buffer[0x06]); /* 18 */
Bitmap_Head.biCompr = 0;
Bitmap_Head.biSizeIm = 0;
Bitmap_Head.biXPels = Bitmap_Head.biYPels = 0;
Bitmap_Head.biWidth = ToS (&buffer[0x00]); /* 12 */
Bitmap_Head.biHeight = ToS (&buffer[0x02]); /* 14 */
Bitmap_Head.biPlanes = ToS (&buffer[0x04]); /* 16 */
Bitmap_Head.biBitCnt = ToS (&buffer[0x06]); /* 18 */
Bitmap_Head.biCompr = 0;
Bitmap_Head.biSizeIm = 0;
Bitmap_Head.biXPels = Bitmap_Head.biYPels = 0;
Bitmap_Head.biClrUsed = 0;
Maps = 3;
}
......@@ -184,16 +197,16 @@ ReadBMP (const gchar *name)
g_message (_("Error reading BMP file header from '%s'"), filename);
return -1;
}
Bitmap_Head.biWidth =ToL (&buffer[0x00]); /* 12 */
Bitmap_Head.biHeight =ToL (&buffer[0x04]); /* 16 */
Bitmap_Head.biPlanes =ToS (&buffer[0x08]); /* 1A */
Bitmap_Head.biBitCnt =ToS (&buffer[0x0A]); /* 1C */
Bitmap_Head.biCompr =ToL (&buffer[0x0C]); /* 1E */
Bitmap_Head.biSizeIm =ToL (&buffer[0x10]); /* 22 */
Bitmap_Head.biXPels =ToL (&buffer[0x14]); /* 26 */
Bitmap_Head.biYPels =ToL (&buffer[0x18]); /* 2A */
Bitmap_Head.biClrUsed =ToL (&buffer[0x1C]); /* 2E */
Bitmap_Head.biClrImp =ToL (&buffer[0x20]); /* 32 */
Bitmap_Head.biWidth = ToL (&buffer[0x00]); /* 12 */
Bitmap_Head.biHeight = ToL (&buffer[0x04]); /* 16 */
Bitmap_Head.biPlanes = ToS (&buffer[0x08]); /* 1A */
Bitmap_Head.biBitCnt = ToS (&buffer[0x0A]); /* 1C */
Bitmap_Head.biCompr = ToL (&buffer[0x0C]); /* 1E */
Bitmap_Head.biSizeIm = ToL (&buffer[0x10]); /* 22 */
Bitmap_Head.biXPels = ToL (&buffer[0x14]); /* 26 */
Bitmap_Head.biYPels = ToL (&buffer[0x18]); /* 2A */
Bitmap_Head.biClrUsed = ToL (&buffer[0x1C]); /* 2E */
Bitmap_Head.biClrImp = ToL (&buffer[0x20]); /* 32 */
/* 36 */
Maps = 4;
}
......@@ -255,22 +268,28 @@ ReadBMP (const gchar *name)
* word length (32 bits == 4 bytes)
*/
rowbytes= ( (Bitmap_Head.biWidth * Bitmap_Head.biBitCnt - 1) / 32) * 4 + 4;
rowbytes= ((Bitmap_Head.biWidth * Bitmap_Head.biBitCnt - 1) / 32) * 4 + 4;
#ifdef DEBUG
printf("\nSize: %u, Colors: %u, Bits: %u, Width: %u, Height: %u, Comp: %u, Zeile: %u\n",
Bitmap_File_Head.bfSize,Bitmap_Head.biClrUsed,Bitmap_Head.biBitCnt,Bitmap_Head.biWidth,
Bitmap_Head.biHeight, Bitmap_Head.biCompr, rowbytes);
printf ("\nSize: %u, Colors: %u, Bits: %u, Width: %u, Height: %u, "
"Comp: %u, Zeile: %u\n",
Bitmap_File_Head.bfSize,
Bitmap_Head.biClrUsed,
Bitmap_Head.biBitCnt,
Bitmap_Head.biWidth,
Bitmap_Head.biHeight,
Bitmap_Head.biCompr,
rowbytes);
#endif
if (Bitmap_Head.biCompr == BI_BITFIELDS &&
(Bitmap_Head.biBitCnt == 16 || Bitmap_Head.biBitCnt == 16))
{
gint32 tmp[3];
gint32 green_mask;
gint i, green;
gint i, green;
if (!ReadOK(fd, tmp, 3 * sizeof(gint32)))
if (!ReadOK (fd, tmp, 3 * sizeof (gint32)))
{
g_message (_("'%s' is not a valid BMP file"), filename);
return -1;
......@@ -286,7 +305,7 @@ ReadBMP (const gchar *name)
green_mask = green_mask >> 1;
}
using565 = green == 6;
using565 = (green == 6);
}
else
{
......@@ -296,10 +315,10 @@ ReadBMP (const gchar *name)
}
#ifdef DEBUG
printf("Colormap read\n");
printf ("Colormap read\n");
#endif
fseek(fd, Bitmap_File_Head.bfOffs, SEEK_SET);
fseek (fd, Bitmap_File_Head.bfOffs, SEEK_SET);
/* Get the Image and return the ID or -1 on error*/
image_ID = ReadImage (fd,
......@@ -332,28 +351,29 @@ ReadBMP (const gchar *name)
return image_ID;
}
Image
ReadImage (FILE *fd,
gint width,
gint height,
guchar cmap[256][3],
gint ncols,
gint bpp,
gint compression,
gint rowbytes,
gint grey)
static gint32
ReadImage (FILE *fd,
gint width,
gint height,
guchar cmap[256][3],
gint ncols,
gint bpp,
gint compression,
gint rowbytes,
gboolean grey)
{
guchar v,wieviel;
GimpPixelRgn pixel_rgn;
gint xpos = 0, ypos = 0;
Image image;
gint32 layer;
guchar v, n;
GimpPixelRgn pixel_rgn;
gint xpos = 0;
gint ypos = 0;
gint32 image;
gint32 layer;
GimpDrawable *drawable;
guchar *dest, *temp, *buffer;
guchar gimp_cmap[768];
gushort rgb;
long rowstride, channels;
gint i, j, cur_progress, max_progress, unused;
guchar *dest, *temp, *buffer;
guchar gimp_cmap[768];
gushort rgb;
glong rowstride, channels;
gint i, j, cur_progress, max_progress;
/* Make a new image in the gimp */
......@@ -382,95 +402,96 @@ ReadImage (FILE *fd,
channels = 1;
}
gimp_image_set_filename(image, filename);
gimp_image_set_filename (image, filename);
gimp_image_add_layer(image,layer,0);
drawable = gimp_drawable_get(layer);
gimp_image_add_layer (image, layer, 0);
drawable = gimp_drawable_get (layer);
dest = g_malloc(drawable->width*drawable->height*channels);
buffer= g_malloc(rowbytes);
dest = g_malloc (drawable->width * drawable->height * channels);
buffer = g_malloc (rowbytes);
rowstride = drawable->width * channels;
ypos = height - 1; /* Bitmaps begin in the lower left corner */
cur_progress = 0;
max_progress = height;
switch (bpp) {
case 32:
switch (bpp)
{
while (ReadOK (fd, buffer, rowbytes))
{
temp = dest + (ypos * rowstride);
for (xpos= 0; xpos < width; ++xpos)
{
*(temp++)= buffer[xpos * 4 + 2];
*(temp++)= buffer[xpos * 4 + 1];
*(temp++)= buffer[xpos * 4];
}
--ypos; /* next line */
cur_progress++;
if ((cur_progress % 5) == 0)
gimp_progress_update ((gdouble) cur_progress /
(gdouble) max_progress);
}
}
break;
case 32:
{
while (ReadOK (fd, buffer, rowbytes))
{
temp = dest + (ypos * rowstride);
for (xpos= 0; xpos < width; ++xpos)
{
*(temp++)= buffer[xpos * 4 + 2];
*(temp++)= buffer[xpos * 4 + 1];
*(temp++)= buffer[xpos * 4];
}
--ypos; /* next line */
cur_progress++;
if ((cur_progress % 5) == 0)
gimp_progress_update ((gdouble) cur_progress /
(gdouble) max_progress);
}
}
break;
case 24:
{
while (ReadOK (fd, buffer, rowbytes))
{
temp = dest + (ypos * rowstride);
for (xpos= 0; xpos < width; ++xpos)
{
*(temp++)= buffer[xpos * 3 + 2];
*(temp++)= buffer[xpos * 3 + 1];
*(temp++)= buffer[xpos * 3];
}
--ypos; /* next line */
cur_progress++;
if ((cur_progress % 5) == 0)
gimp_progress_update ((gdouble) cur_progress /
(gdouble) max_progress);
}
}
break;
case 24:
{
while (ReadOK (fd, buffer, rowbytes))
{
temp = dest + (ypos * rowstride);
for (xpos= 0; xpos < width; ++xpos)
{
*(temp++)= buffer[xpos * 3 + 2];
*(temp++)= buffer[xpos * 3 + 1];
*(temp++)= buffer[xpos * 3];
}
--ypos; /* next line */
cur_progress++;
if ((cur_progress % 5) == 0)
gimp_progress_update ((gdouble) cur_progress /
(gdouble) max_progress);
}
}
break;
case 16:
{
while (ReadOK (fd, buffer, rowbytes))
{
temp = dest + (ypos * rowstride);
for (xpos= 0; xpos < width; ++xpos)
{
rgb= ToS(&buffer[xpos * 2]);
if (using565) /* rgb 565 encoded */
{
*(temp++)= ((rgb >> 11) & 0x1f) * 8;
*(temp++)= ((rgb >> 5) & 0x3f) * 4;
*(temp++)= ((rgb) & 0x1f) * 8;
}
else /* rgb555 */
{
*(temp++)= ((rgb >> 10) & 0x1f) * 8;
*(temp++)= ((rgb >> 5) & 0x1f) * 8;
*(temp++)= ((rgb) & 0x1f) * 8;
}
}
--ypos; /* next line */
cur_progress++;
if ((cur_progress % 5) == 0)
gimp_progress_update ((gdouble) cur_progress /
(gdouble) max_progress);
}
}
break;
case 8:
case 4:
case 1:
{
if (compression == 0)
case 16:
{
while (ReadOK (fd, buffer, rowbytes))
{
temp = dest + (ypos * rowstride);
for (xpos= 0; xpos < width; ++xpos)
{
rgb= ToS(&buffer[xpos * 2]);
if (using565) /* rgb 565 encoded */
{
*(temp++)= ((rgb >> 11) & 0x1f) * 8;
*(temp++)= ((rgb >> 5) & 0x3f) * 4;
*(temp++)= ((rgb) & 0x1f) * 8;
}
else /* rgb555 */
{
*(temp++)= ((rgb >> 10) & 0x1f) * 8;
*(temp++)= ((rgb >> 5) & 0x1f) * 8;
*(temp++)= ((rgb) & 0x1f) * 8;
}
}
--ypos; /* next line */
cur_progress++;
if ((cur_progress % 5) == 0)
gimp_progress_update ((gdouble) cur_progress /
(gdouble) max_progress);
}
}
break;
case 8:
case 4:
case 1:
{
if (compression == 0)
{
while (ReadOK (fd, &v, 1))
{
......@@ -483,8 +504,7 @@ ReadImage (FILE *fd,
}
if (xpos == width)
{
unused = ReadOK (fd, buffer, rowbytes - 1 -
(width * bpp - 1) / 8);
ReadOK (fd, buffer, rowbytes - 1 - (width * bpp - 1) / 8);
ypos--;
xpos = 0;
......@@ -502,7 +522,7 @@ ReadImage (FILE *fd,
{
while (ypos >= 0 && xpos <= width)
{
unused = ReadOK (fd, buffer, 2);
ReadOK (fd, buffer, 2);
if ((guchar) buffer[0] != 0)
/* Count + Color - record */
{
......@@ -527,10 +547,10 @@ ReadImage (FILE *fd,
if (((guchar) buffer[0] == 0) && ((guchar) buffer[1] > 2))
/* uncompressed record */
{
wieviel = buffer[1];
for (j = 0; j < wieviel; j += (8 / bpp))
n = buffer[1];
for (j = 0; j < n; j += (8 / bpp))
{
unused = ReadOK (fd, &v, 1);
ReadOK (fd, &v, 1);
i = 1;
while ((i <= (8 / bpp)) && (xpos < width))
{
......@@ -543,11 +563,12 @@ ReadImage (FILE *fd,
}
}
if ((wieviel % 2) && (bpp==4))
wieviel++;
if ((n % 2) && (bpp==4))
n++;
if ((n / (8 / bpp)) % 2)
ReadOK (fd, &v, 1);
if ((wieviel / (8 / bpp)) % 2)
unused = ReadOK (fd, &v, 1);
/*if odd(x div (8 div bpp )) then blockread(f,z^,1);*/
}
if (((guchar) buffer[0] == 0) && ((guchar) buffer[1]==0))
......@@ -569,19 +590,20 @@ ReadImage (FILE *fd,
if (((guchar) buffer[0]==0) && ((guchar) buffer[1]==2))
/* Deltarecord */
{
unused = ReadOK (fd, buffer, 2);
ReadOK (fd, buffer, 2);
xpos += (guchar) buffer[0];
ypos -= (guchar) buffer[1];
}
}
break;
}
}
break;
default:
g_assert_not_reached ();
break;
}
break;
default:
/* This is very bad, we should not be here */
;
}
fclose (fd);
if (bpp <= 8)
......
......@@ -51,7 +51,16 @@ static gint max_progress = 0;
static gint encoded = 0;
static gint save_dialog (void);
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);
static void
......@@ -98,29 +107,31 @@ WriteBMP (const gchar *filename,
gint32 image,
gint32 drawable_ID)
{
FILE *outfile;
gint Red[MAXCOLORS];
gint Green[MAXCOLORS];
gint Blue[MAXCOLORS];
guchar *cmap;
gint rows, cols, Spcols, channels, MapSize, SpZeile;
glong BitsPerPixel;
gint colors;
gchar *temp_buf;
guchar *pixels;
GimpPixelRgn pixel_rgn;
FILE *outfile;
gint Red[MAXCOLORS];
gint Green[MAXCOLORS];
gint Blue[MAXCOLORS];
guchar *cmap;
gint rows, cols, Spcols, channels, MapSize, SpZeile;
glong BitsPerPixel;
gint colors;
gchar *temp_buf;
guchar *pixels;
GimpPixelRgn pixel_rgn;
GimpDrawable *drawable;
GimpImageType drawable_type;
guchar puffer[50];
gint i;
guchar puffer[50];
gint i;
/* first: can we save this image? */
drawable = gimp_drawable_get(drawable_ID);
drawable_type = gimp_drawable_type(drawable_ID);
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);
if (gimp_drawable_has_alpha(drawable_ID))
if (gimp_drawable_has_alpha (drawable_ID))
{
g_message (_("Cannot save images with alpha channel."));
return GIMP_PDB_EXECUTION_ERROR;
......@@ -316,7 +327,7 @@ WriteBMP (const gchar *filename,
return GIMP_PDB_SUCCESS;
}
void
static void
WriteImage (FILE *f,
guchar *src,
gint width,
......@@ -530,7 +541,7 @@ WriteImage (FILE *f,
gimp_progress_update (1);
}
static gint
static gboolean
save_dialog (void)
{
GtkWidget *dlg;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment