summaryrefslogtreecommitdiffstats
path: root/kviewshell/renderedDocumentPagePixmap.cpp
blob: aaa068789cbf2625fd24af1892a1637472edf033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

/* This file is part of the KDE project
   Copyright (C) 2005 Wilfried Huss <Wilfried.Huss@gmx.at>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <config.h>

#include <kimageeffect.h>
#include <kdebug.h>
#include <qimage.h>

#include "kvsprefs.h"
#include "renderedDocumentPagePixmap.h"

RenderedDocumentPagePixmap::RenderedDocumentPagePixmap()
  : _accessiblePixmap(0), dirty(true)
{
}

RenderedDocumentPagePixmap::~RenderedDocumentPagePixmap()
{
  delete _accessiblePixmap;
}


QPainter* RenderedDocumentPagePixmap::getPainter()
{
  if (paintingActive()) {
    kdError(1223) << "RenderedDocumentPagePixmap::getPainter() called when painting was active" << endl;
    return 0;
  } else
    return new QPainter(this);
}


void RenderedDocumentPagePixmap::resize(int width, int height)
{
  QPixmap::resize(width, height);
  if(_accessiblePixmap)
    _accessiblePixmap->resize(width, height);

  dirty = true;
}

void RenderedDocumentPagePixmap::resize(const QSize& size)
{
  resize(size.width(), size.height());
}

QPixmap RenderedDocumentPagePixmap::accessiblePixmap()
{
  if (!_accessiblePixmap || dirty)
  {
    QImage backImage = this->convertToImage();
    switch (KVSPrefs::renderMode())
    {
      case KVSPrefs::EnumRenderMode::Inverted:
        // Invert image pixels using QImage internal function
        backImage.invertPixels(false);
        break;
      case KVSPrefs::EnumRenderMode::Recolor:
        // Recolor image using KImageEffect::flatten with dither:0
        KImageEffect::flatten(backImage, KVSPrefs::recolorForeground(), KVSPrefs::recolorBackground());
        break;
      case KVSPrefs::EnumRenderMode::BlackWhite:
        // Manual Gray and Contrast
        unsigned int* data = (unsigned int*)backImage.bits();
        int val;
        int pixels = backImage.width() * backImage.height();
        int con = KVSPrefs::bWContrast();
        int thr = 255 - KVSPrefs::bWThreshold();

        for( int i = 0; i < pixels; ++i )
        {
          val = qGray(data[i]);
          if (val > thr)
            val = 128 + (127 * (val - thr)) / (255 - thr);
          else if ( val < thr )
            val = (128 * val) / thr;
          if (con > 2)
          {
            val = con * (val - thr) / 2 + thr;
            if (val > 255)
              val = 255;
            else if (val < 0)
              val = 0;
          }
          data[i] = qRgba(val, val, val, 255);
        }
        break;
    }
    if (!_accessiblePixmap)
      _accessiblePixmap = new QPixmap(width(), height());

    _accessiblePixmap->convertFromImage(backImage);
    dirty = false;
  }

  return *_accessiblePixmap;
}

unsigned int RenderedDocumentPagePixmap::memory()
{
  return size().height() * size().width() * depth() / 8;
}

#include "renderedDocumentPagePixmap.moc"