/*----------------------------------------------------------------------+ | invcmap.c - Microsoft Video 1 Compressor - Inverse Color Map. | | | | Copyright (c) 1990-1994 Microsoft Corporation. | | Portions Copyright Media Vision Inc. | | All Rights Reserved. | | | | You have a non-exclusive, worldwide, royalty-free, and perpetual | | license to use this source code in developing hardware, software | | (limited to drivers and other software required for hardware | | functionality), and firmware for video display and/or processing | | boards. Microsoft makes no warranties, express or implied, with | | respect to the Video 1 codec, including without limitation warranties | | of merchantability or fitness for a particular purpose. Microsoft | | shall not be liable for any damages whatsoever, including without | | limitation consequential damages arising from your use of the Video 1 | | codec. | | | | | +----------------------------------------------------------------------*/ #include #include #include #include //#pragma optimize("", off) int redloop(void); int greenloop( int restart ); int blueloop( int restart ); #ifdef _WIN32 #define maxfill(pbuffer, side) \ memset(pbuffer, -1, colormax*colormax*colormax*sizeof(LONG)) #else void maxfill( DWORD _huge *buffer, long side); #endif void inv_cmap_2( int colors, BYTE colormap[3][256], int bits, DWORD _huge *dist_buf, LPBYTE rgbmap ); void inv_cmap_1( int colors, BYTE colormap[3][256], int bits, DWORD _huge *dist_buf, LPBYTE rgbmap ); /* Track minimum and maximum in inv_cmap_2. */ #define MINMAX_TRACK BYTE NewMap[3][256]; LPVOID FAR PASCAL MakeITable(LPRGBQUAD lprgbq, int nColors) { LPVOID lpDistBuf; LPBYTE lpITable; int i; lpITable = GlobalAllocPtr(GHND|GMEM_SHARE,32768l); if (lpITable == NULL) return NULL; // error no memory lpDistBuf = (LPVOID)GlobalAllocPtr(GHND,32768l * sizeof(DWORD)); if (lpDistBuf == NULL) { GlobalFreePtr(lpITable); return NULL; // error no memory } for (i = 0; i < nColors; i++) { NewMap[0][i] = lprgbq[i].rgbRed; NewMap[1][i] = lprgbq[i].rgbGreen; NewMap[2][i] = lprgbq[i].rgbBlue; } inv_cmap_2(nColors,NewMap,5,lpDistBuf,lpITable); GlobalFreePtr(lpDistBuf); return lpITable; } static int bcenter, gcenter, rcenter; static long gdist, rdist, cdist; static long cbinc, cginc, crinc; static DWORD _huge *gdp; static DWORD _huge *rdp; static DWORD _huge *cdp; static LPBYTE grgbp; static LPBYTE rrgbp; static LPBYTE crgbp; static int gstride, rstride; static long x, xsqr, colormax; static int cindex; /***************************************************************** * TAG( inv_cmap_2 ) * * Compute an inverse colormap efficiently. * Inputs: * colors: Number of colors in the forward colormap. * colormap: The forward colormap. * bits: Number of quantization bits. The inverse * colormap will have (2^bits)^3 entries. * dist_buf: An array of (2^bits)^3 long integers to be * used as scratch space. * Outputs: * rgbmap: The output inverse colormap. The entry * rgbmap[(r<<(2*bits)) + (g<> nbits; gcenter = colormap[1][cindex] >> nbits; bcenter = colormap[2][cindex] >> nbits; rdist = colormap[0][cindex] - (rcenter * x + x/2); gdist = colormap[1][cindex] - (gcenter * x + x/2); cdist = colormap[2][cindex] - (bcenter * x + x/2); cdist = rdist*rdist + gdist*gdist + cdist*cdist; crinc = 2 * ((rcenter + 1) * xsqr - (colormap[0][cindex] * x)); cginc = 2 * ((gcenter + 1) * xsqr - (colormap[1][cindex] * x)); cbinc = 2 * ((bcenter + 1) * xsqr - (colormap[2][cindex] * x)); /* Array starting points. */ cdp = dist_buf + rcenter * rstride + gcenter * gstride + bcenter; crgbp = rgbmap + rcenter * rstride + gcenter * gstride + bcenter; (void)redloop(); } } /* redloop -- loop up and down from red center. */ int redloop() { int detect; int r, i = cindex; int first; long txsqr = xsqr + xsqr; static long rxx; detect = 0; /* Basic loop up. */ for ( r = rcenter, rdist = cdist, rxx = crinc, rdp = cdp, rrgbp = crgbp, first = 1; r < (int) colormax; r++, rdp += rstride, rrgbp += rstride, rdist += rxx, rxx += txsqr, first = 0 ) { if ( greenloop( first ) ) detect = 1; else if ( detect ) break; } /* Basic loop down. */ for ( r = rcenter - 1, rxx = crinc - txsqr, rdist = cdist - rxx, rdp = cdp - rstride, rrgbp = crgbp - rstride, first = 1; r >= 0; r--, rdp -= rstride, rrgbp -= rstride, rxx -= txsqr, rdist -= rxx, first = 0 ) { if ( greenloop( first ) ) detect = 1; else if ( detect ) break; } return detect; } /* greenloop -- loop up and down from green center. */ int greenloop( int restart ) { int detect; int g, i = cindex; int first; long txsqr = xsqr + xsqr; static int here, min, max; #ifdef MINMAX_TRACK static int prevmax, prevmin; int thismax, thismin; #endif static long ginc, gxx, gcdist; /* "gc" variables maintain correct */ static DWORD _huge *gcdp; /* values for bcenter position, */ static LPBYTE gcrgbp; /* despite modifications by blueloop */ /* to gdist, gdp, grgbp. */ if ( restart ) { here = gcenter; min = 0; max = (int) colormax - 1; ginc = cginc; #ifdef MINMAX_TRACK prevmax = 0; prevmin = (int) colormax; #endif } #ifdef MINMAX_TRACK thismin = min; thismax = max; #endif detect = 0; /* Basic loop up. */ for ( g = here, gcdist = gdist = rdist, gxx = ginc, gcdp = gdp = rdp, gcrgbp = grgbp = rrgbp, first = 1; g <= max; g++, gdp += gstride, gcdp += gstride, grgbp += gstride, gcrgbp += gstride, gdist += gxx, gcdist += gxx, gxx += txsqr, first = 0 ) { if ( blueloop( first ) ) { if ( !detect ) { /* Remember here and associated data! */ if ( g > here ) { here = g; rdp = gcdp; rrgbp = gcrgbp; rdist = gcdist; ginc = gxx; #ifdef MINMAX_TRACK thismin = here; #endif } detect = 1; } } else if ( detect ) { #ifdef MINMAX_TRACK thismax = g - 1; #endif break; } } /* Basic loop down. */ for ( g = here - 1, gxx = ginc - txsqr, gcdist = gdist = rdist - gxx, gcdp = gdp = rdp - gstride, gcrgbp = grgbp = rrgbp - gstride, first = 1; g >= min; g--, gdp -= gstride, gcdp -= gstride, grgbp -= gstride, gcrgbp -= gstride, gxx -= txsqr, gdist -= gxx, gcdist -= gxx, first = 0 ) { if ( blueloop( first ) ) { if ( !detect ) { /* Remember here! */ here = g; rdp = gcdp; rrgbp = gcrgbp; rdist = gcdist; ginc = gxx; #ifdef MINMAX_TRACK thismax = here; #endif detect = 1; } } else if ( detect ) { #ifdef MINMAX_TRACK thismin = g + 1; #endif break; } } #ifdef MINMAX_TRACK /* If we saw something, update the edge trackers. For now, only * tracks edges that are "shrinking" (min increasing, max * decreasing. */ if ( detect ) { if ( thismax < prevmax ) max = thismax; prevmax = thismax; if ( thismin > prevmin ) min = thismin; prevmin = thismin; } #endif return detect; } /* blueloop -- loop up and down from blue center. */ int blueloop( int restart ) { int detect; register DWORD _huge *dp; register LPBYTE rgbp; register long bdist, bxx; register int b, i = cindex; register long txsqr = xsqr + xsqr; register int lim; static int here, min, max; #ifdef MINMAX_TRACK static int prevmin, prevmax; int thismin, thismax; #endif /* MINMAX_TRACK */ static long binc; if ( restart ) { here = bcenter; min = 0; max = (int) colormax - 1; binc = cbinc; #ifdef MINMAX_TRACK prevmin = (int) colormax; prevmax = 0; #endif /* MINMAX_TRACK */ } detect = 0; #ifdef MINMAX_TRACK thismin = min; thismax = max; #endif /* Basic loop up. */ /* First loop just finds first applicable cell. */ for ( b = here, bdist = gdist, bxx = binc, dp = gdp, rgbp = grgbp, lim = max; b <= lim; b++, dp++, rgbp++, bdist += bxx, bxx += txsqr ) { if ( *dp > (DWORD)bdist ) { /* Remember new 'here' and associated data! */ if ( b > here ) { here = b; gdp = dp; grgbp = rgbp; gdist = bdist; binc = bxx; #ifdef MINMAX_TRACK thismin = here; #endif } detect = 1; break; } } /* Second loop fills in a run of closer cells. */ for ( ; b <= lim; b++, dp++, rgbp++, bdist += bxx, bxx += txsqr ) { if ( *dp > (DWORD)bdist ) { *dp = bdist; *rgbp = (BYTE) i; } else { #ifdef MINMAX_TRACK thismax = b - 1; #endif break; } } /* Basic loop down. */ /* Do initializations here, since the 'find' loop might not get * executed. */ lim = min; b = here - 1; bxx = binc - txsqr; bdist = gdist - bxx; dp = gdp - 1; rgbp = grgbp - 1; /* The 'find' loop is executed only if we didn't already find * something. */ if ( !detect ) for ( ; b >= lim; b--, dp--, rgbp--, bxx -= txsqr, bdist -= bxx ) { if ( *dp > (DWORD)bdist ) { /* Remember here! */ /* No test for b against here necessary because b < * here by definition. */ here = b; gdp = dp; grgbp = rgbp; gdist = bdist; binc = bxx; #ifdef MINMAX_TRACK thismax = here; #endif detect = 1; break; } } /* The 'update' loop. */ for ( ; b >= lim; b--, dp--, rgbp--, bxx -= txsqr, bdist -= bxx ) { if ( *dp > (DWORD)bdist ) { *dp = bdist; *rgbp = (BYTE) i; } else { #ifdef MINMAX_TRACK thismin = b + 1; #endif break; } } /* If we saw something, update the edge trackers. */ #ifdef MINMAX_TRACK if ( detect ) { /* Only tracks edges that are "shrinking" (min increasing, max * decreasing. */ if ( thismax < prevmax ) max = thismax; if ( thismin > prevmin ) min = thismin; /* Remember the min and max values. */ prevmax = thismax; prevmin = thismin; } #endif /* MINMAX_TRACK */ return detect; } #ifndef _WIN32 void maxfill( DWORD _huge *buffer, long side) { register unsigned long maxv = ~0uL; register long i; register DWORD _huge *bp; for ( i = colormax * colormax * colormax, bp = buffer; i > 0; i--, bp++ ) *bp = maxv; } #endif #ifdef CMAP1 /***************************************************************** * TAG( inv_cmap_1 ) * * Compute an inverse colormap efficiently. * Inputs: * colors: Number of colors in the forward colormap. * colormap: The forward colormap. * bits: Number of quantization bits. The inverse * colormap will have (2^bits)^3 entries. * dist_buf: An array of (2^bits)^3 long integers to be * used as scratch space. * Outputs: * rgbmap: The output inverse colormap. The entry * rgbmap[(r<<(2*bits)) + (g< bdist ) { *dp = bdist; *rgbp = i; } } } } #endif