~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-tools/tinatool/gphx/gdk2/gphxGdk_color.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :  TINA
 36  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/gphx/gdk2/gphxGdk_color.c,v $
 37  * Date    :  $Date: 2007/02/15 01:55:50 $
 38  * Version :  $Revision: 1.2 $
 39  * CVS Id  :  $Id: gphxGdk_color.c,v 1.2 2007/02/15 01:55:50 paul Exp $
 40  *
 41  * Author  :  giob@man.ac.uk, paul.bromiley@manchester.ac.uk
 42  *
 43  *********
 44 */
 45 /** 
 46  *  @file  gphxGdk_color.c
 47  *  @brief Functions to set and change colours.
 48  *
 49  *  These functions set foreground and background colours for windows and graphics contexts.
 50  *  They don't do any colourmap handling except for tv_screen_reset_color, which at present
 51  *  only alters the background colour of a Tv_screen.
 52  *
 53  *  Not that there are some particularities arising from the need to maintain existing Tina
 54  *  functionality while using GDK.  These are documented where they arise.  The main point to
 55  *  be aware of is that GdkRGB (see gphxGdk_cmap.c) does not require us to provide pixel
 56  *  values, nor to allocate colours (GdkRGB handles this itself, in the background).  This
 57  *  leaves us free to define our own pixel values.  I have exploited this fact to store Tina's
 58  *  historical integer colour values in the "pixel field" (i.e. the first 8 bits of the 32-bit
 59  *  integer used by GdkRGB).  In X11 Tina, these values were the X11 long int pixel fields of 
 60  *  an XColor, but in GTK/GDK Tina they are simply the integers stored in the std_lut arrays.
 61  *
 62  *  GAB 19 Dec 2003. 
 63  *
 64  *  Note: the functions in this file are duplicated for each graphics lib (x11, GDK, etc ...).
 65  *
 66  */
 67 
 68 
 69 #include "gphxGdk_color.h"
 70 
 71 #if HAVE_CONFIG_H
 72   #include <config.h>
 73 #endif
 74 
 75 #include <gtk/gtk.h>
 76 #include <tina/sys/sysDef.h>
 77 #include <tinatool/gphx/gdk2/gphx_GdkDef.h>
 78 #include <tinatool/gphx/gdk2/gphxGdk_cmap.h>
 79 
 80 /**
 81  * @brief  Convert a long int (X) colour representation to suit GdkRGB.  
 82  * @param  tv_screen  Pointer to current Tv_screen.
 83  * @param  color      Long U_int representing a colour.
 84  * @param  rgb        Pointer to 32-bit int representing the colour as required by GdkRGB.
 85  * @return Bool   True if conversion succeeded, false otherwise.
 86  *
 87  *  GdkRGB represents colours as 32-bit integers, where the first 8 bits are
 88  *  unused by Gdk and the remaining 24 bits are packed 8-bit red, green, blue
 89  *  values in the range 0 to 255.  Tina uses the X long unsigned int pixel values
 90  *  to represent colours.  This function converts between the two formats.
 91  *
 92  *  NOTE that this only works because I have chosen to use the first 8 bits
 93  *  of the 32-bit int as a pixel value in the range 0 to 255, and this value
 94  *  matches that stored in the std_lut fields of Cmap_data and Cmap_data_visible
 95  *  structs.  If this representation is changed the colour matching will fail
 96  *  horribly, with very nasty results.
 97  *
 98  *  GAB 16 Dec 2003.
 99  */
