bmp.c 8.44 KB
Newer Older
Manish Singh's avatar
Manish Singh committed
1
/* bmp.c                                          */
2
/* Version 0.52                                   */
3
/* This is a File input and output filter for the */
Elliot Lee's avatar
Elliot Lee committed
4 5 6
/* Gimp. It loads and saves images in windows(TM) */
/* bitmap format.                                 */
/* Some Parts that deal with the interaction with */
7
/* GIMP are taken from the GIF plugin by          */
Elliot Lee's avatar
Elliot Lee committed
8 9 10 11 12
/* Peter Mattis & Spencer Kimball and from the    */
/* PCX plugin by Francisco Bustamante.            */
/*                                                */
/* Alexander.Schulz@stud.uni-karlsruhe.de         */

Manish Singh's avatar
Manish Singh committed
13 14
/* Changes:   28.11.1997 Noninteractive operation */
/*            16.03.1998 Endian-independent!!     */
15
/*            21.03.1998 Little Bug-fix           */
16
/*            06.04.1998 Bugfix in Padding        */
17 18
/*            11.04.1998 Arch. cleanup (-Wall)    */
/*                       Parses gtkrc             */
Sven Neumann's avatar
Sven Neumann committed
19
/*            14.04.1998 Another Bug in Padding   */
20 21 22 23 24 25
/*            28.04.1998 RLE-Encoding rewritten   */
/*            29.10.1998 Changes by Tor Lillqvist */
/*                       <tml@iki.fi> to support  */
/*                       16 and 32 bit images     */
/*            28.11.1998 Bug in RLE-read-padding  */
/*                       fixed.                   */
26
/*            19.12.1999 Resolution support added */
27 28 29
/*            06.05.2000 Overhaul for 16&24-bit   */
/*                       plus better OS/2 code    */
/*                       by njl195@zepler.org.uk  */
30 31 32 33
/*            29.06.2006 Full support for 16/32   */
/*                       bits bitmaps and support */
/*                       for alpha channel        */
/*                       by p.filiciak@zax.pl     */
34

35
/*
36
 * GIMP - The GNU Image Manipulation Program
Manish Singh's avatar
Manish Singh committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50
 * 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
51
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Manish Singh's avatar
Manish Singh committed
52 53 54
 * ----------------------------------------------------------------------------
 */

55
#include "config.h"
56

Elliot Lee's avatar
Elliot Lee committed
57
#include <stdlib.h>
58 59 60 61 62
#include <string.h>

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

Elliot Lee's avatar
Elliot Lee committed
63
#include "bmp.h"
64

65
#include "libgimp/stdplugins-intl.h"
Elliot Lee's avatar
Elliot Lee committed
66

67

68 69
const gchar *filename    = NULL;
gboolean     interactive = FALSE;
70
gboolean     lastvals    = FALSE;
Elliot Lee's avatar
Elliot Lee committed
71

72
struct Bitmap_File_Head_Struct Bitmap_File_Head;
73 74
struct Bitmap_Head_Struct      Bitmap_Head;

Elliot Lee's avatar
Elliot Lee committed
75 76 77

/* Declare some local functions.
 */
78 79 80 81 82 83
static void   query (void);
static void   run   (const gchar      *name,
                     gint              nparams,
                     const GimpParam  *param,
                     gint             *nreturn_vals,
                     GimpParam       **return_vals);
Elliot Lee's avatar
Elliot Lee committed
84

85
const GimpPlugInInfo PLUG_IN_INFO =
Elliot Lee's avatar
Elliot Lee committed
86
{
87 88 89 90
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
Elliot Lee's avatar
Elliot Lee committed
91 92
};

Adrian Likins's avatar
Adrian Likins committed
93
MAIN ()
Elliot Lee's avatar
Elliot Lee committed
94 95

static void
96
query (void)
Elliot Lee's avatar
Elliot Lee committed
97
{
98
  static const GimpParamDef load_args[] =
Elliot Lee's avatar
Elliot Lee committed
99
  {
100
    { GIMP_PDB_INT32,    "run-mode",     "Interactive, non-interactive" },
101
    { GIMP_PDB_STRING,   "filename",     "The name of the file to load" },
102
    { GIMP_PDB_STRING,   "raw-filename", "The name entered" },
Elliot Lee's avatar
Elliot Lee committed
103
  };
104
  static const GimpParamDef load_return_vals[] =
Elliot Lee's avatar
Elliot Lee committed
105
  {
106
    { GIMP_PDB_IMAGE, "image", "Output image" },
Elliot Lee's avatar
Elliot Lee committed
107 108
  };

109
  static const GimpParamDef save_args[] =
Elliot Lee's avatar
Elliot Lee committed
110
  {
111
    { GIMP_PDB_INT32,    "run-mode",     "Interactive, non-interactive" },
112 113 114
    { GIMP_PDB_IMAGE,    "image",        "Input image" },
    { GIMP_PDB_DRAWABLE, "drawable",     "Drawable to save" },
    { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },
115
    { GIMP_PDB_STRING,   "raw-filename", "The name entered" },
Elliot Lee's avatar
Elliot Lee committed
116
  };
117

118
  gimp_install_procedure (LOAD_PROC,
Marc Lehmann's avatar
Marc Lehmann committed
119 120
                          "Loads files of Windows BMP file format",
                          "Loads files of Windows BMP file format",
Elliot Lee's avatar
Elliot Lee committed
121 122 123
                          "Alexander Schulz",
                          "Alexander Schulz",
                          "1997",
124
                          N_("Windows BMP image"),
Elliot Lee's avatar
Elliot Lee committed
125
                          NULL,
126
                          GIMP_PLUGIN,
127 128
                          G_N_ELEMENTS (load_args),
                          G_N_ELEMENTS (load_return_vals),
Elliot Lee's avatar
Elliot Lee committed
129 130
                          load_args, load_return_vals);

131 132
  gimp_register_file_handler_mime (LOAD_PROC, "image/bmp");
  gimp_register_magic_load_handler (LOAD_PROC,
133 134 135
                                    "bmp",
                                    "",
                                    "0,string,BM");
136

137
  gimp_install_procedure (SAVE_PROC,
Marc Lehmann's avatar
Marc Lehmann committed
138 139
                          "Saves files in Windows BMP file format",
                          "Saves files in Windows BMP file format",
Elliot Lee's avatar
Elliot Lee committed
140 141 142
                          "Alexander Schulz",
                          "Alexander Schulz",
                          "1997",
143
                          N_("Windows BMP image"),
144
                          "INDEXED, GRAY, RGB*",
145
                          GIMP_PLUGIN,
146
                          G_N_ELEMENTS (save_args), 0,
Elliot Lee's avatar
Elliot Lee committed
147 148
                          save_args, NULL);

149 150
  gimp_register_file_handler_mime (SAVE_PROC, "image/bmp");
  gimp_register_save_handler (SAVE_PROC, "bmp", "");
Elliot Lee's avatar
Elliot Lee committed
151 152 153
}

