ScummVM API documentation
pal_render.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * of the License, or(at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AGS_PLUGINS_AGS_PAL_RENDER_PAL_RENDER_H
23 #define AGS_PLUGINS_AGS_PAL_RENDER_PAL_RENDER_H
24 
25 #include "ags/lib/allegro.h"
26 #include "ags/plugins/ags_plugin.h"
27 #include "ags/plugins/plugin_base.h"
28 #include "common/algorithm.h"
29 
30 namespace AGS3 {
31 namespace Plugins {
32 namespace AGSPalRender {
33 
34 struct PALSTRUCT {
35  byte r;
36  byte b;
37  byte g;
38 };
39 
40 extern IAGSEngine *engine;
41 extern unsigned char clut[65536];
42 extern unsigned char cycle_remap [256];
43 extern const int alphamultiply [4096];
44 extern PALSTRUCT objectivepal[256];
45 
46 // this class exists solely to take advantage of g++'s
47 // -fvisibility-inlines-hidden option, so that these
48 // methods can be inlined without any trace or complaint
49 class Mix {
50 public:
51 //unsigned char MixColorAlpha (unsigned char fg,unsigned char bg,unsigned char alpha);
52 //unsigned char MixColorAdditive (unsigned char fg,unsigned char bg,unsigned char alpha);
53  static unsigned char MixColorAlpha(unsigned char fg, unsigned char bg, unsigned char alpha, int use_objpal = 0) {
54  unsigned char rfg = cycle_remap[fg]; //Automatic remapping of palette slots.
55  //unsigned char rbg = cycle_remap [bg]; //Saves on typing elsewhere.
56  AGSColor *palette = engine->GetPalette();
57  int i = 0;
58  //int out_r = (palette[fg].r>>1) * alpha + (palette[bg].r>>1) * (255 - alpha);
59  //int out_g = palette[fg].g * alpha + palette[bg].g * (255 - alpha);
60  //int out_b = (palette[fg].b>>1) * alpha + (palette[bg].b>>1) * (255 - alpha);
61  int out_r, out_g, out_b;
62  if (use_objpal == 0) {
63  out_r = (objectivepal[rfg].r >> 1) *alpha + (palette[bg].r >> 1) *(255 - alpha);
64  out_g = objectivepal[rfg].g * alpha + palette[bg].g * (255 - alpha);
65  out_b = (objectivepal[rfg].b >> 1) *alpha + (palette[bg].b >> 1) *(255 - alpha);
66  } else {
67  out_r = (objectivepal[rfg].r >> 1) *alpha + (objectivepal[bg].r >> 1) *(255 - alpha);
68  out_g = objectivepal[rfg].g * alpha + objectivepal[bg].g * (255 - alpha);
69  out_b = (objectivepal[rfg].b >> 1) *alpha + (objectivepal[bg].b >> 1) *(255 - alpha);
70  }
71  //char ralpha = MAX(0,MIN(63,alpha>>2));
72  //unsigned char invralpha = 64-ralpha;
73  //if (ralpha > alpha) engine->AbortGame ("oops");
74  //int out_r = alphamultiply[((palette[fg].r>>1)<<6) +ralpha] + alphamultiply[((palette[bg].r>>1)<<6) +(invralpha)];
75  //int out_g = alphamultiply[((palette[fg].g) <<6) +ralpha] + alphamultiply[((palette[bg].g) <<6) +(invralpha)];
76  //int out_b = alphamultiply[((palette[fg].b>>1)<<6) +ralpha] + alphamultiply[((palette[bg].b>>1)<<6) +(invralpha)];
77  out_r = (out_r + 1 + (out_r >> 8)) >> 8;
78  out_g = (out_g + 1 + (out_g >> 8)) >> 8;
79  out_b = (out_b + 1 + (out_b >> 8)) >> 8;
80  //out_r = (out_r + 1 + (out_r >> 6)) >> 6;
81  //out_g = (out_g + 1 + (out_g >> 6)) >> 6;
82  //out_b = (out_b + 1 + (out_b >> 6)) >> 6;
83  i = ((out_r << 11) | (out_g << 5) | out_b);
84  unsigned char *clutp = clut;
85  //unsigned char result = cycle_remap [clut[i>>8][i%256]]; //Once again, to make sure that the palette slot used is the right one.
86  return cycle_remap[*(clutp + i)]; //Once again, to make sure that the palette slot used is the right one.
87  //engine->ReleaseBitmapSurface (clutspr);
88  }
89 
90  static unsigned char MixColorLightLevel(unsigned char fg, unsigned char intensity) {
91  unsigned char rfg = cycle_remap [fg]; //Automatic remapping of palette slots.
92  int i = 0;
93  //int dark_r = (((palette[fg].r>>1) * (intensity))>>8);
94  //int dark_b = (((palette[fg].b>>1) * (intensity))>>8);
95  //int dark_g = (((palette[fg].g) * (intensity))>>8);
96  int dark_r = (((objectivepal[rfg].r >> 1) * (intensity)) >> 8);
97  int dark_b = (((objectivepal[rfg].b >> 1) * (intensity)) >> 8);
98  int dark_g = (((objectivepal[rfg].g) * (intensity)) >> 8);
99  i = ((dark_r << 11) | (dark_g << 5) | dark_b);
100  unsigned char *clutp = clut;
101  return cycle_remap [*(clutp + i)]; //Once again, to make sure that the palette slot used is the right one.
102  }
103 
104  static unsigned char MixColorAdditive(unsigned char fg, unsigned char bg, unsigned char alpha, int use_objpal = 0) {
105  unsigned char rfg = cycle_remap[fg]; //Automatic remapping of palette slots.
106  //unsigned char rbg = cycle_remap[bg]; //Saves on typing elsewhere.
107  //BITMAP *clutspr = engine->GetSpriteGraphic (clutslot);
108  //if (!clutspr) engine->AbortGame ("MixColorAlpha: Can't load CLUT sprite into memory.");
109  //uint8 *clutarray = engine->GetRawBitmapSurface (clutspr);
110  AGSColor *palette = engine->GetPalette();
111  int i = 0;
112  int add_r, add_b, add_g = 0;
113  //char ralpha = MAX(0,MIN(63,alpha>>2));
114  //add_r = (((palette[fg].r>>1) * (alpha))>>8);
115  //add_b = (((palette[fg].b>>1) * (alpha))>>8);
116  //add_g = (((palette[fg].g) * (alpha))>>8);
117  add_r = (((objectivepal[rfg].r >> 1) * (alpha)) >> 8);
118  add_b = (((objectivepal[rfg].b >> 1) * (alpha)) >> 8);
119  add_g = (((objectivepal[rfg].g) * (alpha)) >> 8);
120  //int a_g = MAX(0,MIN(63,alpha>>2));
121  //add_r = ((alphamultiply[(palette[fg].r>>1)<<6)+ralpha])>>6);
122  //add_b = ((alphamultiply[(palette[fg].b>>1)<<6)+ralpha])>>6);
123  //add_g = ((alphamultiply[(palette[fg].g) <<6)+ralpha])>>6);
124  //int out_r = MIN(31,(palette[bg].r>>1) + add_r);
125  //int out_g = MIN(63, palette[bg].g + add_g);
126  //int out_b = MIN(31,(palette[bg].b>>1) + add_b);
127  int out_r, out_g, out_b;
128  if (use_objpal == 0) {
129  out_r = MIN(31, (palette[bg].r >> 1) + add_r);
130  out_g = MIN(63, palette[bg].g + add_g);
131  out_b = MIN(31, (palette[bg].b >> 1) + add_b);
132  } else {
133  out_r = MIN(31, (objectivepal [bg].r >> 1) + add_r);
134  out_g = MIN(63, objectivepal [bg].g + add_g);
135  out_b = MIN(31, (objectivepal [bg].b >> 1) + add_b);
136  }
137  i = ((out_r << 11) | (out_g << 5) | out_b);
138  unsigned char *clutp = clut;
139  unsigned char result = cycle_remap [*(clutp + i)]; //Once again, to make sure that the palette slot used is the right one.
140  //unsigned char result = cycle_remap [clut[i>>8][i%256]]; //Once again, to make sure that the palette slot used is the right one.
141  //engine->ReleaseBitmapSurface (clutspr);
142  return result;
143  }
144 
145  static unsigned char MixColorMultiply(unsigned char fg, unsigned char bg, unsigned char alpha, int use_objpal = 0) {
146  unsigned char rfg = cycle_remap [fg]; //Automatic remapping of palette slots.
147  unsigned char rbg = cycle_remap [bg]; //Saves on typing elsewhere.
148  AGSColor *palette = engine->GetPalette();
149  int i = 0;
150  int mul_r, mul_b, mul_g = 0;
151  int out_r, out_g, out_b = 0;
152  if (use_objpal == 0) {
153  mul_r = ((objectivepal[rfg].r >> 1) * (palette[rbg].r >> 1)) / 64;
154  mul_b = ((objectivepal[rfg].b >> 1) * (palette[rbg].b >> 1)) / 64;
155  mul_g = ((objectivepal[rfg].g * palette[rbg].g) / 64);
156  out_r = ((palette[rbg].r >> 1) * (63 - (alpha / 4)) + (mul_r * (alpha / 4))) / 63;
157  out_g = (palette[rbg].g * (63 - (alpha / 4)) + (mul_g * (alpha / 4))) / 63;
158  out_b = ((palette[rbg].b >> 1) * (63 - (alpha / 4)) + (mul_b * (alpha / 4))) / 63;
159  } else {
160  mul_r = ((objectivepal[rfg].r >> 1) * (objectivepal[rbg].r >> 1)) / 64;
161  mul_b = ((objectivepal[rfg].b >> 1) * (objectivepal[rbg].b >> 1)) / 64;
162  mul_g = ((objectivepal[rfg].g * objectivepal[rbg].g) / 64);
163 
164  out_r = ((objectivepal[rbg].r >> 1) * (63 - (alpha / 4)) + (mul_r * (alpha / 4))) / 63;
165  out_g = (objectivepal[rbg].g * (63 - (alpha / 4)) + (mul_g * (alpha / 4))) / 63;
166  out_b = ((objectivepal[rbg].b >> 1) * (63 - (alpha / 4)) + (mul_b * (alpha / 4))) / 63;
167  }
168  i = ((out_r << 11) | (out_g << 5) | out_b);
169  unsigned char *clutp = clut;
170  unsigned char result = cycle_remap [*(clutp + i)]; //Once again, to make sure that the palette slot used is the right one.
171  //unsigned char result = cycle_remap [clut[i>>8][i%256]]; //Once again, to make sure that the palette slot used is the right one.
172  //engine->ReleaseBitmapSurface (clutspr);
173  return result;
174  }
175 };
176 
177 void GetColor565(ScriptMethodParams &params);
178 
179 unsigned short root(unsigned short x);
180 float FastSin(float x);
181 float FastCos(float x);
182 
183 } // namespace AGSPalRender
184 } // namespace Plugins
185 } // namespace AGS3
186 
187 #endif
Definition: plugin_base.h:131
Definition: ags_plugin.h:328
Definition: ags_plugin.h:79
Definition: pal_render.h:34
T MIN(T a, T b)
Definition: util.h:59
Definition: ags.h:40
Definition: pal_render.h:49