1 /* bmp.c                                          */
2 /* Version 0.52                                   */
3 /* This is a File input and output filter for the */
4 /* Gimp. It loads and saves images in windows(TM) */
5 /* bitmap format.                                 */
6 /* Some Parts that deal with the interaction with */
7 /* GIMP are taken from the GIF plugin by          */
8 /* Peter Mattis & Spencer Kimball and from the    */
9 /* PCX plugin by Francisco Bustamante.            */
10 /*                                                */
11 /* Alexander.Schulz@stud.uni-karlsruhe.de         */
12 
13 /* Changes:   28.11.1997 Noninteractive operation */
14 /*            16.03.1998 Endian-independent!!     */
15 /*            21.03.1998 Little Bug-fix           */
16 /*            06.04.1998 Bugfix in Padding        */
17 /*            11.04.1998 Arch. cleanup (-Wall)    */
18 /*                       Parses gtkrc             */
19 /*            14.04.1998 Another Bug in Padding   */
20 /*            28.04.1998 RLE-Encoding rewritten   */
21 /*            29.10.1998 Changes by Tor Lillqvist */
22 /*                       <tml@iki.fi> to support  */
23 /*                       16 and 32 bit images     */
24 /*            28.11.1998 Bug in RLE-read-padding  */
25 /*                       fixed.                   */
26 /*            19.12.1999 Resolution support added */
27 /*            06.05.2000 Overhaul for 16&24-bit   */
28 /*                       plus better OS/2 code    */
29 /*                       by njl195@zepler.org.uk  */
30 /*            29.06.2006 Full support for 16/32   */
31 /*                       bits bitmaps and support */
32 /*                       for alpha channel        */
33 /*                       by p.filiciak@zax.pl     */
34 
35 /*
36  * GIMP - The GNU Image Manipulation Program
37  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
38  *
39  * This program is free software: you can redistribute it and/or modify
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
42  * (at your option) any later version.
43  *
44  * This program is distributed in the hope that it will be useful,
45  * but WITHOUT ANY WARRANTY; without even the implied warranty of
46  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47  * GNU General Public License for more details.
48  *
49  * 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/>.
51  * ----------------------------------------------------------------------------
52  */
53 
54 #include "config.h"
55 
56 #include <stdlib.h>
57 #include <string.h>
58 
59 #include <libgimp/gimp.h>
60 #include <libgimp/gimpui.h>
61 
62 #include "bmp.h"
63 #include "bmp-load.h"
64 #include "bmp-save.h"
65 
66 #include "libgimp/stdplugins-intl.h"
67 
68 
69 /* Declare some local functions.
70  */
71 static void   query (void);
72 static void   run   (const gchar      *name,
73                      gint              nparams,
74                      const GimpParam  *param,
75                      gint             *nreturn_vals,
76                      GimpParam       **return_vals);
77 
78 
79 const GimpPlugInInfo PLUG_IN_INFO =
80 {
81   NULL,  /* init_proc  */
82   NULL,  /* quit_proc  */
83   query, /* query_proc */
84   run,   /* run_proc   */
85 };
86 
87 
MAIN()88 MAIN ()
89 
90 
91 static void
92 query (void)
93 {
94   static const GimpParamDef load_args[] =
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" },
99   };
100   static const GimpParamDef load_return_vals[] =
101   {
102     { GIMP_PDB_IMAGE, "image", "Output image" },
103   };
104 
105   static const GimpParamDef save_args[] =
106   {
107     { GIMP_PDB_INT32,    "run-mode",     "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
108     { GIMP_PDB_IMAGE,    "image",        "Input image" },
109     { GIMP_PDB_DRAWABLE, "drawable",     "Drawable to save" },
110     { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },
111     { GIMP_PDB_STRING,   "raw-filename", "The name entered" },
112   };
113 
114   gimp_install_procedure (LOAD_PROC,
115                           "Loads files of Windows BMP file format",
116                           "Loads files of Windows BMP file format",
117                           "Alexander Schulz",
118                           "Alexander Schulz",
119                           "1997",
120                           N_("Windows BMP image"),
121                           NULL,
122                           GIMP_PLUGIN,
123                           G_N_ELEMENTS (load_args),
124                           G_N_ELEMENTS (load_return_vals),
125                           load_args, load_return_vals);
126 
127   gimp_register_file_handler_mime (LOAD_PROC, "image/bmp");
128   gimp_register_magic_load_handler (LOAD_PROC,
129                                     "bmp",
130                                     "",
131                                     "0,string,BM");
132 
133   gimp_install_procedure (SAVE_PROC,
134                           "Saves files in Windows BMP file format",
135                           "Saves files in Windows BMP file format",
136                           "Alexander Schulz",
137                           "Alexander Schulz",
138                           "1997",
139                           N_("Windows BMP image"),
140                           "INDEXED, GRAY, RGB*",
141                           GIMP_PLUGIN,
142                           G_N_ELEMENTS (save_args), 0,
143                           save_args, NULL);
144 
145   gimp_register_file_handler_mime (SAVE_PROC, "image/bmp");
146   gimp_register_save_handler (SAVE_PROC, "bmp", "");
147 }
148 
149 static void
run(const gchar * name,gint nparams,const GimpParam * param,gint * nreturn_vals,GimpParam ** return_vals)150 run (const gchar      *name,
151      gint              nparams,
152      const GimpParam  *param,
153      gint             *nreturn_vals,
154      GimpParam       **return_vals)
155 {
156   static GimpParam   values[2];
157   GimpRunMode        run_mode;
158   GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
159   GError            *error  = NULL;
160 
161   INIT_I18N ();
162   gegl_init (NULL, NULL);
163 
164   run_mode = param[0].data.d_int32;
165 
166   *nreturn_vals = 1;
167   *return_vals  = values;
168 
169   values[0].type          = GIMP_PDB_STATUS;
170   values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
171 
172   if (strcmp (name, LOAD_PROC) == 0)
173     {
174       switch (run_mode)
175         {
176         case GIMP_RUN_INTERACTIVE:
177           break;
178 
179         case GIMP_RUN_NONINTERACTIVE:
180           /*  Make sure all the arguments are there!  */
181           if (nparams != 3)
182             status = GIMP_PDB_CALLING_ERROR;
183           break;
184 
185         default:
186           break;
187         }
188 
189       if (status == GIMP_PDB_SUCCESS)
190         {
191           gint32 image_ID = load_image (param[1].data.d_string,
192                                         &error);
193 
194           if (image_ID != -1)
195             {
196               *nreturn_vals = 2;
197               values[1].type         = GIMP_PDB_IMAGE;
198               values[1].data.d_image = image_ID;
199             }
200           else
201             {
202               status = GIMP_PDB_EXECUTION_ERROR;
203             }
204         }
205     }
206   else if (strcmp (name, SAVE_PROC) == 0)
207     {
208       gint32           image_ID    = param[1].data.d_int32;
209       gint32           drawable_ID = param[2].data.d_int32;
210       GimpExportReturn export      = GIMP_EXPORT_CANCEL;
211 
212       /*  eventually export the image */
213       switch (run_mode)
214         {
215         case GIMP_RUN_INTERACTIVE:
216         case GIMP_RUN_WITH_LAST_VALS:
217           gimp_ui_init (PLUG_IN_BINARY, FALSE);
218 
219           export = gimp_export_image (&image_ID, &drawable_ID, "BMP",
220                                       GIMP_EXPORT_CAN_HANDLE_RGB   |
221                                       GIMP_EXPORT_CAN_HANDLE_GRAY  |
222                                       GIMP_EXPORT_CAN_HANDLE_ALPHA |
223                                       GIMP_EXPORT_CAN_HANDLE_INDEXED);
224 
225           if (export == GIMP_EXPORT_CANCEL)
226             {
227               values[0].data.d_status = GIMP_PDB_CANCEL;
228               return;
229             }
230           break;
231 
232         case GIMP_RUN_NONINTERACTIVE:
233           /*  Make sure all the arguments are there!  */
234           if (nparams != 5)
235             status = GIMP_PDB_CALLING_ERROR;
236           break;
237 
238         default:
239           break;
240         }
241 
242       if (status == GIMP_PDB_SUCCESS)
243         status = save_image (param[3].data.d_string,
244                              image_ID, drawable_ID,
245                              run_mode,
246                              &error);
247 
248       if (export == GIMP_EXPORT_EXPORT)
249         gimp_image_delete (image_ID);
250     }
251   else
252     {
253       status = GIMP_PDB_CALLING_ERROR;
254     }
255 
256   if (status != GIMP_PDB_SUCCESS && error)
257     {
258       *nreturn_vals = 2;
259       values[1].type          = GIMP_PDB_STRING;
260       values[1].data.d_string = error->message;
261     }
262 
263   values[0].data.d_status = status;
264 }
265