/
tifInverter
128 lines (105 loc) · 4.63 KB
/
tifInverter
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
#include <stdio.h>
#include <iostream>
#include <tiffio.h>
/* Found this on a blog and it seemed useful for my particular problem, maybe I can use it somewhere?
#define TIFFSetR(pixel, x) ((unsigned char *)pixel)[0] = x
#define TIFFSetG(pixel, x) ((unsigned char *)pixel)[1] = x
#define TIFFSetB(pixel, x) ((unsigned char *)pixel)[2] = x
#define TIFFSetA(pixel, x) ((unsigned char *)pixel)[3] = x
*/
using namespace std;
int main()
{
//Get image path
const char * imagePath;
imagePath = "C:/images/002222-012515185003-Cam1.tif";
TIFF *tif = TIFFOpen(imagePath, "r");
//Get image width, height and other important info
uint32 width, height, samplesPerPixel, bitsPerSample, photometricSetting;
uint32* raster;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesPerPixel);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitsPerSample);
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometricSetting);
//Calculate total number of pixels
uint32 npixels = width*height;
raster = (uint32 *)_TIFFmalloc(npixels *sizeof(uint32));
//Read image data and store information into an array
//TIFFReadRGBAImage(tif, width, height, raster, 0); // Had this originally
TIFFReadRGBAImageOriented(tif, width, height, raster, ORIENTATION_BOTLEFT, 0);
uint32 e, c, red, green, blue, alpha;
red = 0, green = 0, blue = 0, alpha = 0;
//Add color values together for average calculation
for (e = height - 1; e != -1; e--)
{
for (c = 0; c < width; c++)
{
red = red + TIFFGetR(raster[c]);
green = green + TIFFGetG(raster[c]);
blue = blue + TIFFGetB(raster[c]);
alpha = TIFFGetA(raster[c]);
}
}
//Calculate image's color averages
uint32 redAvg, blueAvg, greenAvg;
redAvg = red / npixels;
blueAvg = blue / npixels;
greenAvg = green / npixels;
cout << "Image: " << imagePath << endl;
cout << "Average Red: " << redAvg << endl;
cout << "Average Blue: " << blueAvg << endl;
cout << "Average Green: " << greenAvg << endl;
cout << "Alpha: " << alpha << endl;
/* Haven't been able to complete the following code yet...
//Create complement image
TIFF* newTif = TIFFOpen("C:/images/002222-012515185003-Cam1(Complement).tif", "w");
uint32 newRed, newGreen, newBlue;
// Use same alpha values as original image.
uint32* newRaster;
newRaster = (uint32 *)_TIFFmalloc(npixels *sizeof(uint32));
char *image = new char [width*height*samplesPerPixel]; // Saw this in a tutorial but what is is used for?
//Set the image's tags
TIFFSetField(newTif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(newTif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(newTif, TIFFTAG_SAMPLESPERPIXEL, samplesPerPixel); // set number of channels per pixel
TIFFSetField(newTif, TIFFTAG_BITSPERSAMPLE, bitsPerSample); // set the size of the channels
TIFFSetField(newTif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT); // set the origin of the image.
TIFFSetField(newTif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(newTif, TIFFTAG_PHOTOMETRIC, photometricSetting); // Was originally set to 'PHOTOMETRIC_RGB' instead of 'photometricSetting'
//Invert color values and write to new image file
for (e = height - 1; e != -1; e--)
{
for (c = 0; c < width; c++)
{
red = TIFFGetR(raster[c]);
newRed = 255 - red;
green = TIFFGetG(raster[c]);
newGreen = 255 - green;
blue = TIFFGetB(raster[c]);
newBlue = 255 - blue;
// ...Then do what?
}
}
tsize_t linebytes = samplesPerPixel * width; // length in memory of one row of pixel in the image.
unsigned char *buffer = NULL // buffer used to store the row of pixel information for writing to file
// Allocating memory to store the pixels of current row
if (TIFFScanlineSize(newTif)linebytes)
buffer =(unsigned char *)_TIFFmalloc(linebytes);
else
buffer = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(newTif));
// We set the strip size of the file to be size of one row of pixels
TIFFSetField(newTif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(newTif, width*samplesPerPixel));
// Now writing image to the file one strip at a time
for (uint32 row = 0; row < height; row++)
{
memcpy(buffer, &image[(height - row - 1)*linebytes], linebytes);
if (TIFFWriteScanline(newTif, buffer, row, 0) < 0)
break;
}
//Free up the memory and close the image
_TIFFfree(raster);
TIFFClose(tif);
//TIFFClose(newTif);
return 0;
}