-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImage.cpp
174 lines (147 loc) · 5.12 KB
/
Image.cpp
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
/***********************************************************************
Image - Class to represent images as float-valued texture objects.
Copyright (c) 2013-2016 Oliver Kreylos
This file is part of the Augmented Reality Sandbox (SARndbox).
The Augmented Reality Sandbox 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.
The Augmented Reality Sandbox 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 the Augmented Reality Sandbox; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "Image.h"
#include "CImg.h"
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <GL/gl.h>
#include <GL/GLContextData.h>
#include <GL/Extensions/GLARBTextureFloat.h>
#include <GL/Extensions/GLARBTextureRectangle.h>
#include <GL/Extensions/GLARBTextureRg.h>
#include <GL/Extensions/GLARBShaderObjects.h>
#include <Geometry/Matrix.h>
#include <stdio.h>
using namespace cimg_library;
/********************************
Methods of class Image::DataItem:
********************************/
Image::DataItem::DataItem(void)
:textureObjectId(0)
{
/* Check for and initialize all required OpenGL extensions: */
GLARBTextureFloat::initExtension();
GLARBTextureRectangle::initExtension();
GLARBTextureRg::initExtension();
GLARBShaderObjects::initExtension();
/* Create the texture object: */
glGenTextures(1,&textureObjectId);
}
Image::DataItem::~DataItem(void)
{
/* Destroy the texture object: */
glDeleteTextures(1,&textureObjectId);
}
/**********************
Methods of class Image:
**********************/
void Image::calcMatrix(void)
{
/* Convert the image transformation into a projective transformation matrix: */
imageTransform=PTransform(transform);
PTransform::Matrix& dtm=imageTransform.getMatrix();
/* Pre-multiply the projective transformation matrix with the image space to image pixel space transformation: */
PTransform image;
image.getMatrix()(0,0)=Scalar(imageSize[0]-1)/(imageSize[0]);
image.getMatrix()(0,3)=Scalar(0.5);
image.getMatrix()(1,1)=Scalar(imageSize[1]-1)/(-imageSize[1]);
image.getMatrix()(1,3)=Scalar(0.5)+Scalar(imageSize[1]-1);
image.getMatrix()(2,2)=Scalar(1);
image.getMatrix()(2,3)=0.0;
imageTransform.leftMultiply(image);
/* Convert the full transformation to column-major OpenGL format: */
GLfloat* dtmPtr=imageTransformMatrix;
for(int j=0;j<4;++j)
for(int i=0;i<4;++i,++dtmPtr)
*dtmPtr=GLfloat(dtm(i,j));
}
Image::Image(void)
:rgb(0),
transform(OGTransform::identity),
rotateImage(false),
flipImageX(false),
flipImageY(false)
{
imageSize[0]=imageSize[1]=0;
}
Image::~Image(void)
{
delete[] rgb;
}
void Image::initContext(GLContextData& contextData) const
{
/* Create and register a data item: */
DataItem* dataItem=new DataItem;
contextData.addDataItem(this,dataItem);
/* Upload the image array into the texture object: */
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,dataItem->textureObjectId);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_RGB,imageSize[0],imageSize[1],0,GL_RGB,GL_FLOAT,rgb);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,0);
}
void Image::load(const char* imageFileName)
{
/* Read the image file: */
CImg<float> image(imageFileName);
if (rotateImage)
image.rotate(180.0);
if (flipImageX)
image.mirror('x');
if (flipImageY)
image.mirror('y');
imageSize[0] = image.width();
imageSize[1] = image.height();
imageBox[0] = 0.0;
imageBox[1] = imageSize[1];
imageBox[2] = imageSize[0];
imageBox[3] = 0.0;
if (rgb != 0) delete[] rgb;
rgb=new float[imageSize[1]*imageSize[0]*3];
int ind = 0;
cimg_forY(image, y)
{
cimg_forX(image, x)
{
rgb[ind++]=image(x, y, 0)/255.0;
rgb[ind++]=image(x, y, 1)/255.0;
rgb[ind++]=image(x, y, 2)/255.0;
}
}
calcMatrix();
}
void Image::bindTexture(GLContextData& contextData) const
{
/* Get the context data item: */
DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this);
/* Bind the image texture: */
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,dataItem->textureObjectId);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_RGB,imageSize[0],imageSize[1],0,GL_RGB,GL_FLOAT,rgb);
}
void Image::setTransform(const OGTransform& newTransform)
{
transform=newTransform;
/* Update the image transformation: */
calcMatrix();
}
void Image::uploadImageTransform(GLint location) const
{
/* Upload the matrix to OpenGL: */
glUniformMatrix4fvARB(location,1,GL_FALSE,imageTransformMatrix);
}