home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / common / guillotine.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  6.5 KB  |  261 lines

  1. /*
  2.  *  Guillotine plug-in v0.9 by Adam D. Moss, adam@foxbox.org.  1998/09/01
  3.  */
  4.  
  5. /* The GIMP -- an image manipulation program
  6.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. /*
  24.  * HISTORY:
  25.  *     0.9 : 1998/09/01
  26.  *           Initial release.
  27.  */
  28.  
  29. #include "config.h"
  30.  
  31. #include <stdlib.h>
  32.  
  33. #include <libgimp/gimp.h>
  34.  
  35. #include "libgimp/stdplugins-intl.h"
  36.  
  37.  
  38. /* Declare local functions.
  39.  */
  40. static void      query  (void);
  41. static void      run    (gchar     *name,
  42.              gint       nparams,
  43.              GimpParam    *param,
  44.              gint      *nreturn_vals,
  45.              GimpParam   **return_vals);
  46.  
  47. static void      guillotine (gint32 image_ID);
  48.  
  49.  
  50. GimpPlugInInfo PLUG_IN_INFO =
  51. {
  52.   NULL,  /* init_proc  */
  53.   NULL,  /* quit_proc  */
  54.   query, /* query_proc */
  55.   run,   /* run_proc   */
  56. };
  57.  
  58.  
  59. MAIN ()
  60.  
  61. static void
  62. query (void)
  63. {
  64.   static GimpParamDef args[] =
  65.   {
  66.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  67.     { GIMP_PDB_IMAGE, "image", "Input image" },
  68.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable (unused)" }
  69.   };
  70.   static gint nargs = sizeof (args) / sizeof (args[0]);
  71.  
  72.   gimp_install_procedure ("plug_in_guillotine",
  73.               "Slice up the image into subimages, cutting along the image's Guides.  Fooey to you and your broccoli, Pokey.",
  74.               "This function takes an image and blah blah.  Hooray!",
  75.               "Adam D. Moss (adam@foxbox.org)",
  76.               "Adam D. Moss (adam@foxbox.org)",
  77.               "1998",
  78.               N_("<Image>/Image/Transforms/Guillotine"),
  79.               "RGB*, INDEXED*, GRAY*",
  80.               GIMP_PLUGIN,
  81.               nargs, 0,
  82.               args, NULL);
  83. }
  84.  
  85. static void
  86. run (gchar   *name,
  87.      gint     nparams,
  88.      GimpParam  *param,
  89.      gint    *nreturn_vals,
  90.      GimpParam **return_vals)
  91. {
  92.   static GimpParam values[1];
  93.   gint32 image_ID;
  94.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  95.  
  96.   *nreturn_vals = 1;
  97.   *return_vals = values;
  98.  
  99.   values[0].type = GIMP_PDB_STATUS;
  100.   values[0].data.d_status = status;
  101.  
  102.   INIT_I18N();
  103.  
  104.   /*  Get the specified drawable  */
  105.   image_ID = param[1].data.d_image;
  106.  
  107.   if (status == GIMP_PDB_SUCCESS)
  108.     {
  109.       gimp_progress_init (_("Guillotine..."));
  110.       guillotine (image_ID);
  111.       gimp_displays_flush ();
  112.     }
  113.  
  114.   values[0].data.d_status = status;
  115. }
  116.  
  117.  
  118. static gint
  119. unexciting (const void *a,
  120.         const void *b)
  121. {
  122.   gint j = * (gint *) a;
  123.   gint k = * (gint *) b;
  124.  
  125.   return (j - k);
  126. }
  127.  
  128.  
  129. static void
  130. guillotine (gint32 image_ID)
  131. {
  132.   gint num_vguides;
  133.   gint num_hguides;
  134.   gint guide_num;
  135.   gint* hguides;
  136.   gint* vguides;
  137.   gchar filename[1024];
  138.   gint i,x,y;
  139.  
  140.   num_vguides = 0;
  141.   num_hguides = 0;
  142.   guide_num = gimp_image_find_next_guide(image_ID, 0);
  143.  
  144.   /* Count the guides so we can allocate appropriate memory */
  145.   if (guide_num > 0)
  146.     {
  147.       do
  148.     {
  149.       switch (gimp_image_get_guide_orientation(image_ID, guide_num))
  150.         {
  151.         case GIMP_VERTICAL:
  152.           num_vguides++; break;
  153.         case GIMP_HORIZONTAL:
  154.           num_hguides++; break;
  155.         default:
  156.           g_print ("Aie!  Aie!  Aie!\n");
  157.           gimp_quit ();
  158.         }
  159.       guide_num = gimp_image_find_next_guide (image_ID, guide_num);
  160.     }
  161.       while (guide_num > 0);
  162.     }
  163.  
  164.   if (num_vguides+num_hguides)
  165.     {
  166.       g_print ("Yay... found %d horizontal guides and %d vertical guides.\n",
  167.            num_hguides, num_vguides);
  168.     }
  169.   else
  170.     {
  171.       g_print ("Poopy, no guides.\n");
  172.       return;
  173.     }
  174.  
  175.  
  176.   /* Allocate memory for the arrays of guide offsets, build arrays */
  177.   vguides = g_malloc ((num_vguides+2) * sizeof(gint));
  178.   hguides = g_malloc ((num_hguides+2) * sizeof(gint));
  179.   num_vguides = 0;
  180.   num_hguides = 0;
  181.   vguides[num_vguides++] = 0;
  182.   hguides[num_hguides++] = 0;
  183.   guide_num = gimp_image_find_next_guide(image_ID, 0);
  184.   if (guide_num>0)
  185.     {
  186.       do
  187.     {
  188.       switch (gimp_image_get_guide_orientation(image_ID, guide_num))
  189.         {
  190.         case GIMP_VERTICAL:
  191.           vguides[num_vguides++] =
  192.         gimp_image_get_guide_position(image_ID, guide_num); break;
  193.         case GIMP_HORIZONTAL:
  194.           hguides[num_hguides++] =
  195.         gimp_image_get_guide_position(image_ID, guide_num); break;
  196.         default:
  197.           g_print ("Aie!  Aie!  Aie!  Too!\n");
  198.           gimp_quit();
  199.         }
  200.       guide_num = gimp_image_find_next_guide(image_ID, guide_num);
  201.     }
  202.       while (guide_num>0);
  203.     }
  204.   vguides[num_vguides++] = gimp_image_width(image_ID);
  205.   hguides[num_hguides++] = gimp_image_height(image_ID);
  206.  
  207.   qsort(hguides, num_hguides, sizeof(gint), &unexciting);
  208.   qsort(vguides, num_vguides, sizeof(gint), &unexciting);
  209.  
  210.   for (i=0;i<num_vguides;i++)
  211.     g_print ("%d,",vguides[i]);
  212.   g_print ("\n");
  213.   for (i=0;i<num_hguides;i++)
  214.     g_print ("%d,",hguides[i]);
  215.   g_print ("\n");
  216.  
  217.  
  218.   /* Do the actual dup'ing and cropping... this isn't a too naive a
  219.      way to do this since we got copy-on-write tiles, either. */
  220.   for (y=0; y<num_hguides-1; y++)
  221.     {
  222.       for (x=0; x<num_vguides-1; x++)
  223.     {
  224.       gint32 new_image;
  225.  
  226.       new_image = gimp_channel_ops_duplicate (image_ID);
  227.  
  228.       if (new_image == -1)
  229.         {
  230.           g_print ("Aie3!\n");
  231.           return;
  232.         }
  233.  
  234.       gimp_image_undo_disable (new_image);
  235.  
  236. /*        gimp_undo_push_group_start (new_image); */
  237.  
  238. /*       printf("(%dx%d:%d,%d:%d,%d)\n", */
  239. /*          (vguides[x+1]-vguides[x]), */
  240. /*          (hguides[y+1]-hguides[y]), */
  241. /*          vguides[x], hguides[y],x, y);  */
  242.  
  243.  
  244.       gimp_crop (new_image, 
  245.              vguides[x+1] - vguides[x], hguides[y+1] - hguides[y],
  246.              vguides[x], hguides[y]);
  247.  
  248. /*        gimp_undo_push_group_end (new_image); */
  249.  
  250.       gimp_image_undo_enable (new_image);
  251.  
  252.       /* show the rough coordinates of the image in the title */
  253.         g_snprintf (filename, sizeof (filename), "%s-(%i,%i)", gimp_image_get_filename (image_ID), 
  254.               x, y);
  255.       gimp_image_set_filename(new_image, filename);
  256.  
  257.       gimp_display_new (new_image);
  258.     }
  259.     }
  260. }
  261.