File: testThumbnail.cpp

package info (click to toggle)
freeimage 3.18.0%2Bds2-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,824 kB
  • sloc: cpp: 42,339; cs: 36,178; pascal: 4,629; ansic: 1,294; makefile: 101; sh: 84
file content (139 lines) | stat: -rw-r--r-- 4,047 bytes parent folder | download | duplicates (5)
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 (drolon@infonie.fr)
//
// 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 BOOL 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 BOOL testSaveThumbnail(const char *lpszPathName, int flags) {
	BOOL 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) {
	BOOL bResult = FALSE;

	printf("testThumbnail ...\n");

	// Thumbnail loading
	bResult = testLoadThumbnail(lpszPathName, flags);
	assert(bResult);

	// Thumbnail saving
	bResult = testSaveThumbnail(lpszPathName, flags);
	assert(bResult);

}