home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / libgimp / gimpdrawable.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-29  |  5.9 KB  |  256 lines

  1. /* LIBGIMP - The GIMP Library
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  *
  4.  * gimpdrawable.c
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Library General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the
  18.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.  * Boston, MA 02111-1307, USA.
  20.  */
  21.  
  22. #include <glib.h>
  23.  
  24. #include "gimp.h"
  25.  
  26.  
  27. #define TILE_WIDTH   _gimp_tile_width
  28. #define TILE_HEIGHT  _gimp_tile_height
  29.  
  30.  
  31. extern gint _gimp_tile_width;
  32. extern gint _gimp_tile_height;
  33.  
  34.  
  35. GimpDrawable *
  36. gimp_drawable_get (gint32 drawable_ID)
  37. {
  38.   GimpDrawable *drawable;
  39.  
  40.   drawable = g_new (GimpDrawable, 1);
  41.   drawable->id           = drawable_ID;
  42.   drawable->width        = gimp_drawable_width  (drawable_ID);
  43.   drawable->height       = gimp_drawable_height (drawable_ID);
  44.   drawable->bpp          = gimp_drawable_bpp    (drawable_ID);
  45.   drawable->ntile_rows   = (drawable->height + TILE_HEIGHT - 1) / TILE_HEIGHT;
  46.   drawable->ntile_cols   = (drawable->width  + TILE_WIDTH  - 1) / TILE_WIDTH;
  47.   drawable->tiles        = NULL;
  48.   drawable->shadow_tiles = NULL;
  49.  
  50.   return drawable;
  51. }
  52.  
  53. void
  54. gimp_drawable_detach (GimpDrawable *drawable)
  55. {
  56.   if (drawable)
  57.     {
  58.       gimp_drawable_flush (drawable);
  59.       if (drawable->tiles)
  60.     g_free (drawable->tiles);
  61.       if (drawable->shadow_tiles)
  62.     g_free (drawable->shadow_tiles);
  63.       g_free (drawable);
  64.     }
  65. }
  66.  
  67. void
  68. gimp_drawable_flush (GimpDrawable *drawable)
  69. {
  70.   GimpTile *tiles;
  71.   gint ntiles;
  72.   gint i;
  73.  
  74.   if (drawable)
  75.     {
  76.       if (drawable->tiles)
  77.     {
  78.       tiles = drawable->tiles;
  79.       ntiles = drawable->ntile_rows * drawable->ntile_cols;
  80.  
  81.       for (i = 0; i < ntiles; i++)
  82.         if ((tiles[i].ref_count > 0) && tiles[i].dirty)
  83.           gimp_tile_flush (&tiles[i]);
  84.     }
  85.  
  86.       if (drawable->shadow_tiles)
  87.     {
  88.       tiles = drawable->shadow_tiles;
  89.       ntiles = drawable->ntile_rows * drawable->ntile_cols;
  90.  
  91.       for (i = 0; i < ntiles; i++)
  92.         if ((tiles[i].ref_count > 0) && tiles[i].dirty)
  93.           gimp_tile_flush (&tiles[i]);
  94.     }
  95.     }
  96. }
  97.  
  98. void
  99. gimp_drawable_delete (GimpDrawable *drawable)
  100. {
  101.   if (drawable)
  102.     {
  103.       if (gimp_drawable_is_layer (drawable->id))
  104.     gimp_layer_delete (drawable->id);
  105.       else
  106.     gimp_channel_delete (drawable->id);
  107.     }
  108. }
  109.  
  110.  
  111. gchar *
  112. gimp_drawable_name (gint32 drawable_ID)
  113. {
  114.   if (gimp_drawable_is_layer (drawable_ID))
  115.     return gimp_layer_get_name (drawable_ID);
  116.  
  117.   return gimp_channel_get_name (drawable_ID);
  118. }
  119.  
  120.  
  121. gboolean
  122. gimp_drawable_visible (gint32 drawable_ID)
  123. {
  124.   if (gimp_drawable_is_layer (drawable_ID))
  125.     return gimp_layer_get_visible (drawable_ID);
  126.  
  127.   return gimp_channel_get_visible (drawable_ID);
  128. }
  129.  
  130.  
  131. void
  132. gimp_drawable_set_name (gint32  drawable_ID,
  133.             gchar  *name)
  134. {
  135.   if (gimp_drawable_is_layer (drawable_ID))
  136.     gimp_layer_set_name (drawable_ID, name);
  137.   else
  138.     gimp_channel_set_name (drawable_ID, name);
  139. }
  140.  
  141. void
  142. gimp_drawable_set_visible (gint32   drawable_ID,
  143.                gboolean visible)
  144. {
  145.   if (gimp_drawable_is_layer (drawable_ID))
  146.     gimp_layer_set_visible (drawable_ID, visible);
  147.   else
  148.     gimp_channel_set_visible (drawable_ID, visible);
  149. }
  150.  
  151. GimpTile *
  152. gimp_drawable_get_tile (GimpDrawable *drawable,
  153.             gboolean      shadow,
  154.             gint          row,
  155.             gint          col)
  156. {
  157.   GimpTile *tiles;
  158.   guint right_tile;
  159.   guint bottom_tile;
  160.   gint  ntiles;
  161.   gint  tile_num;
  162.   gint  i, j, k;
  163.  
  164.   if (drawable)
  165.     {
  166.       if (shadow)
  167.     tiles = drawable->shadow_tiles;
  168.       else
  169.     tiles = drawable->tiles;
  170.  
  171.       if (!tiles)
  172.     {
  173.       ntiles = drawable->ntile_rows * drawable->ntile_cols;
  174.       tiles = g_new (GimpTile, ntiles);
  175.  
  176.       right_tile = drawable->width - ((drawable->ntile_cols - 1) * TILE_WIDTH);
  177.       bottom_tile = drawable->height - ((drawable->ntile_rows - 1) * TILE_HEIGHT);
  178.  
  179.       for (i = 0, k = 0; i < drawable->ntile_rows; i++)
  180.         {
  181.           for (j = 0; j < drawable->ntile_cols; j++, k++)
  182.         {
  183.           tiles[k].bpp       = drawable->bpp;
  184.           tiles[k].tile_num  = k;
  185.           tiles[k].ref_count = 0;
  186.           tiles[k].dirty     = FALSE;
  187.           tiles[k].shadow    = shadow;
  188.           tiles[k].data      = NULL;
  189.           tiles[k].drawable  = drawable;
  190.  
  191.           if (j == (drawable->ntile_cols - 1))
  192.             tiles[k].ewidth  = right_tile;
  193.           else
  194.             tiles[k].ewidth  = TILE_WIDTH;
  195.  
  196.           if (i == (drawable->ntile_rows - 1))
  197.             tiles[k].eheight = bottom_tile;
  198.           else
  199.             tiles[k].eheight = TILE_HEIGHT;
  200.         }
  201.         }
  202.  
  203.       if (shadow)
  204.         drawable->shadow_tiles = tiles;
  205.       else
  206.         drawable->tiles = tiles;
  207.     }
  208.  
  209.       tile_num = row * drawable->ntile_cols + col;
  210.       return &tiles[tile_num];
  211.     }
  212.  
  213.   return NULL;
  214. }
  215.  
  216. GimpTile *
  217. gimp_drawable_get_tile2 (GimpDrawable *drawable,
  218.              gint          shadow,
  219.              gint          x,
  220.              gint          y)
  221. {
  222.   gint row;
  223.   gint col;
  224.  
  225.   col = x / TILE_WIDTH;
  226.   row = y / TILE_HEIGHT;
  227.  
  228.   return gimp_drawable_get_tile (drawable, shadow, row, col);
  229. }
  230.  
  231. guchar *
  232. gimp_drawable_get_thumbnail_data (gint32  drawable_ID,
  233.                   gint   *width,
  234.                   gint   *height,
  235.                   gint   *bpp)
  236. {
  237.   gint    ret_width;
  238.   gint    ret_height;
  239.   guchar *image_data;
  240.   gint    data_size;
  241.  
  242.   _gimp_drawable_thumbnail (drawable_ID,
  243.                 *width,
  244.                 *height,
  245.                 &ret_width,
  246.                 &ret_height,
  247.                 bpp,
  248.                 &data_size,
  249.                 &image_data);
  250.  
  251.   *width  = ret_width;
  252.   *height = ret_height;
  253.  
  254.   return image_data;
  255. }
  256.