100 Bool   rgb_from_long_uint(Tv_screen  *tv_screen, long unsigned int  color, guint32 *rgb)
101 {
102   guchar   *colortable;
103   guchar   *newcolor;
104 
105   if (!tv_screen)
106     return false;
107 
108   colortable = (guchar *) tv_screen->colormap->colors;
109 
110   newcolor = (guchar *) rgb;
111 
112   newcolor[0] = (guchar) colortable[(4 * color)];
113   newcolor[1] = (guchar) colortable[(4 * color) + 1];
114   newcolor[2] = (guchar) colortable[(4 * color) + 2];
115   newcolor[3] = (guchar) colortable[(4 * color) + 3];
116 
117   return true;
118 }
119 
120 /**
121  * @brief  Convert a colour from GdkRGB representation to a GdkColor.  
122  * @param  rgb   Pointer to GdkRGB colour.
123  * @return GdkColor *   Pointer to a corresponding GdkColor.
124  *
125  * GDK does not provide this function as it recommends using either GdkColor or GdkRGB.
126  * However, Tina has a function to set the window background, and in GDK this cannot
127  * be implemented using GdkRGB functionality.  Hence this function does the conversion.
128  */
129 static GdkColor   *color_from_rgb(guint32 *rgb)
130 {
131   guchar    *newrgb = (guchar *) rgb;
132   GdkColor  *color  = g_malloc(sizeof(GdkColor)); 
133 
134   if(big_endian())              /* fixme: undeclared */
135   {
136     color->red   = (gushort) (newrgb[2] << 8);
137     color->green = (gushort) (newrgb[1] << 8);
138     color->blue  = (gushort) (newrgb[0] << 8);
139   }
140   else
141   {
142     color->red   = (gushort) (newrgb[1] << 8);
143     color->green = (gushort) (newrgb[2] << 8);
144     color->blue  = (gushort) (newrgb[3] << 8);
145   }
146     
147   return (gdk_colormap_alloc_color(gdk_rgb_get_cmap (), color, FALSE, TRUE) ? color : NULL);  
148 }
149 
150 /**
151  * @brief  Set the background color of a tv_screen.  
152  * @param  tv_screen  Pointer to Tv_screen for which to set background colour.
153  * @param  color      Long U_int representing colour to be set.
154  *
155  * This requires two colour format conversions!  Little foible of GDK ...
156  */
157 void   tv_screen_background_set(Tv_screen *tv_screen, long unsigned int  color)
158 {
159   guint32   *newrgb = g_malloc(sizeof(guint32));
160   GdkColor  *newcolor;
161     
162   if (!tv_screen)
163     return ;
164 
165   if (rgb_from_long_uint(tv_screen, color, newrgb));
166   {
167     newcolor = color_from_rgb(newrgb);
168     gdk_window_set_background(tv_screen->window, newcolor);
169   }
170   
171   g_free(newrgb);
172 }
173 
174 /**
175  * @brief  Set the foreground color of a tv_screen.  
176  * @param  tv_screen  Pointer to Tv_screen for which to set foreground colour.
177  * @param  color      Long U_int representing colour to be set.
178  */
179 void   tv_screen_color_set(Tv_screen *tv_screen, long unsigned int color)
180 {
181   guint32  *newrgb = g_malloc(sizeof(guint32));
182 
183   if (!tv_screen)
184     return;
185 
186   if (rgb_from_long_uint(tv_screen, color, newrgb));
187     gdk_rgb_gc_set_foreground(tv_screen->gc, *newrgb);
188 
189   g_free(newrgb);
190 }
191 
192 
193 /**
194  * @brief  Set the foreground color of a tv_screen to rgb values.  
195  * @param  tv_screen  Pointer to Tv_screen for which to set foreground colour.
196  * @param  rgb      Long U_int representing colour to be set.
197  *
198  * Notes: this function differs from the surrounding ones in that it 
199  * bypasses the TINA colormaps completely and lets GTK handle the 
200  * colors.  color is a 32-bit integer such that, if r, g, and b are 
201  * 8-bit integers in teh range 0-255, color = 65526*r+256*g+b.
202  *
203  */
204 void   tv_screen_rgb_set(Tv_screen *tv_screen, long unsigned int color)
205 {
206   if (!tv_screen) return;
207 
208   gdk_rgb_gc_set_foreground(tv_screen->gc, color);
209 }
210 
211 
212 /**
213  * @brief  .  
214  * @param  tv_screen  Pointer to Tv_screen for which to reset colour.
215  * @param  pixel      Long U_int pixel value (Tina-specific!) for the colour to be set.
216  * @param  red        Integer value for red setting of new colour.
217  * @param  green      Integer value for green setting of new colour.
218  * @param  blue       Integer value for blue setting of new colour.
219  *
220  * This, like rgb_from_long_uint, is another function that requires my convention that
221  * the pixel value is an integer in the range 0-255.
222  * If you change this, be warned ... !!
223  * GAB 19 Dec 2003.
224  */
225 void    tv_screen_reset_color(Tv_screen *tv_screen, 
226                               long unsigned int pixel, 
227                               int red, int green, int blue)
228 {
229   unsigned char r = red;
230   unsigned char g = green;
231   unsigned char b = blue;
232 
233   guchar  *cmap_cols;
234 
235   if (tv_screen)
236   {
237     cmap_cols = (guchar *) tv_screen->colormap->colors;
238  
239     if (big_endian())
240     {
241       cmap_cols[(4 * pixel)]      = (guchar) (b);
242       cmap_cols[(4 * pixel) + 1]  = (guchar) (g);
243       cmap_cols[(4 * pixel) + 2]  = (guchar) (r);
244       cmap_cols[(4 * pixel) + 3]  = (guchar) (pixel);
245     }
246     else
247     {
248       cmap_cols[(4 * pixel)]      = (guchar) (pixel);
249       cmap_cols[(4 * pixel) + 1]  = (guchar) (r);
250       cmap_cols[(4 * pixel) + 2]  = (guchar) (g);
251       cmap_cols[(4 * pixel) + 3]  = (guchar) (b);
252     }
253   }
254 }
255 
256 /**
257  * @brief  Not used in practice so will not be implemented - GAB 15 Dec 2003.  
258  */
259 void    tv_screen_reset_named_color(Tv_screen * tv_screen, long unsigned int pixel, char *color_name)
260 {
261 /*
262  * Only called from tv_reset_named_color, which itself is not called from anywhere.
263  */
264 }
265 
266 /**
267  * @brief  Get the GDK pixel value for the pixel at pos on tv_screen.  
268  * @param  tv_screen  Pointer to Tv_screen for which to get pixel value.
269  * @param  pos        Position at which to get pixel value.
270  * @return unsigned long   Gdk pixel value at pos on tv_screen.
271  */
272 unsigned long tv_screen_get_pixel(Tv_screen *tv_screen, Ipos pos)
273 {
274   guint32    pixel = 0;
275   GdkImage  *image;
276  
277   if (tv_screen)
278   {
279     image = gdk_image_get(tv_screen->window,
280                           0, 0, 
281                           tv_screen->width, tv_screen->height);
282     if(image)
283     {
284       pixel = gdk_image_get_pixel(image, pos.x, pos.y);
285       gdk_image_destroy(image);
286     }
287   }
288   return (pixel);
289 }
290 
291 /**
292  * @brief  Not required with GDK, but stub needed for consistency with Xv Tina.  
293  */
294 void    tv_screen_alloc_color(Tv_screen * tv_screen, long unsigned int *pixel, int red, int green, int blue)
295 {
296 /**  Not required with GDK, but stub needed for consistency with Xv Tina. */
297 }
298 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.