File: colormap.c

package info (click to toggle)
allegro4.4 2%3A4.4.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,920 kB
  • ctags: 39,951
  • sloc: ansic: 164,225; asm: 17,620; cpp: 3,848; objc: 1,687; sh: 1,131; python: 676; pascal: 179; makefile: 48; perl: 29; lisp: 1
file content (219 lines) | stat: -rw-r--r-- 5,828 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*         ______   ___    ___
 *        /\  _  \ /\_ \  /\_ \ 
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___ 
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      Color (translucency and lighting) mapping table construction utility.
 *
 *      By Shawn Hargreaves.
 *
 *      See readme.txt for copyright information.
 */


#define ALLEGRO_USE_CONSOLE

#include <stdio.h>
#include <string.h>

#include "allegro.h"



void usage(void)
{
   printf("\nColor map construction utility for Allegro " ALLEGRO_VERSION_STR ", " ALLEGRO_PLATFORM_STR);
   printf("\nBy Shawn Hargreaves, " ALLEGRO_DATE_STR "\n\n");
   printf("Usage: colormap <mode> palfile.[pcx|bmp] outputfile [r g b] [modifiers]\n\n");
   printf("   Reads a palette from the input file, and writes out a 64k mapping\n");
   printf("   table for implementing lighting and translucency effects (in a\n");
   printf("   suitable format for use with the global color_map pointer).\n");
   printf("\n");
   printf("   The mode can be either 'light', to build a lighting table, or 'trans',\n");
   printf("   to build a translucency table.\n");
   printf("\n");
   printf("   The r, g, and b values control the color blending formula. For lighting\n");
   printf("   tables they specify the color to use at light level zero, in VGA 0-63\n");
   printf("   format (default black). For translucency tables they specify the degree\n");
   printf("   of translucency for each color component, ranging 0-255 (default 128,\n");
   printf("   0=invisible, 255=solid).\n");
   printf("\n");
   printf("Modifiers:\n");
   printf("   'ss=x' - makes source color #x completely solid\n");
   printf("   'st=x' - makes source color #x completely transparent\n");
   printf("   'ds=x' - makes destination color #x completely solid\n");
   printf("   'dt=x' - makes destination color #x completely transparent\n");
}



int check_color_value(char *s, int def, int min, int max, char *t)
{
   int val;

   if (!s)
      return def;

   val = strtol(s, NULL, 0);

   if ((val < min) || (val > max)) {
      printf("Error: %s value %d is invalid (range is %d to %d)\n", t, val, min, max);
      exit(1);
   }

   return val;
}



PALETTE the_pal;
COLOR_MAP the_map;



void modifier(char *s)
{
   int col, i;

   if (((utolower(s[0]) != 's') && (utolower(s[0]) != 'd')) ||
       ((utolower(s[1]) != 's') && (utolower(s[1]) != 't')) ||
       (s[2] != '=')) {
      printf("Error: unknown modifier '%s'\n", s);
      exit(1);
   } 

   col = strtol(s+3, NULL, 0);
   if ((col < 0) || (col > 255)) {
      printf("Error: modifier color %d is invalid (range is 0 to 255)\n", col);
      exit(1);
   }

   if (utolower(s[0] == 's')) {
      if (utolower(s[1] == 's')) {
	 printf("Making source color #%d solid\n", col);
	 for (i=0; i<PAL_SIZE; i++)
	    the_map.data[col][i] = col;
      }
      else {
	 printf("Making source color #%d transparent\n", col);
	 for (i=0; i<PAL_SIZE; i++)
	    the_map.data[col][i] = i;
      }
   }
   else {
      if (utolower(s[1] == 's')) {
	 printf("Making destination color #%d solid\n", col);
	 for (i=0; i<PAL_SIZE; i++)
	    the_map.data[i][col] = col;
      }
      else {
	 printf("Making destination color #%d transparent\n", col);
	 for (i=0; i<PAL_SIZE; i++)
	    the_map.data[i][col] = i;
      }
   }
}



int main(int argc, char *argv[])
{
   BITMAP *bmp;
   PACKFILE *f;
   int trans = TRUE;
   char *infile = NULL;
   char *outfile = NULL;
   char *r = NULL;
   char *g = NULL;
   char *b = NULL;
   int rval, gval, bval;
   int i = 1;

   if (argc > i) {
      if (stricmp(argv[i], "light") == 0) {
	 trans = FALSE;
	 i++;
      }
      else if (stricmp(argv[i], "trans") == 0) {
	 trans = TRUE;
	 i++;
      }
   }

   if (argc > i)
      infile = argv[i++];

   if (argc > i)
      outfile = argv[i++];

   if ((argc > i) && (!strchr(argv[i], '=')))
      r = argv[i++];

   if ((argc > i) && (!strchr(argv[i], '=')))
      g = argv[i++];

   if ((argc > i) && (!strchr(argv[i], '=')))
      b = argv[i++];

   if ((!infile) || (!outfile)) {
      usage();
      return 1;
   }

   if (install_allegro(SYSTEM_NONE, &errno, atexit) != 0)
      return 1;
   set_color_conversion(COLORCONV_NONE);

   bmp = load_bitmap(infile, the_pal);
   if (!bmp) {
      printf("Error reading palette from '%s'\n", infile);
      return 1;
   }

   destroy_bitmap(bmp);

   printf("Palette read from '%s'\n", infile);

   if (trans) {
      rval = check_color_value(r, 128, 0, 255, "red");
      gval = check_color_value(g, 128, 0, 255, "green");
      bval = check_color_value(b, 128, 0, 255, "blue");

      printf("Solidity: red=%d%%, green=%d%%, blue=%d%%\n", rval*100/255, gval*100/255, bval*100/255);
      printf("Creating translucency color map\n");

      create_trans_table(&the_map, the_pal, rval, gval, bval, NULL);
   }
   else {
      rval = check_color_value(r, 0, 0, 63, "red");
      gval = check_color_value(g, 0, 0, 63, "green");
      bval = check_color_value(b, 0, 0, 63, "blue");

      printf("Light color: red=%d, green=%d, blue=%d\n", rval, gval, bval);
      printf("Creating lighting color map\n");

      create_light_table(&the_map, the_pal, rval, gval, bval, NULL);
   }

   while (i < argc)
      modifier(argv[i++]);

   f = pack_fopen(outfile, F_WRITE);
   if (!f) {
      printf("Error writing '%s'\n", outfile);
      return 1;
   }

   pack_fwrite(&the_map, sizeof(the_map), f);
   pack_fclose(f);

   printf("Color mapping table written to '%s'\n", outfile);
   return 0;
}

END_OF_MAIN()