source: abuse/trunk/src/imlib/image.h @ 515

Last change on this file since 515 was 515, checked in by Sam Hocevar, 11 years ago

imlib: image::Pixel and image::PutPixel? now use vec2i arguments.

  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
11#ifndef _IMAGE_HPP_
12#define _IMAGE_HPP_
13
14#include <stdlib.h>
15#include "linked.h"
16#include "palette.h"
17#include "system.h"
18#include "specs.h"
19#define MAX_DIRTY 200
20
21extern char const *imerr_messages[];  // correspond to imERRORS
22#define imREAD_ERROR            1
23#define imINCORRECT_FILETYPE   2
24#define imFILE_CORRUPTED       3
25#define imFILE_NOT_FOUND       4
26#define imMEMORY_ERROR         5
27#define imNOT_SUPPORTED        6
28#define imWRITE_ERROR          7
29#define imMAX_ERROR            7
30
31int16_t current_error();
32void clear_errors();
33void set_error(int16_t x);
34int16_t last_error();
35void image_init();
36void image_uninit();
37extern linked_list image_list;
38
39typedef struct image_color_t
40{
41    uint16_t r;
42    uint16_t g;
43    uint16_t b;
44} image_color;
45
46class filter;
47
48
49class dirty_rect : public linked_node
50{
51public :
52  int16_t dx1,dy1,dx2,dy2;
53  dirty_rect(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
54  { dx1=x1; dy1=y1; dx2=x2; dy2=y2;
55    if(x2<x1 || y2<y1)
56      printf("add incorrect dirty\n");
57  }
58  virtual int16_t compare(void *n1, int16_t field)
59  { return((dirty_rect *)n1)->dy1>dy1; }
60} ;
61
62class image_descriptor
63{
64private:
65    int16_t l, h;
66    int16_t clipx1, clipy1, clipx2, clipy2;
67
68public:
69    uint8_t keep_dirt,
70            static_mem; // if set, don't free memory on exit
71
72    linked_list dirties;
73    void *extended_descriptor;
74
75    image_descriptor(vec2i size, int keep_dirties = 1, int static_memory = 0);
76    int16_t bound_x1(int16_t x1)  { return x1 < clipx1 ? clipx1 : x1; }
77    int16_t bound_y1(int16_t y1)  { return y1 < clipy1 ? clipy1 : y1; }
78    int16_t bound_x2(int16_t x2)  { return x2 > clipx2 ? clipx2 : x2; }
79    int16_t bound_y2(int16_t y2)  { return y2 > clipy2 ? clipy2 : y2; }
80    int16_t x1_clip() { return clipx1; }
81    int16_t y1_clip() { return clipy1; }
82    int16_t x2_clip() { return clipx2; }
83    int16_t y2_clip() { return clipy2; }
84    void dirty_area(int16_t x1, int16_t y1, int16_t x2, int16_t y2) { ; }
85    void clean_area(int16_t x1, int16_t y1, int16_t x2, int16_t y2) { ; }
86    void clear_dirties();
87    int16_t get_dirty_area(int16_t &x1, int16_t &y1, int16_t &x2, int16_t &y2)
88    {
89        return 0;
90    }
91    void get_clip(int16_t &x1, int16_t &y1, int16_t &x2, int16_t &y2)
92    {
93        x1 = clipx1; y1 = clipy1; x2 = clipx2; y2 = clipy2;
94    }
95    void set_clip(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
96    {
97        if(x2 < x1) x2 = x1;
98        if(y2 < y1) y2 = y1;
99        if(x1 < 0) clipx1 = 0; else clipx1 = x1;
100        if(y1 < 0) clipy1 = 0; else clipy1 = y1;
101        if(x2 >= l) clipx2 = l - 1; else clipx2 = x2;
102        if(y2 >= h) clipy2 = h - 1; else clipy2 = y2;
103    }
104    void reduce_dirties();
105    void add_dirty(int x1, int y1, int x2, int y2);
106    void delete_dirty(int x1, int y1, int x2, int y2);
107    void resize(int16_t length, int16_t height)
108    {
109        l = length; h = height;
110        clipx1 = 0; clipy1 = 0; clipx2 = l - 1; clipy2 = h - 1;
111    }
112};
113
114class image : public linked_node
115{
116private:
117    uint8_t *m_data;
118    vec2i m_size;
119    bool m_locked;
120
121    void MakePage(vec2i size, uint8_t *page_buffer);
122    void DeletePage();
123
124public:
125    image_descriptor *m_special;
126
127    image(spec_entry *e, bFILE *fp);
128    image(bFILE *fp);
129    image(vec2i size, uint8_t *page_buffer = NULL, int create_descriptor = 0);
130    ~image();
131
132    void Lock();
133    void Unlock();
134
135    uint8_t Pixel(vec2i pos);
136    void PutPixel(vec2i pos, uint8_t color);
137
138    inline uint8_t *scan_line(int16_t y)
139    {
140        return m_data + y * m_size.x;
141    }
142    inline uint8_t *next_line(int16_t lasty, uint8_t *last_scan)
143    {
144        return last_scan + m_size.x;
145    }
146    int32_t total_pixels(uint8_t background=0);
147    image *copy(); // makes a copy of an image
148    void clear(int16_t color = -1); // -1 is background color
149    void to_24bit(palette &pal);
150
151    vec2i Size() const { return m_size; }
152
153    void scroll(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
154                int16_t xd, int16_t yd);
155    void fill_image(image *screen, int16_t x1, int16_t y1,
156                    int16_t x2, int16_t y2, int16_t align = 1);
157    void put_image(image *screen, int16_t x, int16_t y, char transparent = 0);
158    void put_part(image *screen, int16_t x, int16_t y, int16_t x1, int16_t y1,
159                  int16_t x2, int16_t y2, char transparent = 0);
160    void put_part_xrev(image *screen, int16_t x, int16_t y,
161                       int16_t x1, int16_t y1, int16_t x2, int16_t y2,
162                       char transparent = 0);
163    void put_part_masked(image *screen, image *mask, int16_t x, int16_t y,
164                         int16_t maskx, int16_t masky, int16_t x1, int16_t y1,
165                         int16_t x2, int16_t y2);
166    image *copy_part_dithered(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
167    void bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color);
168    void xor_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color);
169    void widget_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
170                    uint8_t light, uint8_t med, uint8_t dark);
171    void line(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color);
172    void rectangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
173                   uint8_t color);
174    void burn_led(int16_t x, int16_t y, int32_t num, int16_t color,
175                  int16_t scale = 1);
176    void set_clip(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
177    void get_clip(int16_t &x1,int16_t &y1,int16_t &x2,int16_t &y2);
178    void in_clip(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
179
180    void dirt_off()
181    {
182        if(m_special && m_special->keep_dirt) m_special->keep_dirt = 0;
183    }
184    void dirt_on()
185    {
186        if(m_special) m_special->keep_dirt = 1;
187    }
188
189    void add_dirty(int x1, int y1, int x2, int y2)
190    {
191        if(m_special) m_special->add_dirty(x1, y1, x2, y2);
192    }
193    void delete_dirty(int x1, int y1, int x2, int y2)
194    {
195        if(m_special) m_special->delete_dirty(x1, y1, x2, y2);
196    }
197    void clear_dirties()
198    {
199        if(m_special) m_special->clear_dirties();
200    }
201    void dither(palette *pal); // use a b&w palette!
202    void Scale(vec2i size);
203    void SetSize(vec2i size, uint8_t *page = NULL);
204    void flood_fill(int16_t x, int16_t y, uint8_t color);
205    image *create_smooth(int16_t smoothness = 1); // 0 no smoothness
206    void unpack_scanline(int16_t line, char bitsperpixel = 1);
207    uint8_t brightest_color(palette *pal);
208    void flip_x();
209    void flip_y();
210    void make_color(uint8_t color);
211    uint8_t darkest_color(palette *pal, int16_t noblack = 0);
212};
213
214class image_controller
215{
216public:
217    image_controller()
218    {
219        image_init();
220    }
221    ~image_controller()
222    {
223        image_uninit();
224    }
225};
226
227#endif /* _IMAGE_HPP_ */
228
Note: See TracBrowser for help on using the repository browser.