summaryrefslogtreecommitdiff
path: root/src/gimp2/print-image-gimp.c
blob: 52dc1754651f279b46db5dd8cef350574c8a170d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 *   Print plug-in for the GIMP.
 *
 *   Copyright 1997-2000 Michael Sweet (mike@easysw.com) and
 *	Robert Krawitz (rlk@alum.mit.edu)
 *   Copyright 2000 Charles Briscoe-Smith <cpbs@debian.org>
 *
 *   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, see <https://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>

#include "print_gimp.h"

#include "print-intl.h"


/*
 * "Image" ADT
 *
 * This file defines an abstract data type called "Image".  An Image wraps
 * a Gimp drawable (or some other application-level image representation)
 * for presentation to the low-level printer drivers (which do CMYK
 * separation, dithering and weaving).  The Image ADT has the ability
 * to perform any combination of flips and rotations on the image,
 * and then deliver individual rows to the driver code.
 *
 * Stuff which might be useful to do in this layer:
 *
 * - Scaling, optionally with interpolation/filtering.
 *
 * - Colour-adjustment.
 *
 * - Multiple-image composition.
 *
 * Also useful might be to break off a thin application-dependent
 * sublayer leaving this layer (which does the interesting stuff)
 * application-independent.
 */


/* Concrete type to represent image */
typedef struct
{
  GimpDrawable *drawable;
  GimpPixelRgn  rgn;

  /*
   * Transformations we can impose on the image.  The transformations
   * are considered to be performed in the order given here.
   */

  /* 1: Transpose the x and y axes (flip image over its leading diagonal) */
  int columns;		/* Set if returning columns instead of rows. */

  /* 2: Translate (ox,oy) to the origin */
  int ox, oy;		/* Origin of image */

  /* 3: Flip vertically about the x axis */
  int increment;	/* +1 or -1 for offset of row n+1 from row n. */

  /* 4: Crop to width w, height h */
  int w, h;		/* Width and height of output image */

  /* 5: Flip horizontally about the vertical centre-line of the image */
  int mirror;		/* Set if mirroring rows end-for-end. */

  gint32 image_ID;
  gint32 ncolors;
  gint32 real_bpp;
  GimpImageBaseType base_type;
  guchar *cmap;
  guchar *alpha_table;
  guchar *tmp;
  gint last_printed_percent;
  gint initialized;
} Gimp_Image_t;

static const char *Image_get_appname(stp_image_t *image);
static void Image_conclude(stp_image_t *image);
static stp_image_status_t Image_get_row(stp_image_t *image,
					unsigned char *data,
					size_t byte_limit, int row);
static int Image_height(stp_image_t *image);
static int Image_width(stp_image_t *image);
static void Image_reset(stp_image_t *image);
static void Image_init(stp_image_t *image);

static void Image_transpose(stpui_image_t *image);
static void Image_hflip(stpui_image_t *image);
static void Image_vflip(stpui_image_t *image);
static void Image_rotate_ccw(stpui_image_t *image);
static void Image_rotate_cw(stpui_image_t *image);
static void Image_rotate_180(stpui_image_t *image);
static void Image_crop(stpui_image_t *image, int, int, int, int);

static stpui_image_t theImage =
{
  {
    Image_init,
    Image_reset,
    Image_width,
    Image_height,
    Image_get_row,
    Image_get_appname,
    Image_conclude,
    NULL,
  },
  Image_transpose,
  Image_hflip,
  Image_vflip,
  Image_rotate_ccw,
  Image_rotate_cw,
  Image_rotate_180,
  Image_crop
};

static void
compute_alpha_table(Gimp_Image_t *image)
{
  unsigned val, alpha;
  image->alpha_table = stp_malloc(65536 * sizeof(unsigned char));
  for (val = 0; val < 256; val++)
    for (alpha = 0; alpha < 256; alpha++)
      image->alpha_table[(val * 256) + alpha] =
	val * alpha / 255 + 255 - alpha;
}

static inline unsigned char
alpha_lookup(Gimp_Image_t *image, int val, int alpha)
{
  return image->alpha_table[(val * 256) + alpha];
}

