-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureLoader.cpp
139 lines (120 loc) · 5.39 KB
/
TextureLoader.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
/*!
* OpenGL 3.2 Utils - Extension to the Angel library (from the book Interactive Computer Graphics 6th ed
* https://github.com/mortennobel/OpenGL_3_2_Utils
*
* New BSD License
*
* Copyright (c) 2011, Morten Nobel-Joergensen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TextureLoader.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
unsigned char * loadBMPRaw(const char * imagepath, unsigned int& outWidth, unsigned int& outHeight, bool flipY){
printf("Reading image %s\n", imagepath);
outWidth = -1;
outHeight = -1;
// Data read from the header of the BMP file
unsigned char header[54];
unsigned int dataPos;
unsigned int imageSize;
// Actual RGB data
unsigned char * data;
// Open the file
FILE * file = fopen(imagepath,"rb");
if (!file) {printf("Image could not be opened\n"); return NULL;}
// Read the header, i.e. the 54 first bytes
// If less than 54 byes are read, problem
if ( fread(header, 1, 54, file)!=54 ){
printf("Not a correct BMP file\n");
return NULL;
}
// A BMP files always begins with "BM"
if ( header[0]!='B' || header[1]!='M' ){
printf("Not a correct BMP file\n");
return NULL;
}
// Make sure this is a 24bpp file
if ( *(int*)&(header[0x1E])!=0 ) {printf("Not a correct BMP file\n"); return NULL;}
if ( *(int*)&(header[0x1C])!=24 ) {printf("Not a correct BMP file\n"); return NULL;}
// Read the information about the image
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
outWidth = *(int*)&(header[0x12]);
outHeight = *(int*)&(header[0x16]);
// Some BMP files are misformatted, guess missing information
if (imageSize==0) imageSize=outWidth*outHeight*3; // 3 : one byte for each Red, Green and Blue component
if (dataPos==0) dataPos=54; // The BMP header is done that way
// Create a buffer
data = new unsigned char [imageSize];
// Read the actual data from the file into the buffer
fread(data,1,imageSize,file);
// Everything is in memory now, the file wan be closed
fclose (file);
if (flipY){
// swap y-axis
unsigned char * tmpBuffer = new unsigned char[outWidth*3];
int size = outWidth*3;
for (int i=0;i<outHeight/2;i++){
// copy row i to tmp
//memcpy_s(tmpBuffer,size,data+outWidth*3*i,size);
memcpy(tmpBuffer,data+outWidth*3*i,size);
// copy row h-i-1 to i
//memcpy_s(data+outWidth*3*i, size, data+outWidth*3*(outHeight-i-1), size);
memcpy(data+outWidth*3*i, data+outWidth*3*(outHeight-i-1), size);
// copy tmp to row h-i-1
//memcpy_s(data+outWidth*3*(outHeight-i-1), size,tmpBuffer, size);
memcpy(data+outWidth*3*(outHeight-i-1),tmpBuffer, size);
}
delete [] tmpBuffer;
}
return data;
}
//return red channel for pixel (i,j) of image data, where (i,j) vary between (0,0) and (width,height)
unsigned char getTextureR(int i, int j, unsigned char * data, int width, int height)
{
return data[j*width*3+i*3+2];
}
//return green channel for pixel (i,j) of image data, where (i,j) vary between (0,0) and (width,height)
unsigned char getTextureG(int i, int j, unsigned char * data, int width, int height)
{
return data[j*width*3+i*3+1];
}
//return blue channel for pixel (i,j) of image data, where (i,j) vary between (0,0) and (width,height)
unsigned char getTextureB(int i, int j, unsigned char * data, int width, int height)
{
return data[j*width*3+i*3];
}
//set red channel for pixel (i,j) of image data to val, where (i,j) vary between (0,0) and (width,height) and val varies between 0 and 255
void setTextureR(int i, int j, unsigned char * data, int width, int height, unsigned char val)
{
data[j*width*3+i*3+2] = val;
}
//set green channel for pixel (i,j) of image data to val, where (i,j) vary between (0,0) and (width,height) and val varies between 0 and 255
void setTextureG(int i, int j, unsigned char * data, int width, int height, unsigned char val)
{
data[j*width*3+i*3+1] = val;
}
//set blue channel for pixel (i,j) of image data to val, where (i,j) vary between (0,0) and (width,height) and val varies between 0 and 255
void setTextureB(int i, int j, unsigned char * data, int width, int height, unsigned char val)
{
data[j*width*3+i*3] = val;
}