~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to plug-ins/winicon/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * GIMP Plug-in for Windows Icon files.
 
5
 * Copyright (C) 2002 Christian Kreibich <christian@whoop.org>.
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <string.h>
 
25
#include <stdio.h>
 
26
 
 
27
#include <libgimp/gimp.h>
 
28
#include <libgimp/gimpui.h>
 
29
 
 
30
/* #define ICO_DBG */
 
31
 
 
32
#include "main.h"
 
33
#include "icoload.h"
 
34
#include "icosave.h"
 
35
 
 
36
#include "libgimp/stdplugins-intl.h"
 
37
 
 
38
 
 
39
static void   query (void);
 
40
static void   run   (const gchar      *name,
 
41
                     gint              nparams,
 
42
                     const GimpParam  *param,
 
43
                     gint             *nreturn_vals,
 
44
                     GimpParam       **return_vals);
 
45
 
 
46
 
 
47
GimpPlugInInfo PLUG_IN_INFO =
 
48
{
 
49
  NULL,  /* init_proc  */
 
50
  NULL,  /* quit_proc  */
 
51
  query, /* query_proc */
 
52
  run,   /* run_proc   */
 
53
};
 
54
 
 
55
 
 
56
MAIN ()
 
57
 
 
58
 
 
59
static void
 
60
query (void)
 
61
{
 
62
  static GimpParamDef load_args[] =
 
63
  {
 
64
    { GIMP_PDB_INT32,    "run_mode",     "Interactive, non-interactive" },
 
65
    { GIMP_PDB_STRING,   "filename",     "The name of the file to load" },
 
66
    { GIMP_PDB_STRING,   "raw_filename", "The name entered"             }
 
67
  };
 
68
  static GimpParamDef load_return_vals[] =
 
69
  {
 
70
    { GIMP_PDB_IMAGE, "image", "Output image" },
 
71
  };
 
72
 
 
73
  static GimpParamDef save_args[] =
 
74
  {
 
75
    { GIMP_PDB_INT32,    "run_mode",     "Interactive, non-interactive" },
 
76
    { GIMP_PDB_IMAGE,    "image",        "Input image" },
 
77
    { GIMP_PDB_DRAWABLE, "drawable",     "Drawable to save" },
 
78
    { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },
 
79
    { GIMP_PDB_STRING,   "raw_filename", "The name entered" },
 
80
  };
 
81
 
 
82
  gimp_install_procedure ("file_ico_load",
 
83
                          "Loads files of Windows ICO file format",
 
84
                          "Loads files of Windows ICO file format",
 
85
                          "Christian Kreibich <christian@whoop.org>",
 
86
                          "Christian Kreibich <christian@whoop.org>",
 
87
                          "2002",
 
88
                          N_("Microsoft Windows icon"),
 
89
                          NULL,
 
90
                          GIMP_PLUGIN,
 
91
                          G_N_ELEMENTS (load_args),
 
92
                          G_N_ELEMENTS (load_return_vals),
 
93
                          load_args, load_return_vals);
 
94
 
 
95
  gimp_register_file_handler_mime ("file_ico_load", "image/x-ico");
 
96
  gimp_register_magic_load_handler ("file_ico_load",
 
97
                                    "ico",
 
98
                                    "",
 
99
                                    "0,string,\\000\\001\\000\\000,0,string,\\000\\002\\000\\000");
 
100
 
 
101
  gimp_install_procedure ("file_ico_save",
 
102
                          "Saves files in Windows ICO file format",
 
103
                          "Saves files in Windows ICO file format",
 
104
                          "Christian Kreibich <christian@whoop.org>",
 
105
                          "Christian Kreibich <christian@whoop.org>",
 
106
                          "2002",
 
107
                          N_("Microsoft Windows icon"),
 
108
                          "*",
 
109
                          GIMP_PLUGIN,
 
110
                          G_N_ELEMENTS (save_args), 0,
 
111
                          save_args, NULL);
 
112
 
 
113
  gimp_register_file_handler_mime ("file_ico_save", "image/x-ico");
 
114
  gimp_register_save_handler ("file_ico_save", "ico", "");
 
115
}
 
116
 
 
117
static void
 
118
run (const gchar      *name,
 
119
     gint              nparams,
 
120
     const GimpParam  *param,
 
121
     gint             *nreturn_vals,
 
122
     GimpParam       **return_vals)
 
