1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * |
---|
5 | * This software was released into the Public Domain. As with most public |
---|
6 | * domain software, no warranty is made or implied by Crack dot Com or |
---|
7 | * Jonathan Clark. |
---|
8 | */ |
---|
9 | |
---|
10 | #include "config.h" |
---|
11 | |
---|
12 | #include "image24.hpp" |
---|
13 | #include "image.hpp" |
---|
14 | |
---|
15 | image24::image24(unsigned short width, unsigned short height, |
---|
16 | unsigned char *buffer) |
---|
17 | { |
---|
18 | w=width; |
---|
19 | h=height; |
---|
20 | |
---|
21 | data=(unsigned char *)malloc(width*height*3); |
---|
22 | CONDITION(data,"unable to alloc enough memory for 24 bit image"); |
---|
23 | } |
---|
24 | |
---|
25 | void image24::clear(unsigned char r, unsigned char g, |
---|
26 | unsigned char b) |
---|
27 | { |
---|
28 | int x,y; |
---|
29 | unsigned char *p=data; |
---|
30 | for (y=0;y<h;y++) |
---|
31 | { |
---|
32 | for (x=0;x<w;x++) |
---|
33 | { |
---|
34 | *(p++)=r; |
---|
35 | *(p++)=g; |
---|
36 | *(p++)=b; |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | void image24::add_error(int x, int y, int r_error, int g_error, int b_error, int error_mult) |
---|
43 | { |
---|
44 | if (x>=w || x<0 || y>=h || y<0) // check to make sure this pixel is in the image |
---|
45 | return ; |
---|
46 | unsigned char *pix=data+(y*w*3+x*3); |
---|
47 | int result; |
---|
48 | |
---|
49 | result=(int)(*pix)+r_error*error_mult/32; |
---|
50 | if (result>255) |
---|
51 | *pix=255; |
---|
52 | else if (result<0) *pix=0; |
---|
53 | else *pix=result; |
---|
54 | pix++; |
---|
55 | |
---|
56 | result=(int)(*pix)+g_error*error_mult/32; |
---|
57 | if (result>255) |
---|
58 | *pix=255; |
---|
59 | else if (result<0) *pix=0; |
---|
60 | else *pix=result; |
---|
61 | pix++; |
---|
62 | |
---|
63 | result=(int)(*pix)+b_error*error_mult/32; |
---|
64 | if (result>255) |
---|
65 | *pix=255; |
---|
66 | else if (result<0) *pix=0; |
---|
67 | else *pix=result; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | image *image24::dither(palette *pal) |
---|
72 | { |
---|
73 | int i,j,closest; |
---|
74 | unsigned char r,g,b,*cur_pixel=data,ar,ag,ab; |
---|
75 | image *dest=new image(w,h); |
---|
76 | for (j=0;j<h;j++) |
---|
77 | { |
---|
78 | for (i=0;i<w;i++) |
---|
79 | { |
---|
80 | r=(*cur_pixel); cur_pixel++; |
---|
81 | g=(*cur_pixel); cur_pixel++; |
---|
82 | b=(*cur_pixel); cur_pixel++; |
---|
83 | closest=pal->find_closest(r,g,b); // find the closest match in palette |
---|
84 | dest->putpixel(i,j,closest); // set the pixel to this color |
---|
85 | |
---|
86 | pal->get(closest,ar,ag,ab); // see what the actual color we used was |
---|
87 | |
---|
88 | add_error(i+1,j,ar-r,ag-g,ab-b,8); |
---|
89 | add_error(i+2,j,ar-r,ag-g,ab-b,4); |
---|
90 | |
---|
91 | add_error(i-2,j+1,ar-r,ag-g,ab-b,2); |
---|
92 | add_error(i-1,j+1,ar-r,ag-g,ab-b,4); |
---|
93 | add_error(i,j+1,ar-r,ag-g,ab-b,8); |
---|
94 | add_error(i+1,j+1,ar-r,ag-g,ab-b,4); |
---|
95 | add_error(i+2,j+1,ar-r,ag-g,ab-b,2); |
---|
96 | } |
---|
97 | } |
---|
98 | return dest; |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|