386f323bd0ec1406de5a187991d87279742bcab5
[simavr] / examples / shared / libc3 / src / c3pixels.h
1 /*
2         c3pixels.h
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of libc3.
7
8         libc3 is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         libc3 is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with libc3.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #ifndef __C3PIXELS_H___
24 #define __C3PIXELS_H___
25
26 #include "c3types.h"
27 #include "c_utils.h"
28
29 //! for format hint
30 enum {
31         C3PIXEL_ARGB = 0,
32         C3PIXEL_RGB,
33         C3PIXEL_A
34 };
35
36 typedef struct c3pixels_t {
37         str_p name;             // optional
38         uint32_t w, h;  // width & height in pixels
39         size_t row;             // size of one row in bytes
40         void * base;    // base address
41
42         union {
43                 struct {
44                         uint32_t        own : 1,        // is the base our own to delete
45                                 alloc : 1,                      // is the c3pixels_p our own to delete
46                                 dirty : 1,                      // pixels have been changed
47                                 psize : 4,                      // pixel size in byte
48                                 normalize : 1,          // texture coordinates are 0...1
49                                 trace : 1,                      // debug
50                                 format : 8;                     // not used internally
51                 };
52                 uint32_t flags;
53         };
54         c3apiobject_t   texture;
55         int                     refCount;       // TODO: Implement reference counting ?
56 } c3pixels_t, *c3pixels_p;
57
58 DECLARE_C_ARRAY(c3pixels_p, c3pixels_array, 4);
59
60 //! Allocates a new c3pixels, also allocates the pixels if row == NULL
61 c3pixels_p
62 c3pixels_new(
63                 uint32_t w,
64                 uint32_t h,
65                 int      psize /* in bytes */,
66                 size_t row,
67                 void * base);
68
69 //! Initializes p, also allocates the pixels if row == NULL
70 c3pixels_p
71 c3pixels_init(
72                 c3pixels_p p,
73                 uint32_t w,
74                 uint32_t h,
75                 int      psize /* in bytes */,
76                 size_t row,
77                 void * base);
78
79 //! Dispose of the pixels, and potentially p if it was allocated with c3pixels_new
80 void
81 c3pixels_dispose(
82                 c3pixels_p p );
83
84 //! Disposes of the pixels, only
85 void
86 c3pixels_purge(
87                 c3pixels_p p );
88
89 //! (Re)allocate pixels if pixels had been purged
90 void
91 c3pixels_alloc(
92                 c3pixels_p p );
93
94 //! Get a pixel address
95 static inline void *
96 c3pixels_get(
97                 c3pixels_p p,
98                 int x, int y)
99 {
100         return ((uint8_t*)p->base) + (y * p->row) + (x * p->psize);
101 }
102
103 //! Zeroes the pixels
104 void
105 c3pixels_zero(
106                 c3pixels_p p);
107
108 IMPLEMENT_C_ARRAY(c3pixels_array);
109
110 #endif /* __C3PIXELS_H___ */