| 1 | greg | 1.1 | /* Copyright (c) 1990 Regents of the University of California */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | * Integer operations on COLR scanlines | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include "color.h" | 
| 12 |  |  |  | 
| 13 | greg | 1.3 | #define MAXGSHIFT       15              /* maximum shift for gamma table */ | 
| 14 | greg | 1.1 |  | 
| 15 |  |  | static BYTE     g_mant[256], g_nexp[256]; | 
| 16 |  |  |  | 
| 17 |  |  | static BYTE     g_bval[MAXGSHIFT+1][256]; | 
| 18 |  |  |  | 
| 19 |  |  |  | 
| 20 |  |  | setcolrgam(g)                   /* set gamma conversion */ | 
| 21 |  |  | double  g; | 
| 22 |  |  | { | 
| 23 |  |  | extern double   pow(); | 
| 24 |  |  | double  mult; | 
| 25 |  |  | register int    i, j; | 
| 26 |  |  | /* compute colr -> gamb mapping */ | 
| 27 |  |  | for (i = 0; i <= MAXGSHIFT; i++) { | 
| 28 |  |  | mult = pow(0.5, (double)(i+8)); | 
| 29 |  |  | for (j = 0; j < 256; j++) | 
| 30 |  |  | g_bval[i][j] = 256.0 * pow((j+.5)*mult, 1.0/g); | 
| 31 |  |  | } | 
| 32 |  |  | /* compute gamb -> colr mapping */ | 
| 33 |  |  | i = 0; | 
| 34 |  |  | mult = 256.0; | 
| 35 | greg | 1.2 | for (j = 255; j > 0; j--) { | 
| 36 |  |  | while ((g_mant[j] = mult * pow(j/256.0, g)) < 128) { | 
| 37 | greg | 1.1 | i++; | 
| 38 |  |  | mult *= 2.0; | 
| 39 |  |  | } | 
| 40 |  |  | g_nexp[j] = i; | 
| 41 |  |  | } | 
| 42 | greg | 1.2 | g_mant[0] = 0; | 
| 43 |  |  | g_nexp[0] = COLXS; | 
| 44 | greg | 1.1 | } | 
| 45 |  |  |  | 
| 46 |  |  |  | 
| 47 |  |  | colrs_gambs(scan, len)          /* convert scanline of colrs to gamma bytes */ | 
| 48 |  |  | register COLR   *scan; | 
| 49 |  |  | int     len; | 
| 50 |  |  | { | 
| 51 |  |  | register int    i, expo; | 
| 52 |  |  |  | 
| 53 |  |  | while (len-- > 0) { | 
| 54 |  |  | expo = scan[0][EXP] - COLXS; | 
| 55 |  |  | if (expo < -MAXGSHIFT) { | 
| 56 |  |  | if (expo < -MAXGSHIFT-8) { | 
| 57 |  |  | scan[0][RED] = | 
| 58 |  |  | scan[0][GRN] = | 
| 59 |  |  | scan[0][BLU] = 0; | 
| 60 |  |  | } else { | 
| 61 |  |  | i = (-MAXGSHIFT-1) - expo; | 
| 62 |  |  | scan[0][RED] = | 
| 63 |  |  | g_bval[MAXGSHIFT][((scan[0][RED]>>i)+1)>>1]; | 
| 64 |  |  | scan[0][GRN] = | 
| 65 |  |  | g_bval[MAXGSHIFT][((scan[0][GRN]>>i)+1)>>1]; | 
| 66 |  |  | scan[0][BLU] = | 
| 67 |  |  | g_bval[MAXGSHIFT][((scan[0][BLU]>>i)+1)>>1]; | 
| 68 |  |  | } | 
| 69 |  |  | } else if (expo > 0) { | 
| 70 |  |  | if (expo > 8) { | 
| 71 |  |  | scan[0][RED] = | 
| 72 |  |  | scan[0][GRN] = | 
| 73 |  |  | scan[0][BLU] = 255; | 
| 74 |  |  | } else { | 
| 75 |  |  | i = (scan[0][RED]<<1 | 1) << (expo-1); | 
| 76 |  |  | scan[0][RED] = i > 255 ? 255 : g_bval[0][i]; | 
| 77 |  |  | i = (scan[0][GRN]<<1 | 1) << (expo-1); | 
| 78 |  |  | scan[0][GRN] = i > 255 ? 255 : g_bval[0][i]; | 
| 79 |  |  | i = (scan[0][BLU]<<1 | 1) << (expo-1); | 
| 80 |  |  | scan[0][BLU] = i > 255 ? 255 : g_bval[0][i]; | 
| 81 |  |  | } | 
| 82 |  |  | } else { | 
| 83 |  |  | scan[0][RED] = g_bval[-expo][scan[0][RED]]; | 
| 84 |  |  | scan[0][GRN] = g_bval[-expo][scan[0][GRN]]; | 
| 85 |  |  | scan[0][BLU] = g_bval[-expo][scan[0][BLU]]; | 
| 86 |  |  | } | 
| 87 |  |  | scan[0][EXP] = COLXS; | 
| 88 |  |  | scan++; | 
| 89 |  |  | } | 
| 90 |  |  | } | 
| 91 |  |  |  | 
| 92 |  |  |  | 
| 93 |  |  | gambs_colrs(scan, len)          /* convert gamma bytes to colr scanline */ | 
| 94 |  |  | register COLR   *scan; | 
| 95 |  |  | int     len; | 
| 96 |  |  | { | 
| 97 |  |  | register int    nexpo; | 
| 98 |  |  |  | 
| 99 |  |  | while (len-- > 0) { | 
| 100 |  |  | nexpo = g_nexp[scan[0][RED]]; | 
| 101 |  |  | if (g_nexp[scan[0][GRN]] < nexpo) | 
| 102 |  |  | nexpo = g_nexp[scan[0][GRN]]; | 
| 103 |  |  | if (g_nexp[scan[0][BLU]] < nexpo) | 
| 104 |  |  | nexpo = g_nexp[scan[0][BLU]]; | 
| 105 |  |  | if (nexpo < g_nexp[scan[0][RED]]) | 
| 106 |  |  | scan[0][RED] = g_mant[scan[0][RED]] | 
| 107 |  |  | >> (g_nexp[scan[0][RED]]-nexpo); | 
| 108 |  |  | else | 
| 109 |  |  | scan[0][RED] = g_mant[scan[0][RED]]; | 
| 110 |  |  | if (nexpo < g_nexp[scan[0][GRN]]) | 
| 111 |  |  | scan[0][GRN] = g_mant[scan[0][GRN]] | 
| 112 |  |  | >> (g_nexp[scan[0][GRN]]-nexpo); | 
| 113 |  |  | else | 
| 114 |  |  | scan[0][GRN] = g_mant[scan[0][GRN]]; | 
| 115 |  |  | if (nexpo < g_nexp[scan[0][BLU]]) | 
| 116 |  |  | scan[0][BLU] = g_mant[scan[0][BLU]] | 
| 117 |  |  | >> (g_nexp[scan[0][BLU]]-nexpo); | 
| 118 |  |  | else | 
| 119 |  |  | scan[0][BLU] = g_mant[scan[0][BLU]]; | 
| 120 |  |  | scan[0][EXP] = COLXS - nexpo; | 
| 121 |  |  | scan++; | 
| 122 |  |  | } | 
| 123 |  |  | } | 
| 124 |  |  |  | 
| 125 |  |  |  | 
| 126 |  |  | shiftcolrs(scan, len, adjust)   /* shift a scanline of colors by 2^adjust */ | 
| 127 |  |  | register COLR   *scan; | 
| 128 |  |  | register int    len; | 
| 129 |  |  | register int    adjust; | 
| 130 |  |  | { | 
| 131 | greg | 2.2 | int     minexp; | 
| 132 |  |  |  | 
| 133 |  |  | if (adjust == 0) | 
| 134 |  |  | return; | 
| 135 |  |  | minexp = adjust < 0 ? -adjust : 0; | 
| 136 | greg | 1.1 | while (len-- > 0) { | 
| 137 | greg | 2.2 | if (scan[0][EXP] <= minexp) | 
| 138 |  |  | scan[0][RED] = scan[0][GRN] = scan[0][BLU] = | 
| 139 |  |  | scan[0][EXP] = 0; | 
| 140 |  |  | else | 
| 141 |  |  | scan[0][EXP] += adjust; | 
| 142 | greg | 1.1 | scan++; | 
| 143 |  |  | } | 
| 144 |  |  | } | 
| 145 |  |  |  | 
| 146 |  |  |  | 
| 147 |  |  | normcolrs(scan, len, adjust)    /* normalize a scanline of colrs */ | 
| 148 |  |  | register COLR  *scan; | 
| 149 |  |  | int  len; | 
| 150 |  |  | int  adjust; | 
| 151 |  |  | { | 
| 152 |  |  | register int  c; | 
| 153 |  |  | register int  shift; | 
| 154 |  |  |  | 
| 155 |  |  | while (len-- > 0) { | 
| 156 |  |  | shift = scan[0][EXP] + adjust - COLXS; | 
| 157 |  |  | if (shift > 0) { | 
| 158 |  |  | if (shift > 8) { | 
| 159 |  |  | scan[0][RED] = | 
| 160 |  |  | scan[0][GRN] = | 
| 161 |  |  | scan[0][BLU] = 255; | 
| 162 |  |  | } else { | 
| 163 |  |  | shift--; | 
| 164 |  |  | c = (scan[0][RED]<<1 | 1) << shift; | 
| 165 |  |  | scan[0][RED] = c > 255 ? 255 : c; | 
| 166 |  |  | c = (scan[0][GRN]<<1 | 1) << shift; | 
| 167 |  |  | scan[0][GRN] = c > 255 ? 255 : c; | 
| 168 |  |  | c = (scan[0][BLU]<<1 | 1) << shift; | 
| 169 |  |  | scan[0][BLU] = c > 255 ? 255 : c; | 
| 170 |  |  | } | 
| 171 |  |  | } else if (shift < 0) { | 
| 172 |  |  | if (shift < -8) { | 
| 173 |  |  | scan[0][RED] = | 
| 174 |  |  | scan[0][GRN] = | 
| 175 |  |  | scan[0][BLU] = 0; | 
| 176 |  |  | } else { | 
| 177 |  |  | shift = -1-shift; | 
| 178 |  |  | scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1; | 
| 179 |  |  | scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1; | 
| 180 |  |  | scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1; | 
| 181 |  |  | } | 
| 182 |  |  | } | 
| 183 |  |  | scan[0][EXP] = COLXS - adjust; | 
| 184 |  |  | scan++; | 
| 185 |  |  | } | 
| 186 |  |  | } |