Back to index

lightning-sunbird  0.9+nobinonly
icformat.c
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2000 SuSE, Inc.
00003  *
00004  * Permission to use, copy, modify, distribute, and sell this software and its
00005  * documentation for any purpose is hereby granted without fee, provided that
00006  * the above copyright notice appear in all copies and that both that
00007  * copyright notice and this permission notice appear in supporting
00008  * documentation, and that the name of SuSE not be used in advertising or
00009  * publicity pertaining to distribution of the software without specific,
00010  * written prior permission.  SuSE makes no representations about the
00011  * suitability of this software for any purpose.  It is provided "as is"
00012  * without express or implied warranty.
00013  *
00014  * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
00015  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
00016  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00017  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
00018  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
00019  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00020  *
00021  * Author:  Keith Packard, SuSE, Inc.
00022  */
00023 
00024 #include "icint.h"
00025 
00026 #define Mask(n)      ((n) == 32 ? 0xffffffff : ((1 << (n))-1))
00027 
00028 pixman_format_t *
00029 pixman_format_create (pixman_format_name_t name)
00030 {
00031     switch (name) {
00032     case PIXMAN_FORMAT_NAME_ARGB32:
00033        return pixman_format_create_masks (32,
00034                                 0xff000000,
00035                                 0x00ff0000,
00036                                 0x0000ff00,
00037                                 0x000000ff);
00038     case PIXMAN_FORMAT_NAME_RGB24:
00039        return pixman_format_create_masks (32,
00040                                 0x0,
00041                                 0xff0000,
00042                                 0x00ff00,
00043                                 0x0000ff);
00044     case PIXMAN_FORMAT_NAME_A8:
00045        return pixman_format_create_masks (8, 0xff,
00046                                 0, 0, 0);
00047     case PIXMAN_FORMAT_NAME_A1:
00048        return pixman_format_create_masks (1, 0x1,
00049                                 0, 0, 0);
00050     }
00051 
00052     return NULL;
00053 }
00054 
00055 /* XXX: There's some nonsense going on here. The macros above help
00056    pixman_format_create_masks to encode a format into an int, while
00057    immediately afterwards pixman_format_init goes through the effort of
00058    decoding it. This should all be disentagled, (it's probably
00059    possible to just eliminate the encoding macros altogether). */
00060 pixman_format_t *
00061 pixman_format_create_masks (int bpp,
00062                    int alpha_mask,
00063                    int red_mask,
00064                    int green_mask,
00065                    int blue_mask)
00066 {
00067     int type;
00068     int format_code;
00069     pixman_format_t *format;
00070 
00071     if (red_mask == 0 && green_mask == 0 && blue_mask == 0)
00072        type = PICT_TYPE_A;
00073     else if (red_mask > blue_mask)
00074        type = PICT_TYPE_ARGB;
00075     else
00076        type = PICT_TYPE_ABGR;
00077 
00078     format_code = PICT_FORMAT (bpp, type,
00079                             _FbOnes (alpha_mask),
00080                             _FbOnes (red_mask),
00081                             _FbOnes (green_mask),
00082                             _FbOnes (blue_mask));
00083 
00084     format = malloc (sizeof (pixman_format_t));
00085     if (format == NULL)
00086        return NULL;
00087 
00088     pixman_format_init (format, format_code);
00089 
00090     return format;
00091 }
00092 
00093 void
00094 pixman_format_init (pixman_format_t *format, int format_code)
00095 {
00096     memset (format, 0, sizeof (pixman_format_t));
00097     
00098 /* XXX: What do we want to lodge in here?
00099     format->id = FakeClientID (0);
00100 */
00101     format->format_code = format_code;
00102 
00103     switch (PICT_FORMAT_TYPE(format_code)) {
00104     case PICT_TYPE_ARGB:
00105        
00106        format->alphaMask = Mask(PICT_FORMAT_A(format_code));
00107        if (format->alphaMask)
00108            format->alpha = (PICT_FORMAT_R(format_code) +
00109                           PICT_FORMAT_G(format_code) +
00110                           PICT_FORMAT_B(format_code));
00111        
00112        format->redMask = Mask(PICT_FORMAT_R(format_code));
00113        format->red = (PICT_FORMAT_G(format_code) + 
00114                      PICT_FORMAT_B(format_code));
00115        
00116        format->greenMask = Mask(PICT_FORMAT_G(format_code));
00117        format->green = PICT_FORMAT_B(format_code);
00118        
00119        format->blueMask = Mask(PICT_FORMAT_B(format_code));
00120        format->blue = 0;
00121        break;
00122        
00123     case PICT_TYPE_ABGR:
00124        
00125        format->alphaMask = Mask(PICT_FORMAT_A(format_code));
00126        if (format->alphaMask)
00127            format->alpha = (PICT_FORMAT_B(format_code) +
00128                           PICT_FORMAT_G(format_code) +
00129                           PICT_FORMAT_R(format_code));
00130        
00131        format->blueMask = Mask(PICT_FORMAT_B(format_code));
00132        format->blue = (PICT_FORMAT_G(format_code) + 
00133                      PICT_FORMAT_R(format_code));
00134        
00135        format->greenMask = Mask(PICT_FORMAT_G(format_code));
00136        format->green = PICT_FORMAT_R(format_code);
00137        
00138        format->redMask = Mask(PICT_FORMAT_R(format_code));
00139        format->red = 0;
00140        break;
00141        
00142     case PICT_TYPE_A:
00143        
00144        format->alpha = 0;
00145        format->alphaMask = Mask(PICT_FORMAT_A(format_code));
00146 
00147        /* remaining fields already set to zero */
00148        break;
00149     }
00150 
00151     format->depth = _FbOnes ((format->alphaMask << format->alpha) |
00152                           (format->redMask << format->red) |
00153                           (format->blueMask << format->blue) |
00154                           (format->greenMask << format->green));
00155 }
00156 slim_hidden_def(pixman_format_init);
00157 
00158 void
00159 pixman_format_destroy (pixman_format_t *format)
00160 {
00161     free (format);
00162 }
00163 
00164 void
00165 pixman_format_get_masks (pixman_format_t *format,
00166                          int *bpp,
00167                          int *alpha_mask,
00168                          int *red_mask,
00169                          int *green_mask,
00170                          int *blue_mask)
00171 {
00172     *bpp = PICT_FORMAT_BPP (format->format_code);
00173 
00174     if (format->alphaMask)
00175        *alpha_mask = format->alphaMask << format->alpha;
00176     else
00177        *alpha_mask = 0;
00178 
00179     if (format->redMask)
00180        *red_mask = format->redMask << format->red;
00181     else
00182        *red_mask = 0;
00183 
00184     if (format->greenMask)
00185        *green_mask = format->greenMask << format->green;
00186     else
00187        *green_mask = 0;
00188 
00189     if (format->blueMask)
00190        *blue_mask = format->blueMask << format->blue;
00191     else
00192        *blue_mask = 0;
00193 }