Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
111 lines (91 loc) · 2.81 KB

File metadata and controls

111 lines (91 loc) · 2.81 KB
 
1
2
3
4
5
6
7
8
9
10
/**
* Automated SDL_Surface test.
*
* Written by Edgar Simo "bobbens"
*
* Released under Public Domain.
*/
#include "SDL.h"
Nov 22, 2009
Nov 22, 2009
11
#include "../SDL_at.h"
Nov 22, 2009
Nov 22, 2009
13
#include "common.h"
14
15
16
17
18
/**
* @brief Compares a surface and a surface image for equality.
*/
Nov 21, 2009
Nov 21, 2009
19
int surface_compare( SDL_Surface *sur, const SurfaceImage_t *img, int allowable_error )
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
{
int ret;
int i,j;
int bpp;
Uint8 *p, *pd;
/* Make sure size is the same. */
if ((sur->w != img->width) || (sur->h != img->height))
return -1;
SDL_LockSurface( sur );
ret = 0;
bpp = sur->format->BytesPerPixel;
/* Compare image - should be same format. */
for (j=0; j<sur->h; j++) {
for (i=0; i<sur->w; i++) {
p = (Uint8 *)sur->pixels + j * sur->pitch + i * bpp;
pd = (Uint8 *)img->pixel_data + (j*img->width + i) * img->bytes_per_pixel;
switch (bpp) {
case 1:
case 2:
case 3:
ret += 1;
Nov 22, 2009
Nov 22, 2009
45
/*printf("%d BPP not supported yet.\n",bpp);*/
46
47
48
break;
case 4:
Nov 17, 2009
Nov 17, 2009
49
{
Nov 19, 2009
Nov 19, 2009
50
int dist = 0;
Nov 17, 2009
Nov 17, 2009
51
52
53
54
55
Uint8 R, G, B, A;
SDL_GetRGBA(*(Uint32*)p, sur->format, &R, &G, &B, &A);
if (img->bytes_per_pixel == 3) {
Nov 19, 2009
Nov 19, 2009
56
57
58
dist += (R-pd[0])*(R-pd[0]);
dist += (G-pd[1])*(G-pd[1]);
dist += (B-pd[2])*(B-pd[2]);
Nov 17, 2009
Nov 17, 2009
59
} else {
Nov 19, 2009
Nov 19, 2009
60
61
62
63
dist += (R-pd[0])*(R-pd[0]);
dist += (G-pd[1])*(G-pd[1]);
dist += (B-pd[2])*(B-pd[2]);
dist += (A-pd[3])*(A-pd[3]);
Nov 17, 2009
Nov 17, 2009
64
}
Nov 21, 2009
Nov 21, 2009
65
66
/* Allow some difference in blending accuracy */
if (dist > allowable_error) {
Nov 19, 2009
Nov 19, 2009
67
/*printf("pixel %d,%d varies by %d\n", i, j, dist);*/
Nov 17, 2009
Nov 17, 2009
68
69
70
++ret;
}
}
71
72
73
74
75
76
77
break;
}
}
}
SDL_UnlockSurface( sur );
Nov 17, 2009
Nov 17, 2009
78
79
80
81
82
83
84
85
86
87
88
if (ret) {
SDL_SaveBMP(sur, "fail.bmp");
SDL_LockSurface( sur );
bpp = sur->format->BytesPerPixel;
/* Compare image - should be same format. */
if (bpp == 4) {
for (j=0; j<sur->h; j++) {
for (i=0; i<sur->w; i++) {
Nov 22, 2009
Nov 22, 2009
89
Uint8 R, G, B, A;
Nov 17, 2009
Nov 17, 2009
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
p = (Uint8 *)sur->pixels + j * sur->pitch + i * bpp;
pd = (Uint8 *)img->pixel_data + (j*img->width + i) * img->bytes_per_pixel;
R = pd[0];
G = pd[1];
B = pd[2];
if (img->bytes_per_pixel == 4) {
A = pd[3];
} else {
A = 0;
}
*(Uint32*)p = (A << 24) | (R << 16) | (G << 8) | B;
}
}
}
SDL_UnlockSurface( sur );
SDL_SaveBMP(sur, "good.bmp");
}
110
111
return ret;
}