Peter Åstrand | 462753d | 2004-11-16 15:23:25 +0000 | [diff] [blame] | 1 | /* 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 Åstrand | a6bb770 | 2004-12-07 08:22:42 +0000 | [diff] [blame] | 21 | #include <stdio.h> /* jpeglib.h needs FILE */ |
Peter Åstrand | b4a2316 | 2004-12-07 13:52:13 +0000 | [diff] [blame^] | 22 | extern "C" { |
Peter Åstrand | a6bb770 | 2004-12-07 08:22:42 +0000 | [diff] [blame] | 23 | #include <jpeglib.h> |
Peter Åstrand | b4a2316 | 2004-12-07 13:52:13 +0000 | [diff] [blame^] | 24 | } |
Peter Åstrand | 462753d | 2004-11-16 15:23:25 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace rfb; |
| 27 | |
Peter Åstrand | a6bb770 | 2004-12-07 08:22:42 +0000 | [diff] [blame] | 28 | #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 | |
| 38 | static void JpegSetSrcManager(j_decompress_ptr cinfo, char *compressedData, |
| 39 | int compressedLen); |
| 40 | static bool jpegError; |
| 41 | |
Peter Åstrand | 462753d | 2004-11-16 15:23:25 +0000 | [diff] [blame] | 42 | #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 | |
| 55 | Decoder* TightDecoder::create(CMsgReader* reader) |
| 56 | { |
| 57 | return new TightDecoder(reader); |
| 58 | } |
| 59 | |
| 60 | TightDecoder::TightDecoder(CMsgReader* reader_) : reader(reader_) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | TightDecoder::~TightDecoder() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void TightDecoder::readRect(const Rect& r, CMsgHandler* handler) |
| 69 | { |
| 70 | rdr::InStream* is = reader->getInStream(); |
Peter Åstrand | a6bb770 | 2004-12-07 08:22:42 +0000 | [diff] [blame] | 71 | /* 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 Åstrand | 462753d | 2004-11-16 15:23:25 +0000 | [diff] [blame] | 74 | 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 Åstrand | a6bb770 | 2004-12-07 08:22:42 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | // |
| 86 | // A "Source manager" for the JPEG library. |
| 87 | // |
| 88 | |
| 89 | static struct jpeg_source_mgr jpegSrcManager; |
| 90 | static JOCTET *jpegBufferPtr; |
| 91 | static size_t jpegBufferLen; |
| 92 | |
| 93 | static void JpegInitSource(j_decompress_ptr cinfo); |
| 94 | static boolean JpegFillInputBuffer(j_decompress_ptr cinfo); |
| 95 | static void JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes); |
| 96 | static void JpegTermSource(j_decompress_ptr cinfo); |
| 97 | |
| 98 | static void |
| 99 | JpegInitSource(j_decompress_ptr cinfo) |
| 100 | { |
| 101 | jpegError = false; |
| 102 | } |
| 103 | |
| 104 | static boolean |
| 105 | JpegFillInputBuffer(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 | |
| 114 | static void |
| 115 | JpegSkipInputData(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 | |
| 127 | static void |
| 128 | JpegTermSource(j_decompress_ptr cinfo) |
| 129 | { |
| 130 | /* No work necessary here. */ |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | JpegSetSrcManager(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 | } |