blob: a08282022db61b9fa5649337c58c10a6e12ef2ce [file] [log] [blame]
Peter Åstrand462753d2004-11-16 15:23:25 +00001/* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18#include <rfb/CMsgReader.h>
19#include <rfb/CMsgHandler.h>
20#include <rfb/TightDecoder.h>
Peter Åstranda6bb7702004-12-07 08:22:42 +000021#include <stdio.h> /* jpeglib.h needs FILE */
Peter Åstrandb4a23162004-12-07 13:52:13 +000022extern "C" {
Peter Åstranda6bb7702004-12-07 08:22:42 +000023#include <jpeglib.h>
Peter Åstrandb4a23162004-12-07 13:52:13 +000024}
Peter Åstrand462753d2004-11-16 15:23:25 +000025
26using namespace rfb;
27
Peter Åstranda6bb7702004-12-07 08:22:42 +000028#define RGB24_TO_PIXEL(bpp,r,g,b) \
29 ((((PIXEL_T)(r) & 0xFF) * myFormat.redMax + 127) / 255 \
30 << myFormat.redShift | \
31 (((PIXEL_T)(g) & 0xFF) * myFormat.greenMax + 127) / 255 \
32 << myFormat.greenShift | \
33 (((PIXEL_T)(b) & 0xFF) * myFormat.blueMax + 127) / 255 \
34 << myFormat.blueShift)
35
36#define TIGHT_MAX_WIDTH 2048
37
38static void JpegSetSrcManager(j_decompress_ptr cinfo, char *compressedData,
39 int compressedLen);
40static bool jpegError;
41
Peter Åstrand462753d2004-11-16 15:23:25 +000042#define EXTRA_ARGS CMsgHandler* handler
43#define FILL_RECT(r, p) handler->fillRect(r, p)
44#define IMAGE_RECT(r, p) handler->imageRect(r, p)
45#define BPP 8
46#include <rfb/tightDecode.h>
47#undef BPP
48#define BPP 16
49#include <rfb/tightDecode.h>
50#undef BPP
51#define BPP 32
52#include <rfb/tightDecode.h>
53#undef BPP
54
55Decoder* TightDecoder::create(CMsgReader* reader)
56{
57 return new TightDecoder(reader);
58}
59
60TightDecoder::TightDecoder(CMsgReader* reader_) : reader(reader_)
61{
62}
63
64TightDecoder::~TightDecoder()
65{
66}
67
68void TightDecoder::readRect(const Rect& r, CMsgHandler* handler)
69{
70 rdr::InStream* is = reader->getInStream();
Peter Åstranda6bb7702004-12-07 08:22:42 +000071 /* Uncompressed RGB24 JPEG data, before translated, can be up to 3
72 times larger, if VNC bpp is 8. */
73 rdr::U8* buf = reader->getImageBuf(r.area()*3);
Peter Åstrand462753d2004-11-16 15:23:25 +000074 switch (reader->bpp()) {
75 case 8:
76 tightDecode8 (r, is, zis, (rdr::U8*) buf, handler); break;
77 case 16:
78 tightDecode16(r, is, zis, (rdr::U16*)buf, handler); break;
79 case 32:
80 tightDecode32(r, is, zis, (rdr::U32*)buf, handler); break;
81 }
82}
Peter Åstranda6bb7702004-12-07 08:22:42 +000083
84
85//
86// A "Source manager" for the JPEG library.
87//
88
89static struct jpeg_source_mgr jpegSrcManager;
90static JOCTET *jpegBufferPtr;
91static size_t jpegBufferLen;
92
93static void JpegInitSource(j_decompress_ptr cinfo);
94static boolean JpegFillInputBuffer(j_decompress_ptr cinfo);
95static void JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes);
96static void JpegTermSource(j_decompress_ptr cinfo);
97
98static void
99JpegInitSource(j_decompress_ptr cinfo)
100{
101 jpegError = false;
102}
103
104static boolean
105JpegFillInputBuffer(j_decompress_ptr cinfo)
106{
107 jpegError = true;
108 jpegSrcManager.bytes_in_buffer = jpegBufferLen;
109 jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr;
110
111 return TRUE;
112}
113
114static void
115JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes)
116{
117 if (num_bytes < 0 || (size_t)num_bytes > jpegSrcManager.bytes_in_buffer) {
118 jpegError = true;
119 jpegSrcManager.bytes_in_buffer = jpegBufferLen;
120 jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr;
121 } else {
122 jpegSrcManager.next_input_byte += (size_t) num_bytes;
123 jpegSrcManager.bytes_in_buffer -= (size_t) num_bytes;
124 }
125}
126
127static void
128JpegTermSource(j_decompress_ptr cinfo)
129{
130 /* No work necessary here. */
131}
132
133static void
134JpegSetSrcManager(j_decompress_ptr cinfo, char *compressedData, int compressedLen)
135{
136 jpegBufferPtr = (JOCTET *)compressedData;
137 jpegBufferLen = (size_t)compressedLen;
138
139 jpegSrcManager.init_source = JpegInitSource;
140 jpegSrcManager.fill_input_buffer = JpegFillInputBuffer;
141 jpegSrcManager.skip_input_data = JpegSkipInputData;
142 jpegSrcManager.resync_to_restart = jpeg_resync_to_restart;
143 jpegSrcManager.term_source = JpegTermSource;
144 jpegSrcManager.next_input_byte = jpegBufferPtr;
145 jpegSrcManager.bytes_in_buffer = jpegBufferLen;
146
147 cinfo->src = &jpegSrcManager;
148}