-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestThumbnail.cpp
140 lines (111 loc) · 3.86 KB
/
testThumbnail.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
// ==========================================================
// FreeImage 3 Test Script
//
// Design and implementation by
// - Hervé Drolon ([email protected])
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
#include "TestSuite.h"
/**
Test thumbnail loading
*/
static FIBOOL testLoadThumbnail(const char *lpszPathName, int flags) {
FIBITMAP *dib = NULL;
try {
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
dib = FreeImage_Load(fif, lpszPathName, flags);
if(!dib) throw(1);
FIBITMAP *thumbnail = FreeImage_GetThumbnail(dib);
if(!thumbnail) throw(1);
unsigned t_width = FreeImage_GetWidth(thumbnail);
unsigned t_height = FreeImage_GetHeight(thumbnail);
printf("... %s contains a thumbnail whose size is %dx%d\n", lpszPathName, t_width, t_height);
FreeImage_Unload(dib);
return TRUE;
}
catch(int) {
if(dib) FreeImage_Unload(dib);
}
return FALSE;
}
/**
Test thumbnail saving
*/
static FIBOOL testSaveThumbnail(const char *lpszPathName, int flags) {
FIBOOL bResult = FALSE;
FIBITMAP *dib = NULL;
FIBITMAP *t_clone = NULL;
const char *lpszImagePathName = "exif_new_thumb.jpg";
try {
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
// load the dib
dib = FreeImage_Load(fif, lpszPathName, flags);
if(!dib) throw(1);
// get a link to the attached thumbnail
FIBITMAP *thumbnail = FreeImage_GetThumbnail(dib);
if(!thumbnail) throw(1);
// clone the thumbnail and modify it (e.g. convert to greyscale)
assert(FreeImage_GetBPP(thumbnail) == 24);
t_clone = FreeImage_ConvertTo8Bits(thumbnail);
if(!t_clone) throw(1);
// replace the thumbnail
FreeImage_SetThumbnail(dib, t_clone);
// no longer needed
FreeImage_Unload(t_clone);
t_clone = NULL;
// save as a new image
// be sure to delete the Exif segment as it can also contain a thumbnail
// this thumbnail will then be loaded instead of the one we store in the JFXX segment
fif = FIF_TIFF;
FreeImage_SetMetadata(FIMD_EXIF_RAW, dib, NULL, NULL);
bResult = FreeImage_Save(fif, dib, lpszImagePathName, 0);
assert(bResult);
// no longer needed
FreeImage_Unload(dib);
// reload the image and check its thumbnail
dib = FreeImage_Load(fif, lpszImagePathName, 0);
if(!dib) throw(1);
// get a link to the attached thumbnail
FIBITMAP *new_thumbnail = FreeImage_GetThumbnail(dib);
if(!new_thumbnail) throw(1);
// check that the thumbnail is greyscale
// note that with JPEG, we cannot compare pixels between new_thumbnail and t_clone
// because JPEG compression will modify the pixels
assert(FreeImage_GetBPP(new_thumbnail) == 8);
FreeImage_Unload(dib);
return TRUE;
}
catch(int) {
if(dib) FreeImage_Unload(dib);
if(t_clone) FreeImage_Unload(t_clone);
}
return FALSE;
}
/**
Test thumbnail functions
*/
void testThumbnail(const char *lpszPathName, int flags) {
FIBOOL bResult = FALSE;
printf("testThumbnail ...\n");
// Thumbnail loading
bResult = testLoadThumbnail(lpszPathName, flags);
assert(bResult);
#if FREEIMAGE_WITH_LIBTIFF
// Thumbnail saving
bResult = testSaveThumbnail(lpszPathName, flags);
assert(bResult);
#endif
}