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