tableinit24.c 4.92 KB
Newer Older
dscho's avatar
dscho committed
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
/*
  24 bit
 */

/*
 *  OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
 *  Original Xvnc code Copyright (C) 1999 AT&T Laboratories Cambridge.  
 *  All Rights Reserved.
 *
 *  This is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This software is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this software; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 *  USA.
 */

static void
27
rfbInitOneRGBTable24 (uint8_t *table, int inMax, int outMax, int outShift,int swap);
dscho's avatar
dscho committed
28 29 30 31 32 33


static void
rfbInitColourMapSingleTable24(char **table, rfbPixelFormat *in,
                            rfbPixelFormat *out,rfbColourMap* colourMap)
{
34 35 36
    uint32_t i, r, g, b, outValue;
    uint8_t *t;
    uint8_t c;
dscho's avatar
dscho committed
37
    unsigned int nEntries = 1 << in->bitsPerPixel;
dscho's avatar
dscho committed
38 39 40 41
    int shift = colourMap->is16?16:8;

    if (*table) free(*table);
    *table = (char *)malloc(nEntries * 3 + 1);
42
    t = (uint8_t *)*table;
dscho's avatar
dscho committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

    for (i = 0; i < nEntries; i++) {
        r = g = b = 0;
	if(i < colourMap->count) {
	  if(colourMap->is16) {
	    r = colourMap->data.shorts[3*i+0];
	    g = colourMap->data.shorts[3*i+1];
	    b = colourMap->data.shorts[3*i+2];
	  } else {
	    r = colourMap->data.bytes[3*i+0];
	    g = colourMap->data.bytes[3*i+1];
	    b = colourMap->data.bytes[3*i+2];
	  }
	}
        outValue = ((((r * (1 + out->redMax)) >> shift) << out->redShift) |
                (((g * (1 + out->greenMax)) >> shift) << out->greenShift) |
                (((b * (1 + out->blueMax)) >> shift) << out->blueShift));
60
	*(uint32_t*)&t[3*i] = outValue;
dscho's avatar
dscho committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	if(!rfbEndianTest)
	  memmove(t+3*i,t+3*i+1,3);
        if (out->bigEndian != in->bigEndian) {
	  c = t[3*i]; t[3*i] = t[3*i+2]; t[3*i+2] = c;
        }
    }
}

/*
 * rfbInitTrueColourSingleTable sets up a single lookup table for truecolour
 * translation.
 */

static void
rfbInitTrueColourSingleTable24 (char **table, rfbPixelFormat *in,
                                 rfbPixelFormat *out)
{
    int i,outValue;
    int inRed, inGreen, inBlue, outRed, outGreen, outBlue;
80 81
    uint8_t *t;
    uint8_t c;
dscho's avatar
dscho committed
82 83 84
    int nEntries = 1 << in->bitsPerPixel;

    if (*table) free(*table);
85
    *table = (char *)malloc(nEntries * 3 + 1);
86
    t = (uint8_t *)*table;
dscho's avatar
dscho committed
87 88 89 90 91 92 93 94 95 96 97 98 99

    for (i = 0; i < nEntries; i++) {
        inRed   = (i >> in->redShift)   & in->redMax;
        inGreen = (i >> in->greenShift) & in->greenMax;
        inBlue  = (i >> in->blueShift)  & in->blueMax;

        outRed   = (inRed   * out->redMax   + in->redMax / 2)   / in->redMax;
        outGreen = (inGreen * out->greenMax + in->greenMax / 2) / in->greenMax;
        outBlue  = (inBlue  * out->blueMax  + in->blueMax / 2)  / in->blueMax;

	outValue = ((outRed   << out->redShift)   |
                (outGreen << out->greenShift) |
                (outBlue  << out->blueShift));
100
	*(uint32_t*)&t[3*i] = outValue;
dscho's avatar
dscho committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	if(!rfbEndianTest)
	  memmove(t+3*i,t+3*i+1,3);
        if (out->bigEndian != in->bigEndian) {
	  c = t[3*i]; t[3*i] = t[3*i+2]; t[3*i+2] = c;
        }
    }
}


/*
 * rfbInitTrueColourRGBTables sets up three separate lookup tables for the
 * red, green and blue values.
 */

static void
rfbInitTrueColourRGBTables24 (char **table, rfbPixelFormat *in,
                               rfbPixelFormat *out)
{
119 120 121
    uint8_t *redTable;
    uint8_t *greenTable;
    uint8_t *blueTable;
dscho's avatar
dscho committed
122 123

    if (*table) free(*table);
124
    *table = (char *)malloc((in->redMax + in->greenMax + in->blueMax + 3)
dscho's avatar
dscho committed
125
                            * 3 + 1);
126
    redTable = (uint8_t *)*table;
dscho's avatar
dscho committed
127 128 129 130 131 132 133 134 135 136 137 138
    greenTable = redTable + 3*(in->redMax + 1);
    blueTable = greenTable + 3*(in->greenMax + 1);

    rfbInitOneRGBTable24 (redTable, in->redMax, out->redMax,
                           out->redShift, (out->bigEndian != in->bigEndian));
    rfbInitOneRGBTable24 (greenTable, in->greenMax, out->greenMax,
                           out->greenShift, (out->bigEndian != in->bigEndian));
    rfbInitOneRGBTable24 (blueTable, in->blueMax, out->blueMax,
                           out->blueShift, (out->bigEndian != in->bigEndian));
}

static void
139
rfbInitOneRGBTable24 (uint8_t *table, int inMax, int outMax, int outShift,
dscho's avatar
dscho committed
140 141 142 143
                       int swap)
{
    int i;
    int nEntries = inMax + 1;
144 145
    uint32_t outValue;
    uint8_t c;
dscho's avatar
dscho committed
146 147 148

    for (i = 0; i < nEntries; i++) {
      outValue = ((i * outMax + inMax / 2) / inMax) << outShift;
149
      *(uint32_t *)&table[3*i] = outValue;
dscho's avatar
dscho committed
150 151 152 153 154 155 156 157
      if(!rfbEndianTest)
	memmove(table+3*i,table+3*i+1,3);
        if (swap) {
	  c = table[3*i]; table[3*i] = table[3*i+2];
	  table[3*i+2] = c;
        }
    }
}