Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 3 | * Copyright (C) 2011 D. R. Commander. All Rights Reserved. |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 4 | * |
| 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 18 | * USA. |
| 19 | */ |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <rfb/PixelFormat.h> |
| 24 | #include <rfb/Exception.h> |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 25 | #include <rfb/PixelBuffer.h> |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 26 | #include <rfb/PixelTransformer.h> |
| 27 | |
| 28 | using namespace rfb; |
| 29 | |
| 30 | static void noTransFn(void* table_, |
Pierre Ossman | 654e3f9 | 2012-01-30 13:53:11 +0000 | [diff] [blame] | 31 | const PixelFormat& inPF, const void* inPtr, int inStride, |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 32 | const PixelFormat& outPF, void* outPtr, int outStride, |
| 33 | int width, int height) |
| 34 | { |
| 35 | rdr::U8* ip = (rdr::U8*)inPtr; |
| 36 | rdr::U8* op = (rdr::U8*)outPtr; |
| 37 | int inStrideBytes = inStride * (inPF.bpp/8); |
| 38 | int outStrideBytes = outStride * (outPF.bpp/8); |
| 39 | int widthBytes = width * (outPF.bpp/8); |
| 40 | |
| 41 | while (height > 0) { |
| 42 | memcpy(op, ip, widthBytes); |
| 43 | ip += inStrideBytes; |
| 44 | op += outStrideBytes; |
| 45 | height--; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #define BPPOUT 8 |
| 50 | #include "transInitTempl.h" |
| 51 | #define BPPIN 8 |
| 52 | #include "transTempl.h" |
| 53 | #undef BPPIN |
| 54 | #define BPPIN 16 |
| 55 | #include "transTempl.h" |
| 56 | #undef BPPIN |
| 57 | #define BPPIN 32 |
| 58 | #include "transTempl.h" |
| 59 | #undef BPPIN |
| 60 | #undef BPPOUT |
| 61 | |
| 62 | #define BPPOUT 16 |
| 63 | #include "transInitTempl.h" |
| 64 | #define BPPIN 8 |
| 65 | #include "transTempl.h" |
| 66 | #undef BPPIN |
| 67 | #define BPPIN 16 |
| 68 | #include "transTempl.h" |
| 69 | #undef BPPIN |
| 70 | #define BPPIN 32 |
| 71 | #include "transTempl.h" |
| 72 | #undef BPPIN |
| 73 | #undef BPPOUT |
| 74 | |
| 75 | #define BPPOUT 32 |
| 76 | #include "transInitTempl.h" |
| 77 | #define BPPIN 8 |
| 78 | #include "transTempl.h" |
| 79 | #undef BPPIN |
| 80 | #define BPPIN 16 |
| 81 | #include "transTempl.h" |
| 82 | #undef BPPIN |
| 83 | #define BPPIN 32 |
| 84 | #include "transTempl.h" |
| 85 | #undef BPPIN |
| 86 | #undef BPPOUT |
| 87 | |
| 88 | |
| 89 | // Translation functions. Note that transSimple* is only used for 8/16bpp and |
| 90 | // transRGB* is used for 16/32bpp |
| 91 | |
| 92 | static transFnType transSimpleFns[][3] = { |
| 93 | { transSimple8to8, transSimple8to16, transSimple8to32 }, |
| 94 | { transSimple16to8, transSimple16to16, transSimple16to32 }, |
| 95 | }; |
| 96 | static transFnType transRGBFns[][3] = { |
| 97 | { transRGB16to8, transRGB16to16, transRGB16to32 }, |
| 98 | { transRGB32to8, transRGB32to16, transRGB32to32 } |
| 99 | }; |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 100 | |
| 101 | // Table initialisation functions. |
| 102 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 103 | typedef void (*initFnType)(rdr::U8** tablep, const PixelFormat& inPF, |
| 104 | const PixelFormat& outPF); |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 105 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 106 | static initFnType initSimpleFns[] = { |
| 107 | initSimple8, initSimple16, initSimple32 |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 110 | static initFnType initRGBFns[] = { |
| 111 | initRGB8, initRGB16, initRGB32 |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | |
| 115 | PixelTransformer::PixelTransformer(bool econ) |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 116 | : economic(econ), table(0), transFn(0) |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 117 | { |
| 118 | } |
| 119 | |
| 120 | PixelTransformer::~PixelTransformer() |
| 121 | { |
| 122 | delete [] table; |
| 123 | } |
| 124 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 125 | void PixelTransformer::init(const PixelFormat& inPF_, |
| 126 | const PixelFormat& outPF_) |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 127 | { |
| 128 | inPF = inPF_; |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 129 | outPF = outPF_; |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 130 | |
| 131 | if (table) |
| 132 | delete [] table; |
| 133 | table = NULL; |
| 134 | transFn = NULL; |
| 135 | |
| 136 | if ((inPF.bpp != 8) && (inPF.bpp != 16) && (inPF.bpp != 32)) |
| 137 | throw Exception("PixelTransformer: bpp in not 8, 16 or 32"); |
| 138 | |
| 139 | if ((outPF.bpp != 8) && (outPF.bpp != 16) && (outPF.bpp != 32)) |
| 140 | throw Exception("PixelTransformer: bpp out not 8, 16 or 32"); |
| 141 | |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 142 | if (inPF.equal(outPF)) { |
| 143 | transFn = noTransFn; |
| 144 | return; |
| 145 | } |
| 146 | |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 147 | if ((inPF.bpp > 16) || (economic && (inPF.bpp == 16))) { |
| 148 | transFn = transRGBFns[inPF.bpp/32][outPF.bpp/16]; |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 149 | (*initRGBFns[outPF.bpp/16]) (&table, inPF, outPF); |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 150 | } else { |
| 151 | transFn = transSimpleFns[inPF.bpp/16][outPF.bpp/16]; |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 152 | (*initSimpleFns[outPF.bpp/16]) (&table, inPF, outPF); |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Pierre Ossman | 62ea8a2 | 2011-07-15 08:26:18 +0000 | [diff] [blame] | 156 | const PixelFormat &PixelTransformer::getInPF() const |
| 157 | { |
| 158 | return inPF; |
| 159 | } |
| 160 | |
Pierre Ossman | 62ea8a2 | 2011-07-15 08:26:18 +0000 | [diff] [blame] | 161 | const PixelFormat &PixelTransformer::getOutPF() const |
| 162 | { |
| 163 | return outPF; |
| 164 | } |
| 165 | |
Pierre Ossman | 654e3f9 | 2012-01-30 13:53:11 +0000 | [diff] [blame] | 166 | void PixelTransformer::translatePixels(const void* inPtr, void* outPtr, |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 167 | int nPixels) const |
| 168 | { |
| 169 | if (!transFn) |
| 170 | throw Exception("PixelTransformer: not initialised yet"); |
| 171 | |
| 172 | (*transFn)(table, inPF, inPtr, nPixels, |
| 173 | outPF, outPtr, nPixels, nPixels, 1); |
| 174 | } |
| 175 | |
Pierre Ossman | 654e3f9 | 2012-01-30 13:53:11 +0000 | [diff] [blame] | 176 | void PixelTransformer::translateRect(const void* inPtr, int inStride, |
Pierre Ossman | a273934 | 2011-03-08 16:53:07 +0000 | [diff] [blame] | 177 | Rect inRect, |
| 178 | void* outPtr, int outStride, |
| 179 | Point outCoord) const |
| 180 | { |
| 181 | char *in, *out; |
| 182 | |
| 183 | if (!transFn) |
| 184 | throw Exception("PixelTransformer: not initialised yet"); |
| 185 | |
| 186 | in = (char*)inPtr; |
| 187 | in += inPF.bpp/8 * inRect.tl.x; |
| 188 | in += (inStride * inPF.bpp/8) * inRect.tl.y; |
| 189 | |
| 190 | out = (char*)outPtr; |
| 191 | out += outPF.bpp/8 * outCoord.x; |
| 192 | out += (outStride * outPF.bpp/8) * outCoord.y; |
| 193 | |
| 194 | (*transFn)(table, inPF, in, inStride, |
| 195 | outPF, out, outStride, |
| 196 | inRect.width(), inRect.height()); |
| 197 | } |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 198 | |
| 199 | bool PixelTransformer::willTransform(void) |
| 200 | { |
| 201 | return transFn != NULL && transFn != noTransFn; |
| 202 | } |