46 #ifndef SIFTGPU_NO_DEVIL
58 _imgWidth = _imgHeight = 0;
59 _texWidth = _texHeight = 0;
60 _drawWidth = _drawHeight = 0;
67 if(_texID) glDeleteTextures(1, &_texID);
76 glGetTexLevelParameteriv(_texTarget, 0, GL_TEXTURE_WIDTH , &tw);
77 glGetTexLevelParameteriv(_texTarget, 0, GL_TEXTURE_HEIGHT , &th);
79 return tw == _texWidth && th == _texHeight;
82 return _texWidth == 0 && _texHeight ==0;
90 _drawWidth = _imgWidth =
width;
91 _drawHeight = _imgHeight =
height;
97 if(_texID &&
width == _texWidth &&
height == _texHeight )
return;
98 if(_texID==0) glGenTextures(1, &_texID);
100 _texWidth = _imgWidth = _drawWidth =
width;
101 _texHeight = _imgHeight = _drawHeight =
height;
107 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
108 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
112 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
113 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
115 glTexParameteri(_texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
116 glTexParameteri(_texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
117 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
119 glTexImage2D(_texTarget, 0, _iTexFormat,
120 _texWidth, _texHeight, 0, GL_RGBA, GL_FLOAT,
NULL);
121 CheckErrorsGL(
"glTexImage2D");
132 if(_texID &&
width == _texWidth &&
height == _texHeight )
return;
133 if(_texID==0) glGenTextures(1, &_texID);
135 _texWidth = _imgWidth = _drawWidth =
width;
136 _texHeight = _imgHeight = _drawHeight =
height;
142 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
143 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
147 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
148 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
150 glTexParameteri(_texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
151 glTexParameteri(_texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
152 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
154 glTexImage2D(_texTarget, 0,
format, _texWidth, _texHeight, 0, GL_RGBA, GL_FLOAT,
NULL);
161 glBindTexture(_texTarget, _texID);
166 glBindTexture(_texTarget, 0);
173 glTexCoord2i ( 0 , 0 ); glVertex2i ( 0 , 0 );
174 glTexCoord2i ( 0 , _drawHeight ); glVertex2i ( 0 , _drawHeight );
175 glTexCoord2i ( _drawWidth , _drawHeight ); glVertex2i ( _drawWidth , _drawHeight );
176 glTexCoord2i ( _drawWidth , 0 ); glVertex2i ( _drawWidth , 0 );
184 marginx =
min(marginx, _texWidth - _imgWidth);
185 marginy =
min(marginy, _texHeight - _imgHeight);
186 if(marginx >0 || marginy > 0)
192 DrawMargin(_imgWidth + marginx, _imgHeight + marginy);
198 ZeroHistoMargin(_imgWidth, _imgHeight);
203 int marginx =
width & 0x01;
204 int marginy =
height & 0x01;
205 if(marginx >0 || marginy > 0)
207 int right =
width + marginx;
208 int bottom =
height + marginy;
215 glTexCoord2i (
width , 0 ); glVertex2i (
width , 0 );
216 glTexCoord2i (
width , bottom ); glVertex2i (
width , bottom );
217 glTexCoord2i ( right , bottom ); glVertex2i ( right , bottom );
218 glTexCoord2i ( right , 0 ); glVertex2i ( right , 0 );
223 glTexCoord2i ( 0 , bottom ); glVertex2i ( 0 , bottom );
224 glTexCoord2i (
width , bottom ); glVertex2i (
width , bottom );
236 if(right > _drawWidth)
238 glTexCoord2i ( _drawWidth , 0 ); glVertex2i ( _drawWidth , 0 );
239 glTexCoord2i ( _drawWidth , bottom ); glVertex2i ( _drawWidth , bottom );
240 glTexCoord2i ( right , bottom ); glVertex2i ( right , bottom );
241 glTexCoord2i ( right , 0 ); glVertex2i ( right , 0 );
243 if(bottom>_drawHeight)
245 glTexCoord2i ( 0 , _drawHeight ); glVertex2i ( 0 , _drawHeight );
246 glTexCoord2i ( 0 , bottom ); glVertex2i ( 0 , bottom );
247 glTexCoord2i ( _drawWidth , bottom ); glVertex2i ( _drawWidth , bottom );
248 glTexCoord2i ( _drawWidth , _drawHeight ); glVertex2i ( _drawWidth , _drawHeight );
259 int w = _drawWidth, h = _drawHeight;
261 glMultiTexCoord2i( GL_TEXTURE0, 0 , 0 );
262 glMultiTexCoord2i( GL_TEXTURE1, -1 , 0 );
263 glMultiTexCoord2i( GL_TEXTURE2, 1 , 0 );
264 glMultiTexCoord2i( GL_TEXTURE3, 0 , -1 );
265 glMultiTexCoord2i( GL_TEXTURE4, 0 , 1 );
266 glVertex2i ( 0 , 0 );
268 glMultiTexCoord2i( GL_TEXTURE0, 0 , h );
269 glMultiTexCoord2i( GL_TEXTURE1, -1 , h );
270 glMultiTexCoord2i( GL_TEXTURE2, 1 , h );
271 glMultiTexCoord2i( GL_TEXTURE3, 0 , h -1 );
272 glMultiTexCoord2i( GL_TEXTURE4, 0 , h +1 );
273 glVertex2i ( 0 , h );
276 glMultiTexCoord2i( GL_TEXTURE0, w , h );
277 glMultiTexCoord2i( GL_TEXTURE1, w-1 , h );
278 glMultiTexCoord2i( GL_TEXTURE2, w+1 , h );
279 glMultiTexCoord2i( GL_TEXTURE3, w , h-1 );
280 glMultiTexCoord2i( GL_TEXTURE4, w , h+1 );
281 glVertex2i ( w , h );
283 glMultiTexCoord2i( GL_TEXTURE0, w , 0 );
284 glMultiTexCoord2i( GL_TEXTURE1, w-1 , 0 );
285 glMultiTexCoord2i( GL_TEXTURE2, w+1 , 0 );
286 glMultiTexCoord2i( GL_TEXTURE3, w , -1 );
287 glMultiTexCoord2i( GL_TEXTURE4, w , 1 );
288 glVertex2i ( w , 0 );
299 glMultiTexCoord2i( GL_TEXTURE0, 0 , 0 );
300 glMultiTexCoord2i( GL_TEXTURE1, -1 , 0 );
301 glMultiTexCoord2i( GL_TEXTURE2, 1 , 0 );
302 glMultiTexCoord2i( GL_TEXTURE3, 0 , -1 );
303 glMultiTexCoord2i( GL_TEXTURE4, 0 , 1 );
304 glMultiTexCoord2i( GL_TEXTURE5, -1 , -1 );
305 glMultiTexCoord2i( GL_TEXTURE6, -1 , 1 );
306 glMultiTexCoord2i( GL_TEXTURE7, 1 , -1 );
307 glVertex2i ( 0 , 0 );
309 glMultiTexCoord2i( GL_TEXTURE0, 0 , h );
310 glMultiTexCoord2i( GL_TEXTURE1, -1 , h );
311 glMultiTexCoord2i( GL_TEXTURE2, 1 , h );
312 glMultiTexCoord2i( GL_TEXTURE3, 0 , h -1 );
313 glMultiTexCoord2i( GL_TEXTURE4, 0 , h +1 );
314 glMultiTexCoord2i( GL_TEXTURE5, -1 , h -1 );
315 glMultiTexCoord2i( GL_TEXTURE6, -1 , h +1 );
316 glMultiTexCoord2i( GL_TEXTURE7, 1 , h -1 );
317 glVertex2i ( 0 , h );
320 glMultiTexCoord2i( GL_TEXTURE0, w , h );
321 glMultiTexCoord2i( GL_TEXTURE1, w-1 , h );
322 glMultiTexCoord2i( GL_TEXTURE2, w+1 , h );
323 glMultiTexCoord2i( GL_TEXTURE3, w , h -1 );
324 glMultiTexCoord2i( GL_TEXTURE4, w , h +1 );
325 glMultiTexCoord2i( GL_TEXTURE5, w-1 , h -1 );
326 glMultiTexCoord2i( GL_TEXTURE6, w-1 , h +1 );
327 glMultiTexCoord2i( GL_TEXTURE7, w+1 , h -1 );
328 glVertex2i ( w , h );
330 glMultiTexCoord2i( GL_TEXTURE0, w , 0 );
331 glMultiTexCoord2i( GL_TEXTURE1, w-1 , 0 );
332 glMultiTexCoord2i( GL_TEXTURE2, w+1 , 0 );
333 glMultiTexCoord2i( GL_TEXTURE3, w , -1 );
334 glMultiTexCoord2i( GL_TEXTURE4, w , 1 );
335 glMultiTexCoord2i( GL_TEXTURE5, w-1 , -1 );
336 glMultiTexCoord2i( GL_TEXTURE6, w-1 , 1 );
337 glMultiTexCoord2i( GL_TEXTURE7, w+1 , -1 );
338 glVertex2i ( w , 0 );
365 glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, i+GL_COLOR_ATTACHMENT0_EXT, _texTarget, _texID, 0 );
370 glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, i+GL_COLOR_ATTACHMENT0_EXT, _texTarget, 0, 0 );
378 glTexCoord2f ( x1 , y1 ); glVertex2f ( x1 , y1 );
379 glTexCoord2f ( x1 , y2 ); glVertex2f ( x1 , y2 );
380 glTexCoord2f ( x2 , y2 ); glVertex2f ( x2 , y2 );
381 glTexCoord2f ( x2 , y1 ); glVertex2f ( x2 , y1 );
395 glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
403 DrawScaledQuad(
float(scale));
408 DrawScaledQuad(1.0f/scale);
415 float to = 0.5f -0.5f * texscale;
416 float tx = _imgWidth*texscale +to;
417 float ty = _imgHeight*texscale +to;
419 glTexCoord2f ( to , to ); glVertex2i ( 0 , 0 );
420 glTexCoord2f ( to , ty ); glVertex2i ( 0 , _imgHeight );
421 glTexCoord2f ( tx , ty ); glVertex2i ( _imgWidth , _imgHeight );
422 glTexCoord2f ( tx , to ); glVertex2i ( _imgWidth , 0 );
434 glMultiTexCoord2f ( GL_TEXTURE0, to , to );
435 glMultiTexCoord2f ( GL_TEXTURE1, to +1, to );
436 glMultiTexCoord2f ( GL_TEXTURE2, to , to+1 );
437 glMultiTexCoord2f ( GL_TEXTURE3, to +1, to+1 );
438 glVertex2i ( 0 , 0 );
440 glMultiTexCoord2f ( GL_TEXTURE0, to , ty );
441 glMultiTexCoord2f ( GL_TEXTURE1, to +1, ty );
442 glMultiTexCoord2f ( GL_TEXTURE2, to , ty +1 );
443 glMultiTexCoord2f ( GL_TEXTURE3, to +1, ty +1 );
444 glVertex2i ( 0 , h );
446 glMultiTexCoord2f ( GL_TEXTURE0, tx , ty );
447 glMultiTexCoord2f ( GL_TEXTURE1, tx +1, ty );
448 glMultiTexCoord2f ( GL_TEXTURE2, tx , ty +1);
449 glMultiTexCoord2f ( GL_TEXTURE3, tx +1, ty +1);
451 glVertex2i ( w , h );
453 glMultiTexCoord2f ( GL_TEXTURE0, tx , to );
454 glMultiTexCoord2f ( GL_TEXTURE1, tx +1, to );
455 glMultiTexCoord2f ( GL_TEXTURE2, tx , to +1 );
456 glMultiTexCoord2f ( GL_TEXTURE3, tx +1, to +1 );
457 glVertex2i ( w , 0 );
467 float tx = _drawWidth*2 +to;
468 float ty = _drawHeight*2 +to;
470 glMultiTexCoord2f ( GL_TEXTURE0, to , to );
471 glMultiTexCoord2f ( GL_TEXTURE1, to +1, to );
472 glMultiTexCoord2f ( GL_TEXTURE2, to , to+1 );
473 glMultiTexCoord2f ( GL_TEXTURE3, to +1, to+1 );
474 glVertex2i ( 0 , 0 );
476 glMultiTexCoord2f ( GL_TEXTURE0, to , ty );
477 glMultiTexCoord2f ( GL_TEXTURE1, to +1, ty );
478 glMultiTexCoord2f ( GL_TEXTURE2, to , ty +1 );
479 glMultiTexCoord2f ( GL_TEXTURE3, to +1, ty +1 );
480 glVertex2i ( 0 , _drawHeight );
482 glMultiTexCoord2f ( GL_TEXTURE0, tx , ty );
483 glMultiTexCoord2f ( GL_TEXTURE1, tx +1, ty );
484 glMultiTexCoord2f ( GL_TEXTURE2, tx , ty +1);
485 glMultiTexCoord2f ( GL_TEXTURE3, tx +1, ty +1);
487 glVertex2i ( _drawWidth , _drawHeight );
489 glMultiTexCoord2f ( GL_TEXTURE0, tx , to );
490 glMultiTexCoord2f ( GL_TEXTURE1, tx +1, to );
491 glMultiTexCoord2f ( GL_TEXTURE2, tx , to +1 );
492 glMultiTexCoord2f ( GL_TEXTURE3, tx +1, to +1 );
493 glVertex2i ( _drawWidth , 0 );
502 _drawWidth = (1 + _imgWidth) >> 1;
503 _drawHeight = (1 + _imgHeight) >> 1;
506 GLuint oldTexID = _texID;
507 glGenTextures(1, &_texID);
508 glBindTexture(_texTarget, _texID);
509 glTexImage2D(_texTarget, 0, _iTexFormat, _texWidth, _texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
NULL);
512 glBindTexture(_texTarget, oldTexID);
518 glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
523 glDeleteTextures(1, &oldTexID);
530 _imgWidth =
width; _drawWidth = (
width + 1) >> 1;
537 if(_texID &&
width == _imgWidth &&
height == _imgHeight )
return;
538 if(_texID==0) glGenTextures(1, &_texID);
544 _texWidth = (
width + 2) >> 1;
545 _texHeight = (
height + 2) >> 1;
548 _texWidth = (
width + 1) >> 1;
549 _texHeight = (
height + 1) >> 1;
551 _drawWidth = (
width + 1) >> 1;
552 _drawHeight = (
height + 1) >> 1;
558 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
559 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
563 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
564 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
566 glTexParameteri(_texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
567 glTexParameteri(_texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
568 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
570 glTexImage2D(_texTarget, 0, _iTexFormat,
571 _texWidth, _texHeight, 0, GL_RGBA, GL_FLOAT,
NULL);
581 float x2 = _imgWidth*0.5f +x1;
582 float y2 = _imgHeight*0.5f + y1;
584 glTexCoord2f ( x1 , y1 ); glVertex2i ( 0 , 0 );
585 glTexCoord2f ( x1 , y2 ); glVertex2i ( 0 , _imgHeight );
586 glTexCoord2f ( x2 , y2 ); glVertex2i ( _imgWidth , _imgHeight );
587 glTexCoord2f ( x2 , y1 ); glVertex2i ( _imgWidth , 0 );
594 int tw =_drawWidth, th = _drawHeight;
595 float texscale = 1.0f / scale;
596 float x1 = 0.5f - 0.5f*scale, y1 = x1;
597 float x2 = tw * texscale + x1;
598 float y2 = th * texscale + y1;
599 float step = texscale *0.5f;
601 glMultiTexCoord2f( GL_TEXTURE0, x1 , y1 );
602 glMultiTexCoord2f( GL_TEXTURE1, x1+step , y1 );
603 glMultiTexCoord2f( GL_TEXTURE2, x1 , y1 +step);
604 glMultiTexCoord2f( GL_TEXTURE3, x1+step , y1 +step);
605 glVertex2i ( 0 , 0 );
607 glMultiTexCoord2f( GL_TEXTURE0, x1 , y2 );
608 glMultiTexCoord2f( GL_TEXTURE1, x1+step , y2 );
609 glMultiTexCoord2f( GL_TEXTURE2, x1 , y2 +step);
610 glMultiTexCoord2f( GL_TEXTURE3, x1+step , y2 +step);
611 glVertex2i ( 0 , th );
613 glMultiTexCoord2f( GL_TEXTURE0, x2 , y2 );
614 glMultiTexCoord2f( GL_TEXTURE1, x2+step , y2 );
615 glMultiTexCoord2f( GL_TEXTURE2, x2 , y2 +step);
616 glMultiTexCoord2f( GL_TEXTURE3, x2+step , y2 +step);
617 glVertex2i ( tw , th );
619 glMultiTexCoord2f( GL_TEXTURE0, x2 , y1 );
620 glMultiTexCoord2f( GL_TEXTURE1, x2+step , y1 );
621 glMultiTexCoord2f( GL_TEXTURE2, x2 , y1 +step);
622 glMultiTexCoord2f( GL_TEXTURE3, x2+step , y1 +step);
623 glVertex2i ( tw , 0 );
630 int th = _drawHeight;
631 float x1 = 0.5f - 0.5f*scale;
632 float x2 = tw * scale + x1;
633 float y1 = 0.5f - 0.5f * scale;
634 float y2 = th * scale + y1;
635 int step = scale / 2;
638 glMultiTexCoord2f( GL_TEXTURE0, x1 , y1 );
639 glMultiTexCoord2f( GL_TEXTURE1, x1+step , y1 );
640 glMultiTexCoord2f( GL_TEXTURE2, x1 , y1 +step);
641 glMultiTexCoord2f( GL_TEXTURE3, x1+step , y1 +step);
642 glVertex2i ( 0 , 0 );
644 glMultiTexCoord2f( GL_TEXTURE0, x1 , y2 );
645 glMultiTexCoord2f( GL_TEXTURE1, x1+step , y2 );
646 glMultiTexCoord2f( GL_TEXTURE2, x1 , y2 +step);
647 glMultiTexCoord2f( GL_TEXTURE3, x1+step , y2 +step);
648 glVertex2i ( 0 , th );
650 glMultiTexCoord2f( GL_TEXTURE0, x2 , y2 );
651 glMultiTexCoord2f( GL_TEXTURE1, x2+step , y2 );
652 glMultiTexCoord2f( GL_TEXTURE2, x2 , y2 +step);
653 glMultiTexCoord2f( GL_TEXTURE3, x2+step , y2 +step);
654 glVertex2i ( tw , th );
656 glMultiTexCoord2f( GL_TEXTURE0, x2 , y1 );
657 glMultiTexCoord2f( GL_TEXTURE1, x2+step , y1 );
658 glMultiTexCoord2f( GL_TEXTURE2, x2 , y1 +step);
659 glMultiTexCoord2f( GL_TEXTURE3, x2+step , y1 +step);
660 glVertex2i ( tw , 0 );
666 int marginx = (((_imgWidth + 3) /4)*4) - _imgWidth;
667 int marginy = (((-_imgHeight + 3)/4)*4) - _imgHeight;
668 if(marginx >0 || marginy > 0)
670 int tw = (_imgWidth + marginx ) >> 1;
671 int th = (_imgHeight + marginy ) >> 1;
672 tw =
min(_texWidth, tw );
673 th =
min(_texHeight, th);
678 DrawMargin(tw, th, 1, 1);
686 marginx =
min(marginx, _texWidth * 2 - _imgWidth);
687 marginy =
min(marginy, _texHeight * 2 - _imgHeight);
688 if(marginx >0 || marginy > 0)
690 int tw = (_imgWidth + marginx + 1) >> 1;
691 int th = (_imgHeight + marginy + 1) >> 1;
696 DrawMargin(tw, th, marginx, marginy);
701 int tw = (_imgWidth >>1);
702 int th = (_imgHeight >>1);
706 glTexCoord2i ( tw , 0 ); glVertex2i ( tw , 0 );
707 glTexCoord2i ( tw , bottom ); glVertex2i ( tw , bottom );
708 glTexCoord2i ( right, bottom ); glVertex2i ( right, bottom );
709 glTexCoord2i ( right, 0 ); glVertex2i ( right, 0 );
713 glTexCoord2i ( 0 , th ); glVertex2i ( 0 , th );
714 glTexCoord2i ( 0 , bottom ); glVertex2i ( 0 , bottom );
715 glTexCoord2i ( tw , bottom ); glVertex2i ( tw , bottom );
716 glTexCoord2i ( tw , th ); glVertex2i ( tw , th );
726 for(
int i = n-1; i>=0; i--)
728 glActiveTexture(GL_TEXTURE0+i);
729 glBindTexture(_texTarget, 0);
733 template <
class U
int>
int
735 #if !defined(_MSC_VER) || _MSC_VER > 1200
739 DownSamplePixelDataI(
unsigned int gl_format,
int width,
int height,
int ds,
740 const Uint * pin, Uint * pout)
746 const Uint * line = pin, * p;
751 case GL_LUMINANCE_ALPHA:
752 step = ds * (gl_format == GL_LUMINANCE? 1: 2);
753 linestep =
width * step;
754 for(i = 0 ; i < hs; i++, line+=linestep)
756 for(j = 0, p = line; j < ws; j++, p+=step)
764 step = ds * (gl_format == GL_RGB? 3: 4);
765 linestep =
width * step;
767 for(i = 0 ; i < hs; i++, line+=linestep)
769 for(j = 0, p = line; j < ws; j++, p+=step)
772 *po++ = ((19595*p[0] + 38470*p[1] + 7471*p[2]+ 32768)>>16);
778 step = ds * (gl_format == GL_BGR? 3: 4);
779 linestep =
width * step;
780 for(i = 0 ; i < hs; i++, line+=linestep)
782 for(j = 0, p = line; j < ws; j++, p+=step)
784 *po++ = ((7471*p[0] + 38470*p[1] + 19595*p[2]+ 32768)>>16);
797 template <
class U
int>
int
799 #if !defined(_MSC_VER) || _MSC_VER > 1200
803 DownSamplePixelDataI2F(
unsigned int gl_format,
int width,
int height,
int ds,
804 const Uint * pin,
float * pout,
int skip)
808 int ws =
width/ds - skip;
810 const Uint * line = pin, * p;
812 const float factor = (
sizeof(Uint) == 1? 255.0f : 65535.0f);
816 case GL_LUMINANCE_ALPHA:
817 step = ds * (gl_format == GL_LUMINANCE? 1: 2);
818 linestep =
width * step;
819 for(i = 0 ; i < hs; i++, line+=linestep)
821 for(j = 0, p = line; j < ws; j++, p+=step)
823 *po++ = (*p) / factor;
829 step = ds * (gl_format == GL_RGB? 3: 4);
830 linestep =
width * step;
832 for(i = 0 ; i < hs; i++, line+=linestep)
834 for(j = 0, p = line; j < ws; j++, p+=step)
837 *po++ = ((19595*p[0] + 38470*p[1] + 7471*p[2]) / (65535.0f * factor));
843 step = ds * (gl_format == GL_BGR? 3: 4);
844 linestep =
width * step;
845 for(i = 0 ; i < hs; i++, line+=linestep)
847 for(j = 0, p = line; j < ws; j++, p+=step)
849 *po++ = ((7471*p[0] + 38470*p[1] + 19595*p[2]) / (65535.0f * factor));
863 int ws =
width/ds - skip;
865 const float * line = pin, * p;
870 case GL_LUMINANCE_ALPHA:
871 step = ds * (gl_format == GL_LUMINANCE? 1: 2);
872 linestep =
width * step;
873 for(i = 0 ; i < hs; i++, line+=linestep)
875 for(j = 0, p = line; j < ws; j++, p+=step)
883 step = ds * (gl_format == GL_RGB? 3: 4);
884 linestep =
width * step;
885 for(i = 0 ; i < hs; i++, line+=linestep)
887 for(j = 0, p = line; j < ws; j++, p+=step)
889 *po++ = (0.299f*p[0] + 0.587f*p[1] + 0.114f*p[2]);
895 step = ds * (gl_format == GL_BGR? 3: 4);
896 linestep =
width * step;
897 for(i = 0 ; i < hs; i++, line+=linestep)
899 for(j = 0, p = line; j < ws; j++, p+=step)
901 *po++ = (0.114f*p[0] + 0.587f*p[1] + 0.299f * p[2]);
914 unsigned int gl_format,
unsigned int gl_type )
916 int simple_format = IsSimpleGlFormat(gl_format, gl_type);
917 int ws, hs, done = 1;
919 if(_converted_data) {
delete [] _converted_data; _converted_data =
NULL; }
938 if ( ws > _texMaxDim || hs > _texMaxDim)
948 }
while(ws > _texMaxDim || hs > _texMaxDim);
951 std::cerr<<
"Input images is too big to fit into a texture\n";
956 _texWidth = _imgWidth = _drawWidth = ws;
957 _texHeight = _imgHeight = _drawHeight = hs;
961 std::cout<<
"Image size :\t"<<
width<<
"x"<<
height<<
"\n";
962 if(_down_sampled >0) std::cout<<
"Down sample to \t"<<ws<<
"x"<<hs<<
"\n";
969 int tWidth = TruncateWidthCU(_imgWidth);
970 int skip = _imgWidth - tWidth;
974 std::cerr <<
"Input format not supported under current settings.\n";
976 }
else if(_down_sampled > 0 || gl_format != GL_LUMINANCE || gl_type != GL_FLOAT)
978 _converted_data =
new float [_imgWidth * _imgHeight];
979 if(gl_type == GL_UNSIGNED_BYTE)
980 DownSamplePixelDataI2F(gl_format,
width,
height, 1<<_down_sampled,
981 ((
const unsigned char*) data), _converted_data, skip);
982 else if(gl_type == GL_UNSIGNED_SHORT)
983 DownSamplePixelDataI2F(gl_format,
width,
height, 1<<_down_sampled,
984 ((
const unsigned short*) data), _converted_data, skip);
986 DownSamplePixelDataF(gl_format,
width,
height, 1<<_down_sampled, (
float*)data, _converted_data, skip);
988 _pixel_data = _converted_data;
996 for(
int i = 1; i < _imgHeight; ++i)
998 float * dst = ((
float*)data) + i * tWidth, * src = ((
float*)data) + i * _imgWidth;
999 for(
int j = 0; j < tWidth; ++j) *dst++ = * src++;
1003 _texWidth = _imgWidth = _drawWidth = tWidth;
1007 if(_texID ==0) glGenTextures(1, &_texID);
1008 glBindTexture(_texTarget, _texID);
1009 CheckErrorsGL(
"glBindTexture");
1010 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1011 glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1012 glPixelStorei(GL_UNPACK_ALIGNMENT , 1);
1017 if(gl_type == GL_UNSIGNED_BYTE)
1019 unsigned char * newdata =
new unsigned char [_imgWidth * _imgHeight];
1020 DownSamplePixelDataI(gl_format,
width,
height, 1<<_down_sampled, ((
const unsigned char*) data), newdata);
1021 glTexImage2D(_texTarget, 0, GL_LUMINANCE32F_ARB,
1022 _imgWidth, _imgHeight, 0,
1023 GL_LUMINANCE, GL_UNSIGNED_BYTE, newdata);
1025 }
else if(gl_type == GL_UNSIGNED_SHORT)
1027 unsigned short * newdata =
new unsigned short [_imgWidth * _imgHeight];
1028 DownSamplePixelDataI(gl_format,
width,
height, 1<<_down_sampled, ((
const unsigned short*) data), newdata);
1030 glTexImage2D(_texTarget, 0, GL_LUMINANCE32F_ARB,
1031 _imgWidth, _imgHeight, 0,
1032 GL_LUMINANCE, GL_UNSIGNED_SHORT, newdata);
1034 }
else if(gl_type == GL_FLOAT)
1036 float * newdata =
new float [_imgWidth * _imgHeight];
1037 DownSamplePixelDataF(gl_format,
width,
height, 1<<_down_sampled, (
float*)data, newdata);
1038 glTexImage2D(_texTarget, 0, GL_LUMINANCE32F_ARB,
1039 _imgWidth, _imgHeight, 0,
1040 GL_LUMINANCE, GL_FLOAT, newdata);
1052 if(gl_format == GL_LUMINANCE || gl_format == GL_LUMINANCE_ALPHA)
1055 glTexImage2D(_texTarget, 0, GL_LUMINANCE32F_ARB,
1056 _imgWidth, _imgHeight, 0, gl_format, gl_type, data);
1062 glTexImage2D(_texTarget, 0, _iTexFormat, _imgWidth, _imgHeight, 0, gl_format, gl_type, data);
1077 if(_converted_data)
delete [] _converted_data;
1083 #ifndef SIFTGPU_NO_DEVIL
1084 static int devil_loaded = 0;
1088 if(devil_loaded == 0)
1091 ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
1092 ilEnable(IL_ORIGIN_SET);
1097 ilGenImages(1, &imID);
1100 if(ilLoadImage(imagepath))
1102 w = ilGetInteger(IL_IMAGE_WIDTH);
1103 h = ilGetInteger(IL_IMAGE_HEIGHT);
1104 int ilformat = ilGetInteger(IL_IMAGE_FORMAT);
1106 if(SetImageData(w, h, ilGetData(), ilformat, GL_UNSIGNED_BYTE)==0)
1111 std::cout<<
"Image loaded :\t"<<imagepath<<
"\n";
1116 std::cerr<<
"Unable to open image [code = "<<ilGetError()<<
"]\n";
1120 ilDeleteImages(1, &imID);
1124 FILE * file = fopen(imagepath,
"rb");
if (file ==
NULL)
return 0;
1131 std::cerr <<
"ERROR: fileformat not supported\n";
1138 unsigned char * data =
new unsigned char[
width *
height];
1139 unsigned char * pixels = data;
1140 if (strcmp(buf,
"P5")==0 )
1142 fscanf(file,
"%c",buf);
1144 }
else if (strcmp(buf,
"P2")==0 )
1146 for (
int i = 0 ; i<
height; i++)
1148 for (
int j = 0; j <
width; j++)
1150 fscanf(file,
"%d", &g);
1151 *pixels++ = (
unsigned char) g;
1154 }
else if (strcmp(buf,
"P6")==0 )
1156 fscanf(file,
"%c", buf);
1158 unsigned char buf[3];
1159 for ( j =0 ; j< num; j++)
1161 fread(buf,1,3, file);
1162 *pixels++=int(0.10454f* buf[2]+0.60581f* buf[1]+0.28965f* buf[0]);
1164 }
else if (strcmp(buf,
"P3")==0 )
1168 for ( i = 0 ; i< num; i++)
1170 fscanf(file,
"%d %d %d", &r, &g, &b);
1171 *pixels++ = int(0.10454f* b+0.60581f* g+0.28965f* r);
1176 std::cerr <<
"ERROR: fileformat not supported\n";
1179 if(done) SetImageData(
width,
height, data, GL_LUMINANCE, GL_UNSIGNED_BYTE);
1190 if(
format != GL_RGBA &&
format != GL_LUMINANCE)
return 0;
1193 GLint bsize, esize =
width *
height *
sizeof(float) * (
format == GL_RGBA ? 4 : 1);
1195 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
1196 glGetBufferParameteriv(GL_PIXEL_PACK_BUFFER_ARB, GL_BUFFER_SIZE, &bsize);
1199 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, esize,
NULL, GL_STATIC_DRAW_ARB);
1200 glGetBufferParameteriv(GL_PIXEL_PACK_BUFFER_ARB, GL_BUFFER_SIZE, &bsize);
1206 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
1209 return bsize >= esize;
1214 vector<float> buf(GetImgWidth() * GetImgHeight() * 4);
1217 glReadPixels(0, 0, GetImgWidth(), GetImgHeight(), GL_RGBA, GL_FLOAT, &buf[0]);
1220 for(
int i = 0, idx = 0; i < GetImgHeight(); ++i)
1222 for(
int j = 0; j < GetImgWidth(); ++j, idx += 4)
1224 out << i <<
" " << j <<
" " << buf[idx] <<
" " << buf[idx + 1] <<
" "
1225 << buf[idx + 2] <<
" " << buf[idx + 3] <<
"\n";
1234 if(!_data_modified)
return;
1235 if(_pixel_data==
NULL)
return;
1236 InitTexture(_imgWidth, _imgHeight);
1238 glTexImage2D( _texTarget, 0, GL_LUMINANCE32F_ARB,
1239 _imgWidth, _imgHeight, 0,
1240 GL_LUMINANCE, GL_FLOAT, _pixel_data);
1252 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
1255 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
filament::Texture::InternalFormat format
void CopyFromPBO(GLuint pbo, int width, int height, GLenum format=GL_RGBA)
virtual void InitTexture(int width, int height, int clamp_to_edge=1)
virtual void FillMargin(int marginx, int marginy)
int CopyToPBO(GLuint pbo, int width, int height, GLenum format=GL_RGBA)
void SaveToASCII(const char *path)
void DrawScaledQuad(float scale)
virtual void SetImageSize(int width, int height)
static void DetachFBO(int i)
void DrawMargin(int right, int bottom)
virtual void TexConvertRGB()
virtual void ZeroHistoMargin()
void FitRealTexViewPort()
virtual void DrawQuadDS(int scale)
virtual void DrawQuadUS(int scale)
static void UnbindMultiTex(int n)
void DrawMargin(int right, int bottom, int mx, int my)
virtual void ZeroHistoMargin()
virtual void DrawQuadUS(int scale)
virtual void SetImageSize(int width, int height)
virtual void InitTexture(int width, int height, int clamp_to_edge=1)
virtual void DrawQuadDS(int scale)
virtual void FillMargin(int marginx, int marginy)
virtual void TexConvertRGB()
static int _octave_min_default
static int _PreciseBorder
static int _PreProcessOnCPU
static void CheckErrorsGL(const char *location=NULL)
static void FitViewPort(int width, int height)
static void UseShaderZeroPass()
static int HaveShaderMan()
static void UseShaderRGB2Gray()
static void UseShaderMarginCopy(int xmax, int ymax)
static void UnloadProgram()
static const std::string path