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

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

imlib: some more vec2i transition for simplicity.

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