blob: 18b66809b029ab3d296afa931fb84c6a4e34812a [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);
Pierre Ossmanc0397262014-03-14 15:59:46 +010041
42 Palette palette;
43 rdr::U32 palcol;
44
Pierre Ossman56f99d62015-06-05 12:57:02 +020045 buffer.fillRect(buffer.getRect(), colour);
Pierre Ossmanc0397262014-03-14 15:59:46 +010046
47 palcol = 0;
48 memcpy(&palcol, colour, pf.bpp/8);
49 palette.insert(palcol, 1);
50
51 writeRect(&buffer, palette);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000052}
53
Pierre Ossmanc0397262014-03-14 15:59:46 +010054void Encoder::writeSolidRect(const PixelBuffer* pb, const Palette& palette)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000055{
Pierre Ossmanc0397262014-03-14 15:59:46 +010056 rdr::U32 col32;
57 rdr::U16 col16;
58 rdr::U8 col8;
59
60 rdr::U8* buffer;
61
62 assert(palette.size() == 1);
63
64 // The Palette relies on implicit up and down conversion
65 switch (pb->getPF().bpp) {
66 case 32:
67 col32 = (rdr::U32)palette.getColour(0);
68 buffer = (rdr::U8*)&col32;
69 break;
70 case 16:
71 col16 = (rdr::U16)palette.getColour(0);
72 buffer = (rdr::U8*)&col16;
73 break;
Pierre Ossman316a3242014-01-15 12:40:20 +010074 default:
Pierre Ossmanc0397262014-03-14 15:59:46 +010075 col8 = (rdr::U8)palette.getColour(0);
76 buffer = (rdr::U8*)&col8;
77 break;
Pierre Ossman316a3242014-01-15 12:40:20 +010078 }
Pierre Ossmanc0397262014-03-14 15:59:46 +010079
80 writeSolidRect(pb->width(), pb->height(), pb->getPF(), buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081}