static void
154 155 156 157 158
run (const gchar      *name,
     gint             nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
Elliot Lee's avatar
Elliot Lee committed
159
{
160 161 162 163 164 165 166
  static GimpParam  values[2];
  GimpRunMode       run_mode;
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  gint32            image_ID;
  gint32            drawable_ID;
  GimpExportReturn  export = GIMP_EXPORT_CANCEL;

Elliot Lee's avatar
Elliot Lee committed
167 168
  run_mode = param[0].data.d_int32;

169 170
  INIT_I18N ();

Elliot Lee's avatar
Elliot Lee committed
171
  *nreturn_vals = 1;
172
  *return_vals  = values;
173 174
  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
Elliot Lee's avatar
Elliot Lee committed
175

176
  if (strcmp (name, LOAD_PROC) == 0)
Elliot Lee's avatar
Elliot Lee committed
177
    {
Manish Singh's avatar
Manish Singh committed
178 179
       switch (run_mode)
        {
180
        case GIMP_RUN_INTERACTIVE:
181
          interactive = TRUE;
Manish Singh's avatar
Manish Singh committed
182 183
          break;

184
        case GIMP_RUN_NONINTERACTIVE:
Manish Singh's avatar
Manish Singh committed
185
          /*  Make sure all the arguments are there!  */
186
          if (nparams != 3)
187
            status = GIMP_PDB_CALLING_ERROR;
Manish Singh's avatar
Manish Singh committed
188 189 190 191 192 193
          break;

        default:
          break;
        }

194
       if (status == GIMP_PDB_SUCCESS)
195 196 197 198 199 200 201 202 203 204 205 206 207 208
         {
           image_ID = ReadBMP (param[1].data.d_string);

           if (image_ID != -1)
             {
               *nreturn_vals = 2;
               values[1].type         = GIMP_PDB_IMAGE;
               values[1].data.d_image = image_ID;
             }
           else
             {
               status = GIMP_PDB_EXECUTION_ERROR;
             }
         }
Elliot Lee's avatar
Elliot Lee committed
209
    }
210
  else if (strcmp (name, SAVE_PROC) == 0)
Elliot Lee's avatar
Elliot Lee committed
211
    {
212 213 214
      image_ID    = param[1].data.d_int32;
      drawable_ID = param[2].data.d_int32;

215
      /*  eventually export the image */
216
      switch (run_mode)
217 218
        {
        case GIMP_RUN_INTERACTIVE:
219 220 221
          interactive = TRUE;
          /* fallthrough */

222
        case GIMP_RUN_WITH_LAST_VALS:
223
          if (run_mode == GIMP_RUN_WITH_LAST_VALS) lastvals = TRUE;
224
          gimp_ui_init (PLUG_IN_BINARY, FALSE);
225 226 227 228 229
          export = gimp_export_image (&image_ID, &drawable_ID, "BMP",
                                      (GIMP_EXPORT_CAN_HANDLE_RGB |
                                       GIMP_EXPORT_CAN_HANDLE_ALPHA |
                                       GIMP_EXPORT_CAN_HANDLE_GRAY |
                                       GIMP_EXPORT_CAN_HANDLE_INDEXED));
230

231 232 233 234 235 236
          if (export == GIMP_EXPORT_CANCEL)
            {
              values[0].data.d_status = GIMP_PDB_CANCEL;
              return;
            }
          break;
Elliot Lee's avatar
Elliot Lee committed
237

238
        case GIMP_RUN_NONINTERACTIVE:
Elliot Lee's avatar
Elliot Lee committed
239
          /*  Make sure all the arguments are there!  */
240
          if (nparams != 5)
241
            status = GIMP_PDB_CALLING_ERROR;
Elliot Lee's avatar
Elliot Lee committed
242 243 244 245 246 247
          break;

        default:
          break;
        }

248
      if (status == GIMP_PDB_SUCCESS)
249
        status = WriteBMP (param[3].data.d_string, image_ID, drawable_ID);
250

251
      if (export == GIMP_EXPORT_EXPORT)
252
        gimp_image_delete (image_ID);
Elliot Lee's avatar
Elliot Lee committed
253
    }
254 255
  else
    {
256
      status = GIMP_PDB_CALLING_ERROR;
257 258 259
    }

  values[0].data.d_status = status;
Elliot Lee's avatar
Elliot Lee committed
260
}