bmp.c 8.5 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
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
39
 * This program is free software: you can redistribute it and/or modify
Manish Singh's avatar
Manish Singh committed
40
 * it under the terms of the GNU General Public License as published by
41
 * the Free Software Foundation; either version 3 of the License, or
Manish Singh's avatar
Manish Singh committed
42 43 44 45 46 47 48 49
 * (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
50
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Manish Singh's avatar
Manish Singh committed
51 52 53
 * ----------------------------------------------------------------------------
 */

54
#include "config.h"
55

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

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

Elliot Lee's avatar
Elliot Lee committed
62
#include "bmp.h"
63 64
#include "bmp-load.h"
#include "bmp-save.h"
65

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

68

Elliot Lee's avatar
Elliot Lee committed
69 70
/* Declare some local functions.
 */
71 72 73 74 75 76
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
77

78

79
const GimpPlugInInfo PLUG_IN_INFO =
Elliot Lee's avatar
Elliot Lee committed
80
{
81 82 83 84
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
Elliot Lee's avatar
Elliot Lee committed
85 86
};

87

Adrian Likins's avatar
Adrian Likins committed
88
MAIN ()
Elliot Lee's avatar
Elliot Lee committed
89

90

Elliot Lee's avatar
Elliot Lee committed
91
static void
92
query (void)
Elliot Lee's avatar
Elliot Lee committed
93
{
94
  static const GimpParamDef load_args[] =
Elliot Lee's avatar
Elliot Lee committed
95
  {
96
    { GIMP_PDB_INT32,    "run-mode",     "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
97
    { GIMP_PDB_STRING,   "filename",     "The name of the file to load" },
98
    { GIMP_PDB_STRING,   "raw-filename", "The name entered" },
Elliot Lee's avatar
Elliot Lee committed
99
  };
100
  static const GimpParamDef load_return_vals[] =
Elliot Lee's avatar
Elliot Lee committed
101
  {
102
    { GIMP_PDB_IMAGE, "image", "Output image" },
Elliot Lee's avatar
Elliot Lee committed
103 104
  };

105
  static const GimpParamDef save_args[] =
Elliot Lee's avatar
Elliot Lee committed
106
  {
107
    { GIMP_PDB_INT32,    "run-mode",     "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
108 109 110
    { 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" },
111
    { GIMP_PDB_STRING,   "raw-filename", "The name entered" },
Elliot Lee's avatar
Elliot Lee committed
112
  };
113

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

127 128
  gimp_register_file_handler_mime (LOAD_PROC, "image/bmp");
  gimp_register_magic_load_handler (LOAD_PROC,
129 130 131
                                    "bmp",
                                    "",
                                    "0,string,BM");
132

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

145 146
  gimp_register_file_handler_mime (SAVE_PROC, "image/bmp");
  gimp_register_save_handler (SAVE_PROC, "bmp", "");
Elliot Lee's avatar
Elliot Lee committed
147 148 149
}

static void
150
run (const gchar      *name,
151
     gint              nparams,
152 153 154
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
Elliot Lee's avatar
Elliot Lee committed
155
{
156 157 158 159
  static GimpParam   values[2];
  GimpRunMode        run_mode;
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
  GError            *error  = NULL;
160

161
  INIT_I18N ();
162 163
  gegl_init (NULL, NULL);

164
  run_mode = param[0].data.d_int32;
165

Elliot Lee's avatar
Elliot Lee committed
166
  *nreturn_vals = 1;
167
  *return_vals  = values;
168

169 170
  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
Elliot Lee's avatar
Elliot Lee committed
171

172
  if (strcmp (name, LOAD_PROC) == 0)
Elliot Lee's avatar
Elliot Lee committed
173
    {
174
      switch (run_mode)
Manish Singh's avatar
Manish Singh committed
175
        {
176
        case GIMP_RUN_INTERACTIVE:
Manish Singh's avatar
Manish Singh committed
177 178
          break;

179
        case GIMP_RUN_NONINTERACTIVE:
Manish Singh's avatar
Manish Singh committed
180
          /*  Make sure all the arguments are there!  */
181
          if (nparams != 3)
182
            status = GIMP_PDB_CALLING_ERROR;
Manish Singh's avatar
Manish Singh committed
183 184 185 186 187 188
          break;

        default:
          break;
        }

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
      if (status == GIMP_PDB_SUCCESS)
        {
          gint32 image_ID = load_image (param[1].data.d_string,
                                        &error);

          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
205
    }
206
  else if (strcmp (name, SAVE_PROC) == 0)
Elliot Lee's avatar
Elliot Lee committed
207
    {
208 209 210
      gint32           image_ID    = param[1].data.d_int32;
      gint32           drawable_ID = param[2].data.d_int32;
      GimpExportReturn export      = GIMP_EXPORT_CANCEL;
211

212
      /*  eventually export the image */
213
      switch (run_mode)
214 215 216
        {
        case GIMP_RUN_INTERACTIVE:
        case GIMP_RUN_WITH_LAST_VALS:
217
          gimp_ui_init (PLUG_IN_BINARY, FALSE);
218 219 220 221 222 223 224

          export = gimp_export_image (&image_ID, &drawable_ID, "BMP",
                                      GIMP_EXPORT_CAN_HANDLE_RGB   |
                                      GIMP_EXPORT_CAN_HANDLE_GRAY  |
                                      GIMP_EXPORT_CAN_HANDLE_ALPHA |
                                      GIMP_EXPORT_CAN_HANDLE_INDEXED);

225 226 227 228 229 230
          if (export == GIMP_EXPORT_CANCEL)
            {
              values[0].data.d_status = GIMP_PDB_CANCEL;
              return;
            }
          break;
Elliot Lee's avatar
Elliot Lee committed
231

232
        case GIMP_RUN_NONINTERACTIVE:
Elliot Lee's avatar
Elliot Lee committed
233
          /*  Make sure all the arguments are there!  */
234
          if (nparams != 5)
235
            status = GIMP_PDB_CALLING_ERROR;
Elliot Lee's avatar
Elliot Lee committed
236 237 238 239 240 241
          break;

        default:
          break;
        }

242
      if (status == GIMP_PDB_SUCCESS)
243 244 245 246
        status = save_image (param[3].data.d_string,
                             image_ID, drawable_ID,
                             run_mode,
                             &error);
247

248
      if (export == GIMP_EXPORT_EXPORT)
249
        gimp_image_delete (image_ID);
Elliot Lee's avatar
Elliot Lee committed
250
    }
251 252
  else
    {
253
      status = GIMP_PDB_CALLING_ERROR;
254 255
    }

256 257 258 259 260 261 262
  if (status != GIMP_PDB_SUCCESS && error)
    {
      *nreturn_vals = 2;
      values[1].type          = GIMP_PDB_STRING;
      values[1].data.d_string = error->message;
    }

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