-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiff2bmpsw.c
106 lines (97 loc) · 2.78 KB
/
tiff2bmpsw.c
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
#include <stdint.h>
#include <stdlib.h>
#include <tiffio.h>
int writeBmp(char* data, int length);
#pragma pack(1)
typedef struct tagBITMAPFILEHEADER {
uint16_t bfType;
uint32_t bfSize;
uint16_t bfReserved1;
uint16_t bfReserved2;
uint32_t bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
uint32_t biSize;
int32_t biWidth;
int32_t biHeight;
uint16_t biPlanes;
uint16_t biBitCount;
uint32_t biCompression;
uint32_t biSizeImage;
int32_t biXPixPerMeter;
int32_t biYPixPerMeter;
uint32_t biClrUsed;
uint32_t biClrImporant;
} BITMAPINFOHEADER;
void write_bmp(char* buf, int width, int height)
{
struct tagBITMAPFILEHEADER fileheader;
fileheader.bfType = 0x4D42;
fileheader.bfSize = sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) +
width * height * 4;
fileheader.bfReserved1 = 0;
fileheader.bfReserved2 = 0;
fileheader.bfOffBits = sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER);
writeBmp((char*)&fileheader, sizeof(BITMAPFILEHEADER));
struct tagBITMAPINFOHEADER infoheader;
infoheader.biSize = sizeof(BITMAPINFOHEADER);
infoheader.biWidth = width;
infoheader.biHeight = height;
infoheader.biPlanes = 1;
infoheader.biBitCount = 32;
infoheader.biCompression = 0;
infoheader.biSizeImage = 0;
infoheader.biXPixPerMeter = 0;
infoheader.biYPixPerMeter = 0;
infoheader.biClrUsed = 0;
infoheader.biClrImporant = 0;
writeBmp((char*)&infoheader, sizeof(BITMAPINFOHEADER));
// Swap Red and Blue.
int bufsize = width * height * 4;
for (int i = 0; i < bufsize; i += 4) {
char tmp = buf[i + 2];
buf[i + 2] = buf[i];
buf[i] = tmp;
}
writeBmp(buf, bufsize);
}
int tiff_to_bmp(char* filename) {
TIFF *image;
uint32 width, height;
unsigned long imageOffset, result;
int stripMax, stripCount;
char *buffer, tempbyte;
unsigned long bufferSize, count;
image = TIFFOpen(filename, "r");
if(!image) {
fprintf(stderr, "File open failure.\n");
return -1;
}
if(!TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width)) {
fprintf(stderr, "Failed to get width.\n");
TIFFClose(image);
return -1;
}
if(!TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height)) {
fprintf(stderr, "Failed to get height.\n");
TIFFClose(image);
return -1;
}
char *row_pointers = malloc(width * height * 4);
if(!TIFFReadRGBAImageOriented(image, width, height,
(uint32*)row_pointers,
ORIENTATION_BOTLEFT,
1)){
fprintf(stderr, "TIFFReadRGBAImageOriented failed.\n");
free(row_pointers);
TIFFClose(image);
return -1;
}
printf("read done\n");
write_bmp(row_pointers, width, height);
free(row_pointers);
TIFFClose(image);
return 0;
}