123
{
 
124
  static GimpParam   values[2];
 
125
  gint32             image_ID;
 
126
  gint32             drawable_ID;
 
127
  GimpRunMode        run_mode;
 
128
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
 
129
  GimpExportReturn   export = GIMP_EXPORT_CANCEL;
 
130
 
 
131
  INIT_I18N ();
 
132
 
 
133
  run_mode = param[0].data.d_int32;
 
134
 
 
135
  *nreturn_vals = 1;
 
136
  *return_vals  = values;
 
137
  values[0].type          = GIMP_PDB_STATUS;
 
138
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
 
139
 
 
140
  if (strcmp (name, "file_ico_load") == 0)
 
141
    {
 
142
      switch (run_mode)
 
143
        {
 
144
        case GIMP_RUN_INTERACTIVE:
 
145
          break;
 
146
 
 
147
        case GIMP_RUN_NONINTERACTIVE:
 
148
          if (nparams != 3)
 
149
            status = GIMP_PDB_CALLING_ERROR;
 
150
          break;
 
151
 
 
152
        default:
 
153
          break;
 
154
        }
 
155
 
 
156
      if (status == GIMP_PDB_SUCCESS)
 
157
        {
 
158
          image_ID = LoadICO (param[1].data.d_string);
 
159
 
 
160
          if (image_ID != -1)
 
161
            {
 
162
              *nreturn_vals = 2;
 
163
              values[1].type         = GIMP_PDB_IMAGE;
 
164
              values[1].data.d_image = image_ID;
 
165
            }
 
166
          else
 
167
            {
 
168
              status = GIMP_PDB_EXECUTION_ERROR;
 
169
            }
 
170
        }
 
171
    }
 
172
  else if (strcmp (name, "file_ico_save") == 0)
 
173
    {
 
174
      gchar *file_name;
 
175
 
 
176
      image_ID    = param[1].data.d_int32;
 
177
      drawable_ID = param[2].data.d_int32;
 
178
      file_name   = param[3].data.d_string;
 
179
 
 
180
      switch (run_mode)
 
181
        {
 
182
        case GIMP_RUN_INTERACTIVE:
 
183
          break;
 
184
 
 
185
        case GIMP_RUN_NONINTERACTIVE:
 
186
          /*  Make sure all the arguments are there!  */
 
187
          if (nparams < 5)
 
188
            status = GIMP_PDB_CALLING_ERROR;
 
189
          break;
 
190
 
 
191
        case GIMP_RUN_WITH_LAST_VALS:
 
192
          break;
 
193
 
 
194
        default:
 
195
          break;
 
196
        }
 
197
 
 
198
      if (status == GIMP_PDB_SUCCESS)
 
199
        {
 
200
          status = SaveICO (file_name, image_ID);
 
201
        }
 
202
 
 
203
      if (export == GIMP_EXPORT_EXPORT)
 
204
        gimp_image_delete (image_ID);
 
205
    }
 
206
  else
 
207
    {
 
208
      status = GIMP_PDB_CALLING_ERROR;
 
209
    }
 
210
 
 
211
  values[0].type          = GIMP_PDB_STATUS;
 
212
  values[0].data.d_status = status;
 
213
}
 
214
 
 
215
 
 
216
guint8 *
 
217
ico_alloc_map (gint  width,
 
218
               gint  height,
 
219
               gint  bpp,
 
220
               gint *length)
 
221
{
 
222
  gint    len = 0;
 
223
  guint8 *map = NULL;
 
224
 
 
225
  switch (bpp)
 
226
    {
 
227
    case 1:
 
228
      if ((width % 32) == 0)
 
229
        len = (width * height / 8);
 
230
      else
 
231
        len = 4 * ((width/32 + 1) * height);
 
232
      break;
 
233
 
 
234
    case 4:
 
235
      if ((width % 8) == 0)
 
236
        len = (width * height / 2);
 
237
      else
 
238
        len = 4 * ((width/8 + 1) * height);
 
239
      break;
 
240
 
 
241
    case 8:
 
242
      if ((width % 4) == 0)
 
243
        len = width * height;
 
244
      else
 
245
        len = 4 * ((width/4 + 1) * height);
 
246
      break;
 
247
 
 
248
    default:
 
249
      len = width * height * (bpp/8);
 
250
    }
 
251
 
 
252
  *length = len;
 
253
  map = g_new0 (guint8, len);
 
254
 
 
255
  return map;
 
256
}
 
257
 
 
258
 
 
259
void
 
260
ico_cleanup (MsIcon *ico)
 
261
{
 
262
  gint i;
 
263
 
 
264
  if (!ico)
 
265
    return;
 
266
 
 
267
  if (ico->fp)
 
268
    fclose (ico->fp);
 
269
 
 
270
  if (ico->icon_dir)
 
271
    g_free (ico->icon_dir);
 
272
 
 
273
  if (ico->icon_data)
 
274
    {
 
275
      for (i = 0; i < ico->icon_count; i++)
 
276
        {
 
277
          g_free (ico->icon_data[i].palette);
 
278
          g_free (ico->icon_data[i].xor_map);
 
279
          g_free (ico->icon_data[i].and_map);
 
280
        }
 
281
      g_free (ico->icon_data);
 
282
    }
 
283
}