Constantin Kaplinsky | 0b26323 | 2007-08-02 13:51:09 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2007 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 | |
| 19 | #include <rfb/JpegEncoder.h> |
| 20 | #include <rdr/OutStream.h> |
| 21 | #include <rfb/encodings.h> |
| 22 | |
| 23 | using namespace rfb; |
| 24 | |
| 25 | const int JpegEncoder::qualityMap[10] = { |
| 26 | 5, 10, 15, 25, 37, 50, 60, 70, 75, 80 |
| 27 | }; |
| 28 | |
| 29 | JpegEncoder::JpegEncoder(SMsgWriter* writer_) : writer(writer_) |
| 30 | { |
| 31 | jcomp = new StandardJpegCompressor; |
| 32 | jcomp->setQuality(qualityMap[6]); |
| 33 | } |
| 34 | |
| 35 | JpegEncoder::~JpegEncoder() |
| 36 | { |
| 37 | delete jcomp; |
| 38 | } |
| 39 | |
| 40 | void JpegEncoder::setQualityLevel(int level) |
| 41 | { |
| 42 | if (level >= 0 && level <= 9) { |
| 43 | jcomp->setQuality(qualityMap[level]); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | bool JpegEncoder::writeRect(PixelBuffer* pb, const Rect& r) |
| 48 | { |
| 49 | int serverBitsPerPixel = pb->getPF().bpp; |
| 50 | int clientBitsPerPixel = writer->bpp(); |
| 51 | |
| 52 | // FIXME: Implement JPEG compression for (serverBitsPerPixel == 16). |
| 53 | // FIXME: Check that all color components are actually 8 bits wide. |
| 54 | if (serverBitsPerPixel != 32 || clientBitsPerPixel < 16) { |
| 55 | // FIXME: Make sure this return value is checked properly. |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | writer->startRect(r, encodingTight); |
| 60 | rdr::OutStream* os = writer->getOutStream(); |
| 61 | |
| 62 | // Get access to pixel data |
| 63 | int stride; |
| 64 | const rdr::U32* pixels = (const rdr::U32 *)pb->getPixelsR(r, &stride); |
| 65 | const PixelFormat& fmt = pb->getPF(); |
| 66 | |
| 67 | // Encode data |
| 68 | jcomp->compress(pixels, &fmt, r.width(), r.height(), stride); |
| 69 | |
| 70 | // Write Tight-encoded header and JPEG data. |
| 71 | os->writeU8(0x09 << 4); |
| 72 | os->writeCompactLength(jcomp->getDataLength()); |
| 73 | os->writeBytes(jcomp->getDataPtr(), jcomp->getDataLength()); |
| 74 | |
| 75 | writer->endRect(); |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |