summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/imageviewer/texture.cpp
blob: 86ec9d0066bce7d0381ed1dff9e9a1bb89776498 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/***************************************************************************
 *   Copyright (C) 2007 by Markus Leuthold   *
 *   <kusi (+at) forum.titlis.org>   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.        *
 ***************************************************************************/

#undef PERFORMANCE_ANALYSIS

// TQt includes
#include <tqwmatrix.h>
#include <tqfileinfo.h>

//KDE includes
#include <kdebug.h>

// LibKDcraw includes.

#include <libkdcraw/version.h>
#include <libkdcraw/kdcraw.h>

#if KDCRAW_VERSION < 0x000106
#include <libkdcraw/dcrawbinary.h>
#endif

// libkipi includes

#include <libkipi/interface.h>
#include <libkipi/imagecollection.h>

// Local includes
#ifdef PERFORMANCE_ANALYSIS
	#include "timer.h"
#endif
#include "texture.h"

using namespace KIPIviewer;

Texture::Texture(KIPI::Interface *i)
{
	kipiInterface = i;
	rotate_list[0]=90;
	rotate_list[1]=180;
	rotate_list[2]=270;
	rotate_list[3]=180;
	rotate_idx=0;
	reset();
}


Texture::~Texture()
{
}


/*!
	\fn Texture::height()
 */
int Texture::height()
{
	return glimage.height();
}


/*!
	\fn Texture::width()
 */
int Texture::width()
{
	return glimage.width();
}


/*!
	\fn Texture::load(TQString fn, TQSize size, GLuint tn)
	\brief load file from disc and save it in texture
	\param fn filename to load
	\param size size of image which is downloaded to texture mem
	\param tn texture id generated by glGenTexture
	if "size" is set to image size, scaling is only performed by the GPU but not 
	by the CPU, however the AGP usage to texture memory is increased (20MB for a 5mp image)
 */
bool Texture::load(TQString fn, TQSize size, GLuint tn)
{
	filename=fn;
	initial_size=size;
	_texnr=tn;

	// check if its a RAW file.
#if KDCRAW_VERSION < 0x000106
    TQString rawFilesExt(KDcrawIface::DcrawBinary::instance()->rawFiles());
#else
    TQString rawFilesExt(KDcrawIface::KDcraw::rawFiles());
#endif
	TQFileInfo fileInfo(fn);
	if (rawFilesExt.upper().contains( fileInfo.extension(false).upper() )) {
		// it's a RAW file, use the libkdcraw loader
		KDcrawIface::KDcraw::loadDcrawPreview(qimage, fn);
	}	else {
		// use the standard loader
		qimage=TQImage(fn);
	}
	
	//handle rotation
	KIPI::ImageInfo info = kipiInterface->info(filename);
	if (info.angle() != 0) {
		TQWMatrix r;
		r.rotate(info.angle());
		qimage=qimage.xForm(r);
		kdDebug(51000) << "image rotated by " << info.angle() << " degree" << endl;
	}
	
	if (qimage.isNull()) {
		return false;
	}
	
	_load();
	reset();
	rotate_idx=0;
	return true;
}

/*!
	\fn Texture::load(TQImage im, TQSize size, GLuint tn)
	\brief copy file from TQImage to texture
	\param im Qimage to be copied from
	\param size size of image which is downloaded to texture mem
	\param tn texture id generated by glGenTexture
	if "size" is set to image size, scaling is only performed by the GPU but not 
	by the CPU, however the AGP usage to texture memory is increased (20MB for a 5mp image)
 */
bool Texture::load(TQImage im, TQSize size, GLuint tn)
{
	qimage=im;
	initial_size=size;
	_texnr=tn;
	_load();
	reset();
	rotate_idx=0;
	return true;
}

/*!
	\fn Texture::load()
	internal load function
	rt[xy] <= 1
 */
bool Texture::_load()
{
	int w=initial_size.width();
	int h=initial_size.height();

	if (w==0 || w>qimage.width() || h>qimage.height()) {
		glimage=TQGLWidget::convertToGLFormat(qimage);
	} else {
		glimage=TQGLWidget::convertToGLFormat(qimage.scale(w,h,TQImage::ScaleMin));
	}

	w=glimage.width();
	h=glimage.height();
	if (h < w) {
		rtx = 1;
		rty = float(h)/float(w);
	}
	else {
		rtx = float(w)/float(h);
		rty = 1;
	}	
	return true;
}

/*!
	\fn Texture::data()
 */
GLvoid * Texture::data()
{
	return glimage.bits();
}


/*!
	\fn Texture::texnr()
 */
GLuint Texture::texnr()
{
	return _texnr;
}



/*!
	\fn Texture::zoom(float delta, TQPoint mousepos)
	\brief calculate new tex coords on zooming
	\param delta delta between previous zoom and current zoom
	\param mousepos mouse position returned by QT
	\TODO rename mousepos to something more generic
 */
void Texture::zoom(float delta, TQPoint mousepos)
//u: start in texture, u=[0..1], u=0 is begin, u=1 is end of texture
//z=[0..1], z=1 -> no zoom
//l: length of tex in glFrustum coordinate system
//rt: ratio of tex, rt<=1, see _load() for definition
//rd: ratio of display, rd>=1
//m: mouse pos normalized, cd=[0..rd]
//c:  mouse pos normalized to zoom*l, c=[0..1]		
{
	z*=delta;
	delta=z*(1.0/delta-1.0); //convert to real delta=z_old-z_new
	
	float mx=mousepos.x()/(float)display_x*rdx;
	float cx=(mx-rdx/2.0+rtx/2.0)/rtx;
	float vx=ux+cx*z;
	ux=ux+(vx-ux)*delta/z;	
	
	float my=mousepos.y()/(float)display_y*rdy;
	float cy=(my-rdy/2.0+rty/2.0)/rty;
	cy=1-cy;
	float vy=uy+cy*z;
	uy=uy+(vy-uy)*delta/z;

	calcVertex();
}

/*!
	\fn Texture::calcVertex()
	Calculate vertices according internal state variables
	z, ux, uy are calculated in Texture::zoom()
 */
void Texture::calcVertex()
// rt: ratio of tex, rt<=1, see _load() for definition
// u: start in texture, u=[0..1], u=0 is begin, u=1 is end of texture
// l: length of tex in glFrustum coordinate system
// halftexel: the color of a texel is determined by a corner of the texel and not its center point
//                  this seems to introduce a visible jump on changing the tex-size.
//
// the glFrustum coord-sys is visible in [-rdx..rdx] ([-1..1] for square screen) for z=1 (no zoom)
// the tex coord-sys goes from [-rtx..rtx] ([-1..1] for square texture)	
{
	// x part
   	float lx=2*rtx/z;  //length of tex
	float tsx=lx/(float)glimage.width(); //texelsize in glFrustum coordinates
	float halftexel_x = tsx/2.0;
	float wx=lx*(1-ux-z);
	vleft = -rtx-ux*lx - halftexel_x;   //left
	vright = rtx+wx - halftexel_x;      //right
	
	// y part
	float ly=2*rty/z; 
	float tsy=ly/(float)glimage.height(); //texelsize in glFrustum coordinates
	float halftexel_y = tsy/2.0;	
	float wy=ly*(1-uy-z);
	vbottom = -rty - uy*ly + halftexel_y; //bottom
	vtop = rty + wy + halftexel_y;        //top

}

/*!
	\fn Texture::vertex_bottom()
 */
GLfloat Texture::vertex_bottom()
{
    return (GLfloat) vbottom;
}

/*!
	\fn Texture::vertex_top()
 */
GLfloat Texture::vertex_top()
{
    return (GLfloat) vtop;
}


/*!
	\fn Texture::vertex_left()
 */
GLfloat Texture::vertex_left()
{
	return (GLfloat) vleft;
}


/*!
	\fn Texture::vertex_right()
 */
GLfloat Texture::vertex_right()
{
	return (GLfloat) vright;
}


/*!
	\fn Texture::setViewport(int w, int h)
	\param w width of window
	\param h height of window
	Set widget's viewport. Ensures that rdx & rdy are always > 1
 */
void Texture::setViewport(int w, int h)
{
	if (h>w) {
		rdx=1.0;
		rdy=h/float(w);
	}
	else {
		rdx=w/float(h);
		rdy=1.0;	
	}
	display_x=w;
	display_y=h;
}


/*!
	\fn Texture::move(TQPoint diff)
	new tex coordinates have to be calculated if the view is panned
 */
void Texture::move(TQPoint diff)
{
	ux=ux-diff.x()/float(display_x)*z*rdx/rtx;
	uy=uy+diff.y()/float(display_y)*z*rdy/rty;
	calcVertex();
}


/*!
	\fn Texture::reset()
 */
void Texture::reset()
{
	ux=0;
	uy=0;
	z=1.0;
	float zoomdelta=0;

	if ((rtx<rty) && (rdx<rdy) && (rtx/rty < rdx/rdy)) {
		zoomdelta=z-rdx/rdy;
	}
	if ((rtx<rty) && (rtx/rty > rdx/rdy)) {
		zoomdelta=z-rtx;
	}
	
	if ((rtx>=rty) && (rdy<rdx) && (rty/rtx < rdy/rdx)) {
		zoomdelta=z-rdy/rdx;
	}
	if ((rtx>=rty) && (rty/rtx > rdy/rdx)) {
		zoomdelta=z-rty;
	}
	TQPoint p =  TQPoint(display_x/2,display_y/2);
	zoom(1.0-zoomdelta,p);
	
	calcVertex();
}

/*!
	\fn Texture::setSize(TQSize size)
	\param size desired texture size. TQSize(0,0) will take the full image
	\return true if size has changed, false otherwise
	set new texture size in order to reduce AGP bandwidth
 */
bool Texture::setSize(TQSize size)
{
	//don't allow larger textures than the original image. the image will be upsampled by 
	//OpenGL if necessary and not by TQImage::scale
	size=size.boundedTo(qimage.size());
	
	if (glimage.width()==size.width()) {
		return false;
	}

	int w=size.width();
	int h=size.height();

	if (w==0) {
		glimage=TQGLWidget::convertToGLFormat(qimage);
	} else {
		glimage=TQGLWidget::convertToGLFormat(qimage.scale(w,h,TQImage::ScaleMin));
	}
	
	//recalculate half-texel offset
	calcVertex(); 
	
	return true;
}


/*!
	\fn Texture::rotate()
	\brief smart image rotation
	since the two most frequent usecases are a CW or CCW rotation of 90, 
	perform these rotation with one (+90) or two (-90) calls of rotation()
 */
void Texture::rotate()
{
	TQWMatrix r;
	r.rotate(rotate_list[rotate_idx%4]);		
	qimage=qimage.xForm(r);
	_load();
	
	//save new rotation in exif header
	KIPI::ImageInfo info = kipiInterface->info(filename);
	info.setAngle(rotate_list[rotate_idx%4]);
	
	reset();
	rotate_idx++;
}


/*!
	\fn Texture::setToOriginalSize()
	zoom image such that each pixel of the screen corresponds to a pixel in the jpg
	remember that OpenGL is not a pixel exact specification, and the image will still be filtered by OpenGL
 */
void Texture::zoomToOriginal()
{
	float zoomfactorToOriginal;
	reset();
	
	if (qimage.width()/qimage.height() > float(display_x)/float(display_y)) {
		//image touches right and left edge of window
		zoomfactorToOriginal=float(display_x)/qimage.width();
	} else {
		//image touches upper and lower edge of window
		zoomfactorToOriginal=float(display_y)/qimage.height();
	}
		
	zoom(zoomfactorToOriginal,TQPoint(display_x/2,display_y/2));
}