transpal.cpp
1 // "Build Engine & Tools" Copyright (c) 1993-1997 Ken Silverman 2 // Ken Silverman's official web site: "http://www.advsys.net/ken" 3 // See the included license file "BUILDLIC.TXT" for license info. 4 // 5 // This file has been modified from Ken Silverman's original release 6 // by Jonathon Fowler (jf@jonof.id.au) 7 8 #include "compat.h" 9 #include "pragmas.h" 10 #include "colmatch.h" 11 12 #define MAXPALOOKUPS 256 13 14 static int numshades, transratio; 15 static char const * const palettefilename = "palette.dat"; 16 static uint8_t origpalookup[MAXPALOOKUPS<<8], palookup[MAXPALOOKUPS<<8], transluc[65536]; 17 static uint8_t origpalette[768], palette[768]; 18 19 static char getshade(char dashade, char dacol) 20 { 21 int r, g, b, t; 22 char *ptr; 23 24 ptr = (char *)&palette[dacol*3]; 25 t = divscale16(numshades-dashade,numshades); 26 r = ((ptr[0]*t+32768)>>16); 27 g = ((ptr[1]*t+32768)>>16); 28 b = ((ptr[2]*t+32768)>>16); 29 return(paletteGetClosestColor(r,g,b)); 30 } 31 32 static char gettrans(char dat1, char dat2, int datransratio) 33 { 34 int r, g, b; 35 char *ptr, *ptr2; 36 37 ptr = (char *)&palette[dat1*3]; 38 ptr2 = (char *)&palette[dat2*3]; 39 r = ptr[0]; r += (((ptr2[0]-r)*datransratio+128)>>8); 40 g = ptr[1]; g += (((ptr2[1]-g)*datransratio+128)>>8); 41 b = ptr[2]; b += (((ptr2[2]-b)*datransratio+128)>>8); 42 return(paletteGetClosestColor(r,g,b)); 43 } 44 45 int main(int argc, char **argv) 46 { 47 char col, ch; 48 short orignumshades; 49 int fil, i, j, rscale, gscale, bscale; 50 51 ch = 13; 52 if (argc>1) { 53 if (argv[1][0] == '-') { 54 if (argv[1][1] == 't') { ch = 32; puts("Updating translucency table ONLY"); } 55 argc--; 56 argv++; 57 } 58 } 59 60 if ((argc != 3) && (argc != 6)) 61 { 62 Bprintf("TRANSPAL [-t]<numshades><trans#(0-inv,256-opa)><r><g><b> by Kenneth Silverman\n"); 63 Bprintf(" Ex #1: transpal 32 170 30 59 11 (I use these values in my BUILD demo)\n"); 64 Bprintf(" \xc0\xc4\xc4\xc1\xc4\xc4\xc1\xc4\xc4\xc4 The RGB scales are optional\n"); 65 Bprintf(" Ex #2: transpal 64 160\n\n"); 66 Bprintf("Once tables are generated, the optional -t switch determines what to save:\n"); 67 Bprintf(" Exclude -t to update both the shade table and transluscent table\n"); 68 Bprintf(" Include -t to update the transluscent table ONLY\n"); 69 exit(0); 70 } 71 72 numshades = Batol(argv[1]); 73 transratio = Batol(argv[2]); 74 75 if (argc == 6) 76 { 77 rscale = Batol(argv[3]); 78 gscale = Batol(argv[4]); 79 bscale = Batol(argv[5]); 80 } 81 else 82 { 83 rscale = 30; 84 gscale = 59; 85 bscale = 11; 86 } 87 88 if ((numshades < 1) || (numshades > 256)) 89 { Bprintf("Invalid number of shades\n"); exit(0); } 90 if ((transratio < 0) || (transratio > 256)) 91 { Bprintf("Invalid transluscent ratio\n"); exit(0); } 92 93 if ((fil = Bopen(palettefilename,BO_BINARY|BO_RDONLY,BS_IREAD)) == -1) 94 { 95 Bprintf("%s not found",palettefilename); 96 return(0); 97 } 98 Bread(fil,origpalette,768); 99 Bread(fil,&orignumshades,2); orignumshades = B_LITTLE16(orignumshades); 100 orignumshades = min(max<int>(orignumshades,1),256); 101 Bread(fil,origpalookup,(int)orignumshades<<8); 102 Bclose(fil); 103 104 for (int k = 0; k < 768; k++) 105 palette[k] = origpalette[k] << 2; 106 107 initdivtables(); 108 paletteInitClosestColorScale(rscale,gscale,bscale); 109 paletteInitClosestColorMap(palette); 110 paletteInitClosestColorGrid(); 111 112 for(i=0;i<numshades;i++) 113 for(j=0;j<256;j++) 114 { 115 col = getshade((char)i,(char)j); 116 palookup[(i<<8)+j] = col; 117 } 118 119 for(i=0;i<256;i++) 120 for(j=0;j<256;j++) 121 { 122 col = gettrans((char)i,(char)j,transratio); 123 transluc[(i<<8)+j] = col; 124 } 125 126 if (ch == 13) 127 { 128 short s; 129 if ((fil = Bopen(palettefilename,BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY,BS_IREAD|BS_IWRITE)) == -1) 130 { Bprintf("Couldn't save file %s",palettefilename); return(0); } 131 Bwrite(fil,origpalette,768); 132 s = B_LITTLE16(numshades); Bwrite(fil,&s,2); 133 Bwrite(fil,palookup,numshades<<8); 134 Bwrite(fil,transluc,65536); 135 Bclose(fil); 136 Bprintf("Shade table AND transluscent table updated\n"); 137 } 138 else if (ch == 32) 139 { 140 short s; 141 if ((fil = Bopen(palettefilename,BO_BINARY|BO_TRUNC|BO_CREAT|BO_WRONLY,BS_IREAD|BS_IWRITE)) == -1) 142 { Bprintf("Couldn't save file %s",palettefilename); return(0); } 143 Bwrite(fil,origpalette,768); 144 s = B_LITTLE16(orignumshades); Bwrite(fil,&s,2); 145 Bwrite(fil,origpalookup,(int)orignumshades<<8); 146 Bwrite(fil,transluc,65536); 147 Bclose(fil); 148 Bprintf("Transluscent table updated\n"); 149 } 150 else 151 Bprintf("Palette file wasn't touched\n"); 152 153 return 0; 154 }