home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / print / print-image-gimp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  6.7 KB  |  296 lines

  1. /*
  2.  * "$Id: print-image-gimp.c,v 1.2 2000/10/04 01:47:59 mitch Exp $"
  3.  *
  4.  *   Print plug-in for the GIMP.
  5.  *
  6.  *   Copyright 1997-2000 Michael Sweet (mike@easysw.com) and
  7.  *    Robert Krawitz (rlk@alum.mit.edu)
  8.  *   Copyright 2000 Charles Briscoe-Smith <cpbs@debian.org>
  9.  *
  10.  *   This program is free software; you can redistribute it and/or modify it
  11.  *   under the terms of the GNU General Public License as published by the Free
  12.  *   Software Foundation; either version 2 of the License, or (at your option)
  13.  *   any later version.
  14.  *
  15.  *   This program is distributed in the hope that it will be useful, but
  16.  *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17.  *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  18.  *   for more details.
  19.  *
  20.  *   You should have received a copy of the GNU General Public License
  21.  *   along with this program; if not, write to the Free Software
  22.  *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23.  */
  24.  
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28.  
  29. #include "print_gimp.h"
  30.  
  31. #include "print-intl.h"
  32.  
  33.  
  34. /*
  35.  * "Image" ADT
  36.  *
  37.  * This file defines an abstract data type called "Image".  An Image wraps
  38.  * a Gimp drawable (or some other application-level image representation)
  39.  * for presentation to the low-level printer drivers (which do CMYK
  40.  * separation, dithering and weaving).  The Image ADT has the ability
  41.  * to perform any combination of flips and rotations on the image,
  42.  * and then deliver individual rows to the driver code.
  43.  *
  44.  * Stuff which might be useful to do in this layer:
  45.  *
  46.  * - Scaling, optionally with interpolation/filtering.
  47.  *
  48.  * - Colour-adjustment.
  49.  *
  50.  * - Multiple-image composition.
  51.  *
  52.  * Also useful might be to break off a thin application-dependent
  53.  * sublayer leaving this layer (which does the interesting stuff)
  54.  * application-independent.
  55.  */
  56.  
  57.  
  58. /* Concrete type to represent image */
  59. typedef struct
  60. {
  61.   GDrawable *drawable;
  62.   GPixelRgn  rgn;
  63.  
  64.   /*
  65.    * Transformations we can impose on the image.  The transformations
  66.    * are considered to be performed in the order given here.
  67.    */
  68.  
  69.   /* 1: Transpose the x and y axes (flip image over its leading diagonal) */
  70.   int columns;        /* Set if returning columns instead of rows. */
  71.  
  72.   /* 2: Translate (ox,oy) to the origin */
  73.   int ox, oy;        /* Origin of image */
  74.  
  75.   /* 3: Flip vertically about the x axis */
  76.   int increment;    /* +1 or -1 for offset of row n+1 from row n. */
  77.  
  78.   /* 4: Crop to width w, height h */
  79.   int w, h;        /* Width and height of output image */
  80.  
  81.   /* 5: Flip horizontally about the vertical centre-line of the image */
  82.   int mirror;        /* Set if mirroring rows end-for-end. */
  83.  
  84. } Gimp_Image_t;
  85.  
  86.  
  87. Image
  88. Image_GDrawable_new(GDrawable *drawable)
  89. {
  90.   Gimp_Image_t *i = malloc(sizeof(Gimp_Image_t));
  91.   i->drawable = drawable;
  92.   gimp_pixel_rgn_init(&(i->rgn), drawable, 0, 0,
  93.                       drawable->width, drawable->height, FALSE, FALSE);
  94.   Image_reset((Image) i);
  95.   return i;
  96. }
  97.  
  98. void
  99. Image_init(Image image)
  100. {
  101.   /* Nothing to do. */
  102. }
  103.  
  104. void
  105. Image_reset(Image image)
  106. {
  107.   Gimp_Image_t *i = (Gimp_Image_t *) image;
  108.   i->columns = FALSE;
  109.   i->ox = 0;
  110.   i->oy = 0;
  111.   i->increment = 1;
  112.   i->w = i->drawable->width;
  113.   i->h = i->drawable->height;
  114.   i->mirror = FALSE;
  115. }
  116.  
  117. void
  118. Image_transpose(Image image)
  119. {
  120.   Gimp_Image_t *i = (Gimp_Image_t *) image;
  121.   int tmp;
  122.  
  123.   if (i->mirror) i->ox += i->w - 1;
  124.  
  125.   i->columns = !i->columns;
  126.  
  127.   tmp = i->ox;
  128.   i->ox = i->oy;
  129.   i->oy = tmp;
  130.  
  131.   tmp = i->mirror;
  132.   i->mirror = i->increment < 0;
  133.   i->increment = tmp ? -1 : 1;
  134.  
  135.   tmp = i->w;
  136.   i->w = i->h;
  137.   i->h = tmp;
  138.  
  139.   if (i->mirror) i->ox -= i->w - 1;
  140. }
  141.  
  142. void
  143. Image_hflip(Image image)
  144. {
  145.   Gimp_Image_t *i = (Gimp_Image_t *) image;
  146.   i->mirror = !i->mirror;
  147. }
  148.  
  149. void
  150. Image_vflip(Image image)
  151. {
  152.   Gimp_Image_t *i = (Gimp_Image_t *) image;
  153.   i->oy += (i->h-1) * i->increment;
  154.   i->increment = -i->increment;
  155. }
  156.  
  157. /*
  158.  * Image_crop:
  159.  *
  160.  * Crop the given number of pixels off the LEFT, TOP, RIGHT and BOTTOM
  161.  * of the image.
  162.  */
  163.  
  164. void
  165. Image_crop(Image image, int left, int top, int right, int bottom)
  166. {
  167.   Gimp_Image_t *i = (Gimp_Image_t *) image;
  168.   int xmax = (i->columns ? i->drawable->height : i->drawable->width) - 1;
  169.   int ymax = (i->columns ? i->drawable->width : i->drawable->height) - 1;
  170.  
  171.   int nx = i->ox + i->mirror ? right : left;
  172.   int ny = i->oy + top * (i->increment);
  173.  
  174.   int nw = i->w - left - right;
  175.   int nh = i->h - top - bottom;
  176.  
  177.   int wmax, hmax;
  178.  
  179.   if (nx < 0)         nx = 0;
  180.   else if (nx > xmax) nx = xmax;
  181.  
  182.   if (ny < 0)         ny = 0;
  183.   else if (ny > ymax) ny = ymax;
  184.  
  185.   wmax = xmax - nx + 1;
  186.   hmax = i->increment ? ny + 1 : ymax - ny + 1;
  187.  
  188.   if (nw < 1)         nw = 1;
  189.   else if (nw > wmax) nw = wmax;
  190.  
  191.   if (nh < 1)         nh = 1;
  192.   else if (nh > hmax) nh = hmax;
  193.  
  194.   i->ox = nx;
  195.   i->oy = ny;
  196.   i->w = nw;
  197.   i->h = nh;
  198. }
  199.  
  200. void
  201. Image_rotate_ccw(Image image)
  202. {
  203.   Image_transpose(image);
  204.   Image_vflip(image);
  205. }
  206.  
  207. void
  208. Image_rotate_cw(Image image)
  209. {
  210.   Image_transpose(image);
  211.   Image_hflip(image);
  212. }
  213.  
  214. void
  215. Image_rotate_180(Image image)
  216. {
  217.   Image_vflip(image);
  218.   Image_hflip(image);
  219. }
  220.  
  221. int
  222. Image_bpp(Image image)
  223. {
  224.   Gimp_Image_t *i = (Gimp_Image_t *) image;
  225.   return i->drawable->bpp;
  226. }
  227.  
  228. int
  229. Image_width(Image image)
  230. {
  231.   Gimp_Image_t *i = (Gimp_Image_t *) image;
  232.   return i->w;
  233. }
  234.  
  235. int
  236. Image_height(Image image)
  237. {
  238.   Gimp_Image_t *i = (Gimp_Image_t *) image;
  239.   return i->h;
  240. }
  241.  
  242. void
  243. Image_get_row(Image image, unsigned char *data, int row)
  244. {
  245.   Gimp_Image_t *i = (Gimp_Image_t *) image;
  246.   if (i->columns)
  247.     gimp_pixel_rgn_get_col(&(i->rgn), data,
  248.                            i->oy + row * i->increment, i->ox, i->w);
  249.   else
  250.     gimp_pixel_rgn_get_row(&(i->rgn), data,
  251.                            i->ox, i->oy + row * i->increment, i->w);
  252.   if (i->mirror) {
  253.     /* Flip row -- probably inefficiently */
  254.     int f, l, b = i->drawable->bpp;
  255.     for (f = 0, l = i->w - 1; f < l; f++, l--) {
  256.       int c;
  257.       unsigned char tmp;
  258.       for (c = 0; c < b; c++) {
  259.         tmp = data[f*b+c];
  260.         data[f*b+c] = data[l*b+c];
  261.         data[l*b+c] = tmp;
  262.       }
  263.     }
  264.   }
  265. }
  266.  
  267. void
  268. Image_progress_init(Image image)
  269. {
  270.   gimp_progress_init(_("Printing..."));
  271. }
  272.  
  273. void
  274. Image_note_progress(Image image, double current, double total)
  275. {
  276.   gimp_progress_update(current / total);
  277. }
  278.  
  279. void
  280. Image_progress_conclude(Image image)
  281. {
  282.   gimp_progress_update(1);
  283. }
  284.  
  285. const char *
  286. Image_get_appname(Image image)
  287. {
  288.   static char pluginname[] = PLUG_IN_NAME " plug-in V" PLUG_IN_VERSION
  289.     " for GIMP";
  290.   return pluginname;
  291. }
  292.  
  293. /*
  294.  * End of "$Id: print-image-gimp.c,v 1.2 2000/10/04 01:47:59 mitch Exp $".
  295.  */
  296.