1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#include "grobjs.h"
#include <stdlib.h>
int grError = 0;
/********************************************************************
*
* <Function>
* grRealloc
*
* <Description>
* Simple memory re-allocation.
*
* <Input>
* block :: original memory block address
* size :: new requested block size in bytes
*
* <Return>
* the memory block address. 0 in case of error
*
********************************************************************/
char* grAlloc( long size )
{
char* p;
p = (char*)malloc(size);
if (!p && size > 0)
{
grError = gr_err_memory;
}
if (p)
memset( p, 0, size );
return p;
}
/********************************************************************
*
* <Function>
* grRealloc
*
* <Description>
* Simple memory re-allocation.
*
* <Input>
* block :: original memory block address
* size :: new requested block size in bytes
*
* <Return>
* the memory block address. 0 in case of error
*
********************************************************************/
char* grRealloc( const char* block, long size )
{
char* p;
p = realloc( (char*)block, size );
if (!p && size > 0)
{
grError = gr_err_memory;
}
return p;
}
/********************************************************************
*
* <Function>
* grFree
*
* <Description>
* Simple memory release
*
* <Input>
* block :: target block
*
********************************************************************/
void grFree( const void* block )
{
if (block)
free( (char*)block );
}
static
int check_mode( grPixelMode pixel_mode,
int num_grays )
{
if ( pixel_mode <= gr_pixel_mode_none ||
pixel_mode >= gr_pixel_mode_max )
goto Fail;
if ( pixel_mode != gr_pixel_mode_gray ||
( num_grays >= 2 && num_grays < 256 ) )
return 0;
Fail:
grError = gr_err_bad_argument;
return grError;
}
/**********************************************************************
*
* <Function>
* grNewBitmap
*
* <Description>
* creates a new bitmap
*
* <Input>
* pixel_mode :: the target surface's pixel_mode
* num_grays :: number of grays levels for PAL8 pixel mode
* width :: width in pixels
* height :: height in pixels
*
* <Output>
* bit :: descriptor of the new bitmap
*
* <Return>
* Error code. 0 means success.
*
**********************************************************************/
extern int grNewBitmap( grPixelMode pixel_mode,
int num_grays,
int width,
int height,
grBitmap *bit )
{
int pitch;
/* check mode */
if (check_mode(pixel_mode,num_grays))
goto Fail;
/* check dimensions */
if (width < 0 || height < 0)
{
grError = gr_err_bad_argument;
goto Fail;
}
bit->width = width;
bit->rows = height;
bit->mode = pixel_mode;
bit->grays = num_grays;
pitch = width;
switch (pixel_mode)
{
case gr_pixel_mode_mono : pitch = (width+7) >> 3; break;
case gr_pixel_mode_pal4 : pitch = (width+3) >> 2; break;
case gr_pixel_mode_pal8 :
case gr_pixel_mode_gray : pitch = width; break;
case gr_pixel_mode_rgb555:
case gr_pixel_mode_rgb565: pitch = width*2; break;
case gr_pixel_mode_rgb24 : pitch = width*3; break;
case gr_pixel_mode_rgb32 : pitch = width*4; break;
default:
grError = gr_err_bad_target_depth;
return 0;
}
bit->pitch = pitch;
bit->buffer = grAlloc( (long)bit->pitch * bit->rows );
if (!bit->buffer) goto Fail;
return 0;
Fail:
return grError;
}
/**********************************************************************
*
* <Function>
* grDoneBitmap
*
* <Description>
* destroys a bitmap
*
* <Input>
* bitmap :: handle to bitmap descriptor
*
* <Note>
* This function does NOT release the bitmap descriptor, only
* the pixel buffer.
*
**********************************************************************/
extern void grDoneBitmap( grBitmap* bit )
{
grFree( bit->buffer );
bit->buffer = 0;
}