blob: aa2c67564d822b08966fa7e967b027db40342ca6 [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 */
22#include <jpeglib.h>
Peter Åstrand462753d2004-11-16 15:23:25 +000023
24using namespace rfb;
25
Peter Åstranda6bb7702004-12-07 08:22:42 +000026#define RGB24_TO_PIXEL(bpp,r,g,b) \
27 ((((PIXEL_T)(r) & 0xFF) * myFormat.redMax + 127) / 255 \
28 << myFormat.redShift | \
29 (((PIXEL_T)(g) & 0xFF) * myFormat.greenMax + 127) / 255 \
30 << myFormat.greenShift | \
31 (((PIXEL_T)(b) & 0xFF) * myFormat.blueMax + 127) / 255 \
32 << myFormat.blueShift)
33
34#define TIGHT_MAX_WIDTH 2048
35
36static void JpegSetSrcManager(j_decompress_ptr cinfo, char *compressedData,
37 int compressedLen);
38static bool jpegError;
39
Peter Åstrand462753d2004-11-16 15:23:25 +000040#define EXTRA_ARGS CMsgHandler* handler
41#define FILL_RECT(r, p) handler->fillRect(r, p)
42#define IMAGE_RECT(r, p) handler->imageRect(r, p)
43#define BPP 8
44#include <rfb/tightDecode.h>
45#undef BPP
46#define BPP 16
47#include <rfb/tightDecode.h>
48#undef BPP
49#define BPP 32
50#include <rfb/tightDecode.h>
51#undef BPP
52
53Decoder* TightDecoder::create(CMsgReader* reader)
54{
55 return new TightDecoder(reader);
56}
57
58TightDecoder::TightDecoder(CMsgReader* reader_) : reader(reader_)
59{
60}
61
62TightDecoder::~TightDecoder()
63{
64}
65
66void TightDecoder::readRect(const Rect& r, CMsgHandler* handler)
67{
68 rdr::InStream* is = reader->getInStream();
Peter Åstranda6bb7702004-12-07 08:22:42 +000069 /* Uncompressed RGB24 JPEG data, before translated, can be up to 3
70 times larger, if VNC bpp is 8. */
71 rdr::U8* buf = reader->getImageBuf(r.area()*3);
Peter Åstrand462753d2004-11-16 15:23:25 +000072 switch (reader->bpp()) {
73 case 8:
74 tightDecode8 (r, is, zis, (rdr::U8*) buf, handler); break;
75 case 16:
76 tightDecode16(r, is, zis, (rdr::U16*)buf, handler); break;
77 case 32:
78 tightDecode32(r, is, zis, (rdr::U32*)buf, handler); break;
79 }
80}
Peter Åstranda6bb7702004-12-07 08:22:42 +000081
82
83//
84// A "Source manager" for the JPEG library.
85//
86
87static struct jpeg_source_mgr jpegSrcManager;
88static JOCTET *jpegBufferPtr;
89static size_t jpegBufferLen;
90
91static void JpegInitSource(j_decompress_ptr cinfo);
92static boolean JpegFillInputBuffer(j_decompress_ptr cinfo);
93static void JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes);
94static void JpegTermSource(j_decompress_ptr cinfo);
95
96static void
97JpegInitSource(j_decompress_ptr cinfo)
98{
99 jpegError = false;
100}
101
102static boolean
103JpegFillInputBuffer(j_decompress_ptr cinfo)
104{
105 jpegError = true;
106 jpegSrcManager.bytes_in_buffer = jpegBufferLen;
107 jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr;
108
109 return TRUE;
110}
111
112static void
113JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes)
114{
115 if (num_bytes < 0 || (size_t)num_bytes > jpegSrcManager.bytes_in_buffer) {
116 jpegError = true;
117 jpegSrcManager.bytes_in_buffer = jpegBufferLen;
118 jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr;
119 } else {
120 jpegSrcManager.next_input_byte += (size_t) num_bytes;
121 jpegSrcManager.bytes_in_buffer -= (size_t) num_bytes;
122 }
123}
124
125static void
126JpegTermSource(j_decompress_ptr cinfo)
127{
128 /* No work necessary here. */
129}
130
131static void
132JpegSetSrcManager(j_decompress_ptr cinfo, char *compressedData, int compressedLen)
133{
134 jpegBufferPtr = (JOCTET *)compressedData;
135 jpegBufferLen = (size_t)compressedLen;
136
137 jpegSrcManager.init_source = JpegInitSource;
138 jpegSrcManager.fill_input_buffer = JpegFillInputBuffer;
139 jpegSrcManager.skip_input_data = JpegSkipInputData;
140 jpegSrcManager.resync_to_restart = jpeg_resync_to_restart;
141 jpegSrcManager.term_source = JpegTermSource;
142 jpegSrcManager.next_input_byte = jpegBufferPtr;
143 jpegSrcManager.bytes_in_buffer = jpegBufferLen;
144
145 cinfo->src = &jpegSrcManager;
146}