stpui_image_t *
Image_GimpDrawable_new(GimpDrawable *drawable, gint32 image_ID)
{
  Gimp_Image_t *im = stp_malloc(sizeof(Gimp_Image_t));
  memset(im, 0, sizeof(Gimp_Image_t));
  im->drawable = drawable;
  gimp_pixel_rgn_init(&(im->rgn), drawable, 0, 0,
                      drawable->width, drawable->height, FALSE, FALSE);
  im->image_ID = image_ID;
  im->base_type = gimp_image_base_type(image_ID);
  im->initialized = 0;
  theImage.im.rep = im;
  theImage.im.reset(&(theImage.im));
  switch (im->base_type)
    {
    case GIMP_INDEXED:
      im->cmap = gimp_image_get_cmap(image_ID, &(im->ncolors));
      im->real_bpp = 3;
      break;
    case GIMP_GRAY:
      im->real_bpp = 1;
      break;
    case GIMP_RGB:
      im->real_bpp = 3;
      break;
    }
  if (im->drawable->bpp == 2 || im->drawable->bpp == 4)
    compute_alpha_table(im);
  return &theImage;
}

static void
Image_init(stp_image_t *image)
{
  /* Nothing to do. */
}

static void
Image_reset(stp_image_t *image)
{
  Gimp_Image_t *im = (Gimp_Image_t *) (image->rep);
  im->columns = FALSE;
  im->ox = 0;
  im->oy = 0;
  im->increment = 1;
  im->w = im->drawable->width;
  im->h = im->drawable->height;
  im->mirror = FALSE;
}

static int
Image_width(stp_image_t *image)
{
  Gimp_Image_t *im = (Gimp_Image_t *) (image->rep);
  return im->w;
}

static int
Image_height(stp_image_t *image)
{
  Gimp_Image_t *im = (Gimp_Image_t *) (image->rep);
  return im->h;
}

static stp_image_status_t
Image_get_row(stp_image_t *image, unsigned char *data, size_t byte_limit,
	      int row)
{
  Gimp_Image_t *im = (Gimp_Image_t *) (image->rep);
  int last_printed_percent;
  guchar *inter;
  if (!im->initialized)
    {
      gimp_progress_init(_("Printing..."));
      switch (im->base_type)
	{
	case GIMP_INDEXED:
	  im->tmp = stp_malloc(im->drawable->bpp * im->w);
	  break;
	case GIMP_GRAY:
	  if (im->drawable->bpp == 2)
	    im->tmp = stp_malloc(im->drawable->bpp * im->w);
	  break;
	case GIMP_RGB:
	  if (im->drawable->bpp == 4)
	    im->tmp = stp_malloc(im->drawable->bpp * im->w);
	  break;
	}
      im->initialized = 1;
    }
  if (im->tmp)
    inter = im->tmp;
  else
    inter = data;
  if (im->columns)
    gimp_pixel_rgn_get_col(&(im->rgn), inter,
                           im->oy + row * im->increment, im->ox, im->w);
  else
    gimp_pixel_rgn_get_row(&(im->rgn), inter,
                           im->ox, im->oy + row * im->increment, im->w);
  if (im->cmap)
    {
      int i;
      if (im->alpha_table)
	{
	  for (i = 0; i < im->w; i++)
	    {
	      int j;
	      for (j = 0; j < 3; j++)
		{
		  gint32 tval = im->cmap[(3 * inter[2 * i]) + j];
		  data[(3 * i) + j] = alpha_lookup(im, tval,
						   inter[(2 * i) + 1]);
		}

	    }
	}
      else
	{
	  for (i = 0; i < im->w; i++)
	    {
	      data[(3 * i) + 0] = im->cmap[(3 * inter[i]) + 0];
	      data[(3 * i) + 1] = im->cmap[(3 * inter[i]) + 1];
	      data[(3 * i) + 2] = im->cmap[(3 * inter[i]) + 2];
	    }
	}
    }
  else if (im->alpha_table)
    {
      int i;
      for (i = 0; i < im->w; i++)
	{
	  int j;
	  for (j = 0; j < im->real_bpp; j++)
	    {
	      gint32 tval = inter[(i * im->drawable->bpp) + j];
	      data[(i * im->real_bpp) + j] =
		alpha_lookup(im, tval,
			     inter[((i + 1) * im->drawable->bpp) - 1]);
	    }

	}
    }
  if (im->mirror)
    {
      /* Flip row -- probably inefficiently */
      int f;
      int l;
      int b = im->real_bpp;
      for (f = 0, l = im->w - 1; f < l; f++, l--)
	{
	  int c;
	  unsigned char tmp;
	  for (c = 0; c < b; c++)
	    {
	      tmp = data[f*b+c];
	      data[f*b+c] = data[l*b+c];
	      data[l*b+c] = tmp;
	    }
	}
    }
  last_printed_percent = row * 100 / im->h;
  if (last_printed_percent > im->last_printed_percent)
    {
      gimp_progress_update((double) row / (double) im->h);
      im->last_printed_percent = last_printed_percent;
    }
  return STP_IMAGE_STATUS_OK;
}

