Back to index

lightning-sunbird  0.9+nobinonly
icpixels.c
Go to the documentation of this file.
00001 /*
00002  * $Id: icpixels.c,v 1.3.4.1 2005/09/27 23:09:03 vladimir%pobox.com Exp $
00003  *
00004  * Copyright © 1998 Keith Packard
00005  *
00006  * Permission to use, copy, modify, distribute, and sell this software and its
00007  * documentation for any purpose is hereby granted without fee, provided that
00008  * the above copyright notice appear in all copies and that both that
00009  * copyright notice and this permission notice appear in supporting
00010  * documentation, and that the name of Keith Packard not be used in
00011  * advertising or publicity pertaining to distribution of the software without
00012  * specific, written prior permission.  Keith Packard makes no
00013  * representations about the suitability of this software for any purpose.  It
00014  * is provided "as is" without express or implied warranty.
00015  *
00016  * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00017  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
00018  * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
00019  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
00020  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
00021  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00022  * PERFORMANCE OF THIS SOFTWARE.
00023  */
00024 
00025 #include "pixman-xserver-compat.h"
00026 
00027 static void
00028 FbPixelsInit (FbPixels *pixels, pixman_bits_t *buf, int width, int height, int depth, int bpp, int stride);
00029 
00030 static unsigned int
00031 pixman_bits_per_pixel (unsigned int depth);
00032 
00033 static unsigned int
00034 pixman_bits_per_pixel (unsigned int depth)
00035 {
00036     if (depth > 8)
00037        if (depth > 16)
00038            return 32;
00039        else
00040            return 16;
00041     else
00042        if (depth > 4)
00043            return 8;
00044        else if (depth > 1)
00045            return 4;
00046        else
00047            return 1;
00048 }
00049 
00050 FbPixels *
00051 FbPixelsCreate (int width, int height, int depth)
00052 {
00053     FbPixels         *pixels;
00054     pixman_bits_t           *buf;
00055     unsigned int     buf_size;
00056     unsigned int     bpp;
00057     unsigned int     stride;
00058     unsigned int     adjust;
00059     unsigned int     base;
00060 
00061     bpp = pixman_bits_per_pixel (depth);
00062     stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (pixman_bits_t);
00063     buf_size = height * stride;
00064     base = sizeof (FbPixels);
00065     adjust = 0;
00066     if (base & 7)
00067        adjust = 8 - (base & 7);
00068     buf_size += adjust;
00069 
00070     pixels = malloc(base + buf_size);
00071     if (!pixels)
00072        return NULL;
00073 
00074     buf = (pixman_bits_t *) ((char *)pixels + base + adjust);
00075 
00076     FbPixelsInit (pixels, buf, width, height, depth, bpp, stride);
00077 
00078     return pixels;
00079 }
00080 
00081 FbPixels *
00082 FbPixelsCreateForData (pixman_bits_t *data, int width, int height, int depth, int bpp, int stride)
00083 {
00084     FbPixels *pixels;
00085 
00086     pixels = malloc (sizeof (FbPixels));
00087     if (pixels == NULL)
00088        return NULL;
00089 
00090     FbPixelsInit (pixels, data, width, height, depth, bpp, stride);
00091 
00092     return pixels;
00093 }
00094 
00095 static void
00096 FbPixelsInit (FbPixels *pixels, pixman_bits_t *buf, int width, int height, int depth, int bpp, int stride)
00097 {
00098     pixels->data = buf;
00099     pixels->width = width;
00100     pixels->height = height;
00101     pixels->depth = depth;
00102     pixels->bpp = bpp;
00103     pixels->stride = stride;
00104     pixels->x = 0;
00105     pixels->y = 0;
00106     pixels->refcnt = 1;
00107 }
00108 
00109 void
00110 FbPixelsDestroy (FbPixels *pixels)
00111 {
00112     if(--pixels->refcnt)
00113        return;
00114 
00115     free(pixels);
00116 }
00117