blob: 56b5f384647272a1f1447e6e96c197ac98d4ec21 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman316a3242014-01-15 12:40:20 +01002 * Copyright 2014 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
Pierre Ossmanc0397262014-03-14 15:59:46 +010019
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000020#include <rfb/Encoder.h>
Pierre Ossmanc0397262014-03-14 15:59:46 +010021#include <rfb/PixelBuffer.h>
22#include <rfb/Palette.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000023
24using namespace rfb;
25
Pierre Ossmanc0397262014-03-14 15:59:46 +010026Encoder::Encoder(SConnection *conn_, int encoding_,
27 enum EncoderFlags flags_, unsigned int maxPaletteSize_) :
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010028 encoding(encoding_), flags(flags_),
29 maxPaletteSize(maxPaletteSize_), conn(conn_)
Pierre Ossman4aba19e2014-01-29 16:42:48 +010030{
31}
32
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033Encoder::~Encoder()
34{
35}
36
Pierre Ossmanc0397262014-03-14 15:59:46 +010037void Encoder::writeSolidRect(int width, int height,
38 const PixelFormat& pf, const rdr::U8* colour)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000039{
Pierre Ossmanc0397262014-03-14 15:59:46 +010040 ManagedPixelBuffer buffer(pf, width, height);
41 Pixel pixel;
42
43 Palette palette;
44 rdr::U32 palcol;
45
46 pixel = pf.pixelFromBuffer(colour);
47 buffer.fillRect(buffer.getRect(), pixel);
48
49 palcol = 0;
50 memcpy(&palcol, colour, pf.bpp/8);
51 palette.insert(palcol, 1);
52
53 writeRect(&buffer, palette);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054}
55
Pierre Ossmanc0397262014-03-14 15:59:46 +010056void Encoder::writeSolidRect(const PixelBuffer* pb, const Palette& palette)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057{
Pierre Ossmanc0397262014-03-14 15:59:46 +010058 rdr::U32 col32;
59 rdr::U16 col16;
60 rdr::U8 col8;
61
62 rdr::U8* buffer;
63
64 assert(palette.size() == 1);
65
66 // The Palette relies on implicit up and down conversion
67 switch (pb->getPF().bpp) {
68 case 32:
69 col32 = (rdr::U32)palette.getColour(0);
70 buffer = (rdr::U8*)&col32;
71 break;
72 case 16:
73 col16 = (rdr::U16)palette.getColour(0);
74 buffer = (rdr::U8*)&col16;
75 break;
Pierre Ossman316a3242014-01-15 12:40:20 +010076 default:
Pierre Ossmanc0397262014-03-14 15:59:46 +010077 col8 = (rdr::U8)palette.getColour(0);
78 buffer = (rdr::U8*)&col8;
79 break;
Pierre Ossman316a3242014-01-15 12:40:20 +010080 }
Pierre Ossmanc0397262014-03-14 15:59:46 +010081
82 writeSolidRect(pb->width(), pb->height(), pb->getPF(), buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083}