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

  1. /* gap_layer_copy.c
  2.  *    by hof (Wolfgang Hofer)
  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. /* revision history:
  24.  * version 0.99.00 1999.03.03   hof: use the regular gimp_layer_copy and gimp_channel_copy
  25.  *                                   (removed private variant)
  26.  * version 0.98.00 1998.11.26   hof: added channel copy
  27.  * version         1998.11.26   hof: bugfix have to copy the layer's layer_mask too.
  28.  *                                          type check of destination image
  29.  * version 0.96.00              hof: bugfix memory leak (must set src_tile to unref after use) 
  30.  * version 0.93.01              hof: when creating the destination layer
  31.  *                                   add alpha channel if needed in extra call 
  32.  * version 0.90.00;             hof: 1.st (pre) release
  33.  */
  34.  
  35. /* SYTEM (UNIX) includes */ 
  36. /* GIMP includes */
  37. /* GAP includes */
  38. #include "gap_layer_copy.h"
  39. #include "gap_pdb_calls.h"
  40.  
  41. extern      int gap_debug; /* ==0  ... dont print debug infos */
  42.  
  43. /* ============================================================================
  44.  * p_my_layer_copy
  45.  *    copy src_layer to the dst_image,
  46.  *    return the id of the new created layer (the copy)
  47.  * NOTE: source layer MUST have same type (bpp) for now
  48.  *       it would be fine to extend the code to convert between any type
  49.  * ============================================================================
  50.  */
  51. gint32 p_my_layer_copy (gint32 dst_image_id,
  52.                         gint32 src_layer_id,
  53.                         gdouble    opacity, /* 0.0 upto 100.0 */
  54.                         GimpLayerModeEffects mode,
  55.                         gint *src_offset_x,
  56.                         gint *src_offset_y )
  57. {
  58.   gint32 l_new_layer_id;
  59.   gint32 l_ret_id;     
  60.   char  *l_name;
  61.   GimpImageType  l_src_type;
  62.  
  63.   if(gap_debug) printf("GAP p_my_layer_copy: START\n");
  64.  
  65.   l_ret_id = -1;       /* prepare error retcode -1 */
  66.   l_name = NULL;
  67.   
  68.   if(opacity > 99.99) opacity = 100.0;
  69.   if(opacity < 0.0)   opacity = 0.0;
  70.   
  71.   l_name = gimp_layer_get_name(src_layer_id);
  72.   l_src_type   = gimp_drawable_type(src_layer_id);
  73.  
  74.   switch(l_src_type)
  75.   {
  76.     case GIMP_RGB_IMAGE:         /* 0 */
  77.     case GIMP_RGBA_IMAGE:        /* 1 */
  78.        if(gimp_image_base_type(dst_image_id) != GIMP_RGB) { return -1; }
  79.        break;
  80.     case GIMP_GRAY_IMAGE:        /* 2 */
  81.     case GIMP_GRAYA_IMAGE:       /* 3 */
  82.        if(gimp_image_base_type(dst_image_id) != GIMP_GRAY) { return -1; }
  83.        break;
  84.     case GIMP_INDEXED_IMAGE:     /* 4 */
  85.     case GIMP_INDEXEDA_IMAGE:    /* 5 */
  86.        if(gimp_image_base_type(dst_image_id) != GIMP_INDEXED) { return -1; }
  87.        break;
  88.   }
  89.  
  90.  
  91.   /* copy the layer */  
  92.   l_new_layer_id = gimp_layer_copy(src_layer_id);
  93.  
  94.   if(l_new_layer_id >= 0)
  95.   {
  96.     if(p_gimp_drawable_set_image(l_new_layer_id, dst_image_id) >= 0)
  97.     {
  98.         if(! gimp_drawable_has_alpha(l_new_layer_id))
  99.         {
  100.            /* have to add alpha channel */
  101.            gimp_layer_add_alpha(l_new_layer_id);
  102.         }
  103.  
  104.         /* findout the offsets of the original layer within the source Image */
  105.         gimp_drawable_offsets(src_layer_id, src_offset_x, src_offset_y );
  106.  
  107.         gimp_layer_set_name(l_new_layer_id, l_name);
  108.         gimp_layer_set_opacity(l_new_layer_id, opacity);
  109.         gimp_layer_set_mode(l_new_layer_id, mode);
  110.  
  111.  
  112.         l_ret_id = l_new_layer_id;  /* all done OK */
  113.     }
  114.  
  115.   }
  116.     
  117.   if(l_name != NULL) { g_free (l_name); }
  118.  
  119.   if(gap_debug) printf("GAP p_my_layer_copy: ret %d\n", (int)l_ret_id);
  120.  
  121.   return l_ret_id;
  122. }    /* end p_my_layer_copy */
  123.  
  124.  
  125. /* ============================================================================
  126.  * p_my_channel_copy
  127.  *   copy a channel to dst_IMAGE
  128.  * ============================================================================
  129.  */
  130. gint32 p_my_channel_copy (gint32 dst_image_id,
  131.                           gint32 src_channel_id)
  132. {
  133.   gint32 l_new_channel_id;
  134.   gint32 l_ret_id;     
  135.   char  *l_name;
  136.  
  137.   if(gap_debug) printf("GAP :p_my_channel_copy START\n");
  138.  
  139.   l_ret_id = -1;       /* prepare error retcode -1 */
  140.   l_name = NULL;
  141.   
  142.   /* create new channel in destination image */
  143.   l_name = gimp_channel_get_name(src_channel_id);
  144.  
  145.   /* copy the channel */
  146.   l_new_channel_id = gimp_channel_copy(src_channel_id);
  147.   if(l_new_channel_id >= 0)
  148.   {
  149.     if(p_gimp_drawable_set_image(l_new_channel_id, dst_image_id) >= 0)
  150.     {
  151.         gimp_channel_set_name(l_new_channel_id, l_name);
  152.         l_ret_id = l_new_channel_id;  /* all done OK */  
  153.     }
  154.   }
  155.   
  156.   if(l_name != NULL) { g_free (l_name); }
  157.  
  158.   if(gap_debug) printf("GAP :p_my_channel_copy id=%d\n", (int)l_ret_id);
  159.  
  160.   return l_ret_id;
  161. }    /* end p_my_channel_copy */
  162.