FreeBASIC  0.91.0
gfx_image.c
Go to the documentation of this file.
1 /* image create/destroy functions */
2 
3 #include "fb_gfx.h"
4 
5 static void *gfx_imagecreate(int width, int height, unsigned int color, int depth, int flags, int usenewheader)
6 {
9  int size, pitch, header_size = 4;
10  int bpp;
11 
12  if ((!__fb_gfx) || (width <= 0) || (height <= 0)) {
14  return NULL;
15  }
16 
17  if (depth > 0) {
18  bpp = BYTES_PER_PIXEL(depth);
19  if ((bpp != 1) && (bpp != 2) && (bpp != 4)) {
21  return NULL;
22  }
23  }
24  else
25  bpp = __fb_gfx->bpp;
26 
27  if (flags & DEFAULT_COLOR_1) {
28  switch (bpp) {
29  case 1: color = 0; break;
30  case 2: color = MASK_COLOR_16; break;
31  case 4: color = MASK_COLOR_32 | MASK_A_32; break;
32  }
33  }
34  else {
35  if (bpp == 2)
36  color = ((color & 0xF8) >> 3) | ((color & 0xFC00) >> 5) | ((color & 0xF80000) >> 8);
37  }
38 
39  pitch = width * bpp;
40  if (usenewheader) {
41  header_size = sizeof(PUT_HEADER);
42  pitch = (pitch + 0xF) & ~0xF;
43  }
44  size = pitch * height;
45 
46  /* 0xF for the para alignment, p_size is sizeof(void *) rounded up to % 16 for the storage for the original pointer */
47  int p_size = (sizeof(void *) + 0xF) & 0xF;
48  void *tmp = malloc(size + header_size + p_size + 0xF);
49  if (tmp == NULL) {
51  return NULL;
52  }
53 
54  image = (PUT_HEADER *)(((intptr_t)tmp + p_size + 0xF) & ~0xF);
55  ((void **)image)[-1] = tmp;
56 
57  if (!usenewheader) {
58  /* use old-style header for compatibility */
59  image->old.bpp = bpp;
60  image->old.width = width;
61  image->old.height = height;
62  }
63  else {
64  image->type = PUT_HEADER_NEW;
65  image->bpp = bpp;
66  image->width = width;
67  image->height = height;
68  image->pitch = pitch;
69  fb_hMemSet(image->_reserved, 0, sizeof(image->_reserved));
70  }
71  fb_hPrepareTarget(context, (void *)image);
73  context->pixel_set((unsigned char *)image + header_size, color, (pitch * height) / bpp);
74 
76  return image;
77 }
78 
79 FBCALL void *fb_GfxImageCreate(int width, int height, unsigned int color, int depth, int flags)
80 {
81  return gfx_imagecreate( width, height, color, depth, flags, TRUE );
82 }
83 
84 FBCALL void *fb_GfxImageCreateQB(int width, int height, unsigned int color, int depth, int flags)
85 {
86  return gfx_imagecreate( width, height, color, depth, flags, FALSE );
87 }
88 
90 {
91  if( image == NULL ) return;
92  free(((void **)image)[-1]);
93 }