~ubuntu-branches/ubuntu/karmic/kst/karmic

« back to all changes in this revision

Viewing changes to kst/kst/datasources/qimagesource/qimagesource.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2006-06-30 19:11:30 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060630191130-acumuar75bz4puty
Tags: 1.2.1-1ubuntu1
Merge from debian unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                 qimagesource.cpp  -  data source for qimagesources
 
3
                             -------------------
 
4
    begin                : Tue Oct 21 2003
 
5
    copyright            : (C) 2003 The University of Toronto
 
6
    email                :
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
#include "qimagesource.h"
 
19
#include <qcolor.h>
 
20
 
 
21
 
 
22
QimagesourceSource::QimagesourceSource(KConfig *cfg, const QString& filename, const QString& type)
 
23
: KstDataSource(cfg, filename, type) {
 
24
  _image.reset();
 
25
  if (init()) {
 
26
    _valid = true;
 
27
  }
 
28
}
 
29
 
 
30
 
 
31
QimagesourceSource::~QimagesourceSource() {
 
32
}
 
33
 
 
34
 
 
35
bool QimagesourceSource::reset() {
 
36
  init();
 
37
  return true;
 
38
}
 
39
 
 
40
 
 
41
bool QimagesourceSource::init() {
 
42
 
 
43
  _image.reset();
 
44
  _matrixList.clear();
 
45
  _fieldList.clear();
 
46
  _frameCount = 0;
 
47
  if ( _image.load( _filename ) ) {
 
48
    _fieldList.append("INDEX");
 
49
    _fieldList.append( "GRAY" );
 
50
    _fieldList.append( "RED" );
 
51
    _fieldList.append( "GREEN" );
 
52
    _fieldList.append( "BLUE" );
 
53
    _fieldList.append( "1" );
 
54
    _fieldList.append( "2" );
 
55
    _fieldList.append( "3" );
 
56
    _fieldList.append( "4" );
 
57
    _matrixList.append( "GRAY" );
 
58
    _matrixList.append( "RED" );
 
59
    _matrixList.append( "GREEN" );
 
60
    _matrixList.append( "BLUE" );
 
61
    _matrixList.append( "1" );
 
62
    _matrixList.append( "2" );
 
63
    _matrixList.append( "3" );
 
64
    _matrixList.append( "4" );
 
65
    return update() == KstObject::UPDATE;
 
66
  } else {
 
67
    _image.reset();
 
68
    return false;
 
69
  }
 
70
}
 
71
 
 
72
KstObject::UpdateType QimagesourceSource::update(int u) {
 
73
  if (KstObject::checkUpdateCounter(u)) {
 
74
    return lastUpdateResult();
 
75
  }
 
76
  int newNF = _image.width()*_image.height();
 
77
  bool isnew = newNF != _frameCount;
 
78
 
 
79
  _frameCount = newNF;
 
80
 
 
81
  updateNumFramesScalar();
 
82
  return setLastUpdateResult(isnew ? KstObject::UPDATE : KstObject::NO_CHANGE);
 
83
}
 
84
 
 
85
bool QimagesourceSource::matrixDimensions( const QString& matrix,
 
86
                                           int* xDim, int* yDim) {
 
87
  if ( _image.isNull() ) {
 
88
    return false;
 
89
  }
 
90
 
 
91
  if ( !_matrixList.contains( matrix ) ) {
 
92
    return false;
 
93
  }
 
94
 
 
95
  *xDim = _image.width();
 
96
  *yDim = _image.height();
 
97
  return true;
 
98
}
 
99
 
 
100
int QimagesourceSource::readMatrix(KstMatrixData* data,
 
101
                                     const QString& field, int xStart,
 
102
                                     int yStart, int xNumSteps,
 
103
                                     int yNumSteps) {
 
104
  int i,  px, py;
 
105
  int y0, y1, x0, x1;
 
106
  double *z;
 
107
 
 
108
  if ( _image.isNull() ) {
 
109
    return 0;
 
110
  }
 
111
 
 
112
  y0 = yStart;
 
113
  y1 = yStart+yNumSteps;
 
114
 
 
115
  x0 = xStart;
 
116
  x1 = xStart+xNumSteps;
 
117
 
 
118
  i=0;
 
119
 
 
120
  z = data->z;
 
121
  if ( field=="GRAY" || field == "1") {
 
122
    for ( px = xStart; px<x1; px++ ) {
 
123
      for ( py=y1-1; py>=yStart; py-- ) {
 
124
        z[i] = qGray( _image.pixel( px, py ) );
 
125
        i++;
 
126
      }
 
127
    }
 
128
  } else if ( field=="RED" || field == "2" ) {
 
129
    for ( px = xStart; px<x1; px++ ) {
 
130
      for ( py=y1-1; py>=yStart; py-- ) {
 
131
        z[i] = qRed( _image.pixel( px, py ) );
 
132
        i++;
 
133
      }
 
134
    }
 
135
  } else if ( field=="GREEN" || field == "3" ) {
 
136
    for ( px = xStart; px<x1; px++ ) {
 
137
      for ( py=y1-1; py>=yStart; py-- ) {
 
138
        z[i] = qGreen( _image.pixel( px, py ) );
 
139
        i++;
 
140
      }
 
141
    }
 
142
  } else if ( field=="BLUE" || field == "4" ) {
 
143
    for ( px = xStart; px<x1; px++ ) {
 
144
      for ( py=y1-1; py>=yStart; py-- ) {
 
145
        z[i] = qBlue( _image.pixel( px, py ) );
 
146
        i++;
 
147
      }
 
148
    }
 
149
  }
 
150
 
 
151
    // set the suggested matrix transform params: pixel index....
 
152
  data->xMin = x0;
 
153
  data->yMin = y0;
 
154
  data->xStepSize = 1;
 
155
  data->yStepSize = 1;
 
156
 
 
157
  return( i );
 
158
}
 
