dostunnel

annotate src/palman.c @ 1:5d7f784002b0

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 15 Mar 2013 16:49:32 +0200
parents
children
rev   line source
nuclear@0 1 /*
nuclear@0 2 256-color 3D graphics hack for real-mode DOS.
nuclear@0 3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@0 4
nuclear@0 5 This program is free software: you can redistribute it and/or modify
nuclear@0 6 it under the terms of the GNU General Public License as published by
nuclear@0 7 the Free Software Foundation, either version 3 of the License, or
nuclear@0 8 (at your option) any later version.
nuclear@0 9
nuclear@0 10 This program is distributed in the hope that it will be useful,
nuclear@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@0 13 GNU General Public License for more details.
nuclear@0 14
nuclear@0 15 You should have received a copy of the GNU General Public License
nuclear@0 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@0 17 */
nuclear@0 18
nuclear@0 19 #include <string.h>
nuclear@0 20 #include "palman.h"
nuclear@0 21
nuclear@0 22 #define MAX_COLORS 256
nuclear@0 23 static struct palm_color colors[MAX_COLORS];
nuclear@0 24 static int base_index[MAX_COLORS];
nuclear@0 25
nuclear@0 26 static struct palm_color pal[MAX_COLORS];
nuclear@0 27 static unsigned int ncol, pcol;
nuclear@0 28 static int range;
nuclear@0 29
nuclear@0 30 void palm_clear(void)
nuclear@0 31 {
nuclear@0 32 ncol = 0;
nuclear@0 33 }
nuclear@0 34
nuclear@0 35 int palm_add_color(unsigned char r, unsigned char g, unsigned char b)
nuclear@0 36 {
nuclear@0 37 if(ncol >= MAX_COLORS) {
nuclear@0 38 return -1;
nuclear@0 39 }
nuclear@0 40 colors[ncol].r = r;
nuclear@0 41 colors[ncol].g = g;
nuclear@0 42 colors[ncol].b = b;
nuclear@0 43 ncol++;
nuclear@0 44 return 0;
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 int palm_color_index(unsigned char r, unsigned char g, unsigned char b)
nuclear@0 48 {
nuclear@0 49 int i;
nuclear@0 50
nuclear@0 51 for(i=0; i<ncol; i++) {
nuclear@0 52 if(colors[i].r == r && colors[i].g == g && colors[i].b == b) {
nuclear@0 53 return i;
nuclear@0 54 }
nuclear@0 55 }
nuclear@0 56 return -1;
nuclear@0 57 }
nuclear@0 58
nuclear@0 59 /* TODO: build an octree */
nuclear@0 60 int palm_build(void)
nuclear@0 61 {
nuclear@0 62 int i, j;
nuclear@0 63
nuclear@0 64 if(!ncol) {
nuclear@0 65 return -1;
nuclear@0 66 }
nuclear@0 67
nuclear@0 68 /* gradient range for each color */
nuclear@0 69 range = MAX_COLORS / ncol;
nuclear@0 70
nuclear@0 71 if(range <= 1) {
nuclear@0 72 memcpy(pal, colors, ncol * sizeof *pal);
nuclear@0 73 return 0;
nuclear@0 74 }
nuclear@0 75 pcol = 0;
nuclear@0 76
nuclear@0 77 for(i=0; i<ncol; i++) {
nuclear@0 78 unsigned short r, g, b, dr, dg, db;
nuclear@0 79
nuclear@0 80 base_index[i] = pcol;
nuclear@0 81
nuclear@0 82 dr = ((unsigned short)colors[i].r << 8) / (range - 1);
nuclear@0 83 dg = ((unsigned short)colors[i].g << 8) / (range - 1);
nuclear@0 84 db = ((unsigned short)colors[i].b << 8) / (range - 1);
nuclear@0 85
nuclear@0 86 r = g = b = 0;
nuclear@0 87 for(j=0; j<range; j++) {
nuclear@0 88 pal[pcol].r = (unsigned char)(r >> 8);
nuclear@0 89 pal[pcol].g = (unsigned char)(g >> 8);
nuclear@0 90 pal[pcol].b = (unsigned char)(b >> 8);
nuclear@0 91 pcol++;
nuclear@0 92 r += dr;
nuclear@0 93 g += dg;
nuclear@0 94 b += db;
nuclear@0 95 }
nuclear@0 96 }
nuclear@0 97 return pcol;
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 struct palm_color *palm_palette(void)
nuclear@0 101 {
nuclear@0 102 return pal;
nuclear@0 103 }
nuclear@0 104
nuclear@0 105 int palm_palette_size(void)
nuclear@0 106 {
nuclear@0 107 return pcol;
nuclear@0 108 }
nuclear@0 109
nuclear@0 110
nuclear@0 111 int palm_color_base(unsigned char r, unsigned char g, unsigned char b)
nuclear@0 112 {
nuclear@0 113 int c;
nuclear@0 114
nuclear@0 115 if((c = palm_color_index(r, g, b)) == -1) {
nuclear@0 116 return -1;
nuclear@0 117 }
nuclear@0 118 return base_index[c];
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 int palm_color_range(void)
nuclear@0 122 {
nuclear@0 123 return range;
nuclear@0 124 }