static void
Image_transpose(stpui_image_t *image)
{
  Gimp_Image_t *im = (Gimp_Image_t *) (image->im.rep);
  int tmp;

  if (im->mirror) im->ox += im->w - 1;

  im->columns = !im->columns;

  tmp = im->ox;
  im->ox = im->oy;
  im->oy = tmp;

  tmp = im->mirror;
  im->mirror = im->increment < 0;
  im->increment = tmp ? -1 : 1;

  tmp = im->w;
  im->w = im->h;
  im->h = tmp;

  if (im->mirror) im->ox -= im->w - 1;
}

static void
Image_hflip(stpui_image_t *image)
{
  Gimp_Image_t *im = (Gimp_Image_t *) (image->im.rep);
  im->mirror = !im->mirror;
}

static void
Image_vflip(stpui_image_t *image)
{
  Gimp_Image_t *im = (Gimp_Image_t *) (image->im.rep);
  im->oy += (im->h-1) * im->increment;
  im->increment = -im->increment;
}

/*
 * Image_crop:
 *
 * Crop the given number of pixels off the LEFT, TOP, RIGHT and BOTTOM
 * of the image.
 */

static void
Image_crop(stpui_image_t *image, int left, int top, int right, int bottom)
{
  Gimp_Image_t *im = (Gimp_Image_t *) (image->im.rep);
  int xmax = (im->columns ? im->drawable->height : im->drawable->width) - 1;
  int ymax = (im->columns ? im->drawable->width : im->drawable->height) - 1;

  int nx = im->ox + im->mirror ? right : left;
  int ny = im->oy + top * (im->increment);

  int nw = im->w - left - right;
  int nh = im->h - top - bottom;

  int wmax, hmax;

  if (nx < 0)         nx = 0;
  else if (nx > xmax) nx = xmax;

  if (ny < 0)         ny = 0;
  else if (ny > ymax) ny = ymax;

  wmax = xmax - nx + 1;
  hmax = im->increment ? ny + 1 : ymax - ny + 1;

  if (nw < 1)         nw = 1;
  else if (nw > wmax) nw = wmax;

  if (nh < 1)         nh = 1;
  else if (nh > hmax) nh = hmax;

  im->ox = nx;
  im->oy = ny;
  im->w = nw;
  im->h = nh;
}

static void
Image_rotate_ccw(stpui_image_t *image)
{
  Image_transpose(image);
  Image_vflip(image);
}

static void
Image_rotate_cw(stpui_image_t *image)
{
  Image_transpose(image);
  Image_hflip(image);
}

static void
Image_rotate_180(stpui_image_t *image)
{
  Image_vflip(image);
  Image_hflip(image);
}

static void
Image_conclude(stp_image_t *image)
{
  Gimp_Image_t *im = (Gimp_Image_t *) (image->rep);
  gimp_progress_update(1);
  if (im->alpha_table)
    stp_free(im->alpha_table);
  if (im->tmp)
    stp_free(im->tmp);
}

static const char *
Image_get_appname(stp_image_t *image)
{
  static char pluginname[] = "Print plug-in V" VERSION " - " RELEASE_DATE
    " for GIMP";
  return pluginname;
}