159
 
 
160
int QimagesourceSource::readField(double *v, const QString& field, int s, int n) {
 
161
  int i=0, px,  py;
 
162
 
 
163
  if ( field=="INDEX" ) {
 
164
    for ( i=0; i<n; i++ ) {
 
165
      v[i] = i+s;
 
166
    }
 
167
  } else if ( field=="GRAY" || field == "1" ) {
 
168
    for ( i=s; i<s+n; i++ ) {
 
169
      px = i%_image.width();
 
170
      py = i/_image.width();
 
171
      v[i-s] = qGray( _image.pixel( px, py ) );
 
172
    }
 
173
  } else if ( field=="RED" || field == "2" ) {
 
174
    for ( i=s; i<s+n; i++ ) {
 
175
      px = i%_image.width();
 
176
      py = i/_image.width();
 
177
      v[i-s] = qRed( _image.pixel( px, py ) );
 
178
    }
 
179
  } else if ( field=="GREEN" || field == "3" ) {
 
180
    for ( i=s; i<s+n; i++ ) {
 
181
      px = i%_image.width();
 
182
      py = i/_image.width();
 
183
      v[i-s] = qGreen( _image.pixel( px, py ) );
 
184
    }
 
185
  } else if ( field=="BLUE" || field == "4" ) {
 
186
    for ( i=s; i<s+n; i++ ) {
 
187
      px = i%_image.width();
 
188
      py = i/_image.width();
 
189
      v[i-s] = qBlue( _image.pixel( px, py ) );
 
190
    }
 
191
  }
 
192
 
 
193
  return( i );
 
194
}
 
195
 
 
196
 
 
197
bool QimagesourceSource::isValidField(const QString& field) const {
 
198
  return  _fieldList.contains( field );
 
199
}
 
200
 
 
201
bool QimagesourceSource::isValidMatrix(const QString& field) const {
 
202
  return  _matrixList.contains( field );
 
203
}
 
204
 
 
205
int QimagesourceSource::samplesPerFrame(const QString &field) {
 
206
  Q_UNUSED(field)
 
207
  return 1;
 
208
}
 
209
 
 
210
 
 
211
int QimagesourceSource::frameCount(const QString& field) const {
 
212
  Q_UNUSED(field)
 
213
  return _frameCount;
 
214
}
 
215
 
 
216
 
 
217
bool QimagesourceSource::isEmpty() const {
 
218
  return _frameCount < 1;
 
219
}
 
220
 
 
221
 
 
222
QString QimagesourceSource::fileType() const {
 
223
  return "QImage compatible Image";
 
224
}
 
225
 
 
226
 
 
227
void QimagesourceSource::save(QTextStream &ts, const QString& indent) {
 
228
  KstDataSource::save(ts, indent);
 
229
}
 
230
 
 
231
//#include <kdebug.h>
 
232
 
 
233
extern "C" {
 
234
KstDataSource *create_qimagesource(KConfig *cfg, const QString& filename, const QString& type) {
 
235
  return new QimagesourceSource(cfg, filename, type);
 
236
}
 
237
 
 
238
QStringList provides_qimagesource() {
 
239
  QStringList rc;
 
240
  rc += "QImage compatible Image";
 
241
  return rc;
 
242
}
 
243
 
 
244
int understands_qimagesource(KConfig*, const QString& filename) {
 
245
  QString ftype( QImage::imageFormat( filename ) );
 
246
 
 
247
  if ( ftype.isEmpty() ) return 0;
 
248
 
 
249
  if ( ftype == "TIFF" ) {
 
250
    if ( ftype.find( ".tif",  -5, false )<0 ) return 0;
 
251
  }
 
252
 
 
253
  return 90;
 
254
 
 
255
}
 
256
 
 
257
 
 
258
QStringList fieldList_qimagesource(KConfig*, const QString& filename, const QString& type, QString *typeSuggestion, bool *complete) {
 
259
  Q_UNUSED(type)
 
260
  QStringList fieldList;
 
261
 
 
262
  if (complete) {
 
263
    *complete = true;
 
264
  }
 
265
 
 
266
  if (typeSuggestion) {
 
267
    *typeSuggestion = "QImage compatible Image";
 
268
  }
 
269
  if ( QImage::imageFormat( filename ) ) {
 
270
    fieldList.append("INDEX");
 
271
    fieldList.append( "GRAY" );
 
272
    fieldList.append( "RED" );
 
273
    fieldList.append( "GREEN" );
 
274
    fieldList.append( "BLUE" );
 
275
    fieldList.append( "1" );
 
276
    fieldList.append( "2" );
 
277
    fieldList.append( "3" );
 
278
    fieldList.append( "4" );
 
279
  }
 
280
  return fieldList;
 
281
}
 
282
 
 
283
}
 
284
 
 
285
KST_KEY_DATASOURCE_PLUGIN(qimagesource)
 
286
 
 
287
// vim: ts=2 sw=2 et