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