~ubuntu-branches/ubuntu/intrepid/gwenview/intrepid

« back to all changes in this revision

Viewing changes to src/gvjpegtran.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christopher Martin
  • Date: 2004-06-13 18:55:04 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040613185504-net8ekxoswwvyxs9
Tags: 1.1.3-1
New upstream release. Translations now included.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab
 
2
/*
 
3
Gwenview - A simple image viewer for KDE
 
4
Copyright 2000-2004 Aur�lien G�teau
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
*/
 
21
 
 
22
#include <string.h> // For memcpy
 
23
 
 
24
// Qt
 
25
#include <qcursor.h>
 
26
#if QT_VERSION>=0x030100
 
27
#include <qeventloop.h>
 
28
#endif
 
29
 
 
30
// KDE
 
31
#include <kapplication.h>
 
32
#include <kconfig.h>
 
33
#include <kdebug.h>
 
34
#include <klocale.h>
 
35
#include <kmessagebox.h>
 
36
#include <kprocess.h>
 
37
 
 
38
// Local
 
39
#include "gvjpegtran.moc"
 
40
 
 
41
//#define ENABLE_LOG
 
42
#ifdef ENABLE_LOG
 
43
#define LOG(x) kdDebug() << k_funcinfo << x << endl
 
44
#else
 
45
#define LOG(x) ;
 
46
#endif
 
47
 
 
48
const char* CONFIG_PROGRAM_PATH="path";
 
49
const unsigned int CHUNK_SIZE=1024;
 
50
 
 
51
static QString sProgramPath;
 
52
 
 
53
 
 
54
GVJPEGTran::GVJPEGTran()
 
55
{}
 
56
 
 
57
void GVJPEGTran::writeChunk(KProcess* process) {
 
58
        LOG("");
 
59
 
 
60
        if (mSent>=mSrc.size()) {
 
61
                LOG("all done");
 
62
                process->closeStdin();
 
63
                return;
 
64
        }
 
65
        unsigned int size=QMIN(mSrc.size()-mSent, CHUNK_SIZE);
 
66
        LOG("sending " << size << " bytes");
 
67
        process->writeStdin( mSrc.data() + mSent, size );
 
68
        mSent+=size;
 
69
}
 
70
 
 
71
void GVJPEGTran::slotReceivedStdout(KProcess*,char* data,int length) {
 
72
        LOG("size:" << length);
 
73
        uint oldSize=mDst.size();
 
74
        mDst.resize(oldSize+length);
 
75
        memcpy(mDst.data()+oldSize,data,length);
 
76
}
 
77
 
 
78
 
 
79
void GVJPEGTran::slotReceivedStderr(KProcess* process,char* data, int length) {
 
80
        LOG("size:" << length);
 
81
        kdWarning() << "Error: " << QCString(data,length) << endl;
 
82
        mDst.resize(0);
 
83
        process->kill();
 
84
}
 
85
 
 
86
 
 
87
void GVJPEGTran::slotProcessExited() {
 
88
        LOG("");
 
89
#if QT_VERSION<0x030100
 
90
        kapp->exit_loop();
 
91
#else
 
92
        kapp->eventLoop()->exitLoop();
 
93
#endif
 
94
}
 
95
 
 
96
QByteArray GVJPEGTran::apply(const QByteArray& src,GVImageUtils::Orientation orientation) {
 
97
        GVJPEGTran obj;
 
98
        KProcess process;
 
99
        process << sProgramPath << "-copy" << "all";
 
100
        
 
101
        switch (orientation) {
 
102
        case GVImageUtils::HFLIP:
 
103
                process << "-flip" << "horizontal";
 
104
                break;
 
105
        case GVImageUtils::ROT_180:
 
106
                process << "-rotate" << "180";
 
107
                break;
 
108
        case GVImageUtils::VFlip:
 
109
                process << "-flip" << "vertical";
 
110
                break;
 
111
        case GVImageUtils::ROT_90_HFLIP:
 
112
                process << "-transpose";
 
113
                break;
 
114
        case GVImageUtils::ROT_90:
 
115
                process << "-rotate" << "90";
 
116
                break;
 
117
        case GVImageUtils::ROT_90_VFLIP:
 
118
                process << "-transverse";
 
119
                break;
 
120
        case GVImageUtils::ROT_270:
 
121
                process << "-rotate" << "270";
 
122
                break;
 
123
        default:
 
124
                return src;
 
125
        }
 
126
        
 
127
        connect(&process,SIGNAL(wroteStdin(KProcess*)),
 
128
                &obj,SLOT(writeChunk(KProcess*)) );
 
129
        
 
130
        connect(&process,SIGNAL(receivedStdout(KProcess*,char*,int)),
 
131
                &obj,SLOT(slotReceivedStdout(KProcess*,char*,int)) );
 
132
        
 
133
        connect(&process,SIGNAL(receivedStderr(KProcess*,char*,int)),
 
134
                &obj,SLOT(slotReceivedStderr(KProcess*,char*,int)) );
 
135
        
 
136
        connect(&process,SIGNAL(processExited(KProcess*)),
 
137
                &obj,SLOT(slotProcessExited()) );
 
138
        
 
139
        // Return an empty QByteArray on failure. GVDocument will thus consider the
 
140
        // buffer as invalid and will fall back to lossy manipulations.
 
141
        if ( !process.start(KProcess::NotifyOnExit, KProcess::All) ) {
 
142
                KMessageBox::information(0L,
 
143
                                i18n("Gwenview couldn't perform lossless image manipulation.\n"
 
144
                                        "Make sure that the jpegtran program is installed and that "
 
145
                                        "its path in the configuration dialog is correct"
 
146
                                        ),QString::null,"jpegtran failed");
 
147
                return QByteArray();
 
148
        }
 
149
        
 
150
        obj.mSrc=src;
 
151
        obj.mSent=0;
 
152
        obj.writeChunk(&process);
 
153
        
 
154
        kapp->setOverrideCursor(QCursor(WaitCursor));
 
155
#if QT_VERSION<0x030100
 
156
        kapp->enter_loop();
 
157
#else
 
158
        kapp->eventLoop()->enterLoop();
 
159
#endif
 
160
        kapp->restoreOverrideCursor();
 
161
        
 
162
        return obj.mDst;
 
163
}
 
164
 
 
165
 
 
166
//---------------------------------------------------------------------------
 
167
//
 
168
// Config
 
169
//
 
170
//---------------------------------------------------------------------------
 
171
void GVJPEGTran::readConfig(KConfig* config, const QString& group) {
 
172
        config->setGroup(group);
 
173
        sProgramPath=config->readEntry(CONFIG_PROGRAM_PATH,"jpegtran");
 
174
}
 
175
 
 
176
 
 
177
void GVJPEGTran::writeConfig(KConfig* config, const QString& group) {
 
178
        config->setGroup(group);
 
179
        config->writeEntry(CONFIG_PROGRAM_PATH,sProgramPath);
 
180
}
 
181
 
 
182
 
 
183
void GVJPEGTran::setProgramPath(const QString& path) {
 
184
        sProgramPath=path;
 
185
}
 
186
 
 
187
 
 
188
QString GVJPEGTran::programPath() {
 
189
        return sProgramPath;
 
190
}
 
191