Imported Debian version 1.0~trusty
[deb_vid.stab.git] / tests / testutils.c
CommitLineData
80f575fc
DM
1#include <assert.h>
2
3#include "testutils.h"
4#include "libvidstab.h"
5#include "transformtype_operations.h"
6
7void paintRectangle(unsigned char* buffer, const VSFrameInfo* fi, int x, int y, int sizex, int sizey, unsigned char color){
8 if(x>=0 && x+sizex < fi->width && y>=0 && y+sizey < fi->height){
9 int i,j;
10 for(j=y; j < y+sizey; j++){
11 for(i=x; i<x+sizex; i++){
12 buffer[j*fi->width + i] = color;
13 }
14 }
15
16 }
17}
18
19/// corr: correlation length of noise
20void fillArrayWithNoise(unsigned char* buffer, int length, float corr){
21 unsigned char avg=randPixel();
22 int i=0;
23 if(corr<1) corr=1;
24 float alpha = 1.0/corr;
25 for(i=0; i < length; i++){
26 buffer[i] = avg;
27 avg = avg * (1.0-alpha) + randPixel()*alpha;
28 }
29}
30
31VSTransform getTestFrameTransform(int i){
32 VSTransform t = null_transform();
33 t.x = ( (i%2)==0 ? -1 : 1) *i*5;
34 t.y = ( (i%3)==0 ? 1 : -1) *i*5;
35 t.alpha = (i<3 ? 0 : 1) * (i)*1*M_PI/(180.0);
36 t.zoom = 0;
37 return t;
38}
39
40static int readNumber (const char* filename, FILE *f)
41{
42 int c,n=0;
43 for(;;) {
44 c = fgetc(f);
45 if (c==EOF)
46 vs_log_error("TEST", "unexpected end of file in '%s'", filename);
47 if (c >= '0' && c <= '9') n = n*10 + (c - '0');
48 else {
49 ungetc (c,f);
50 return n;
51 }
52 }
53}
54
55
56static void skipWhiteSpace (const char* filename, FILE *f)
57{
58 int c,d;
59 for(;;) {
60 c = fgetc(f);
61 if (c==EOF)
62 vs_log_error("TEST", "unexpected end of file in '%s'", filename);
63
64 // skip comments
65 if (c == '#') {
66 do {
67 d = fgetc(f);
68 if (d==EOF)
69 vs_log_error("TEST", "unexpected end of file in '%s'", filename);
70 } while (d != '\n');
71 continue;
72 }
73
74 if (c > ' ') {
75 ungetc (c,f);
76 return;
77 }
78 }
79}
80
81int loadPGMImage(const char* filename, VSFrame* frame, VSFrameInfo* fi)
82{
83 FILE *f = fopen (filename,"rb");
84 if (!f) {
85 vs_log_error("TEST", "Can't open image file '%s'", filename);
86 return 0;
87 }
88
89 // read in header
90 if (fgetc(f) != 'P' || fgetc(f) != '2')
91 vs_log_error("TEST","image file ist not binary PGM (no P5 header) '%s'", filename);
92 skipWhiteSpace (filename,f);
93
94 // read in image parameters
95 fi->width = readNumber (filename,f);
96 skipWhiteSpace (filename,f);
97 fi->height = readNumber (filename,f);
98 skipWhiteSpace (filename,f);
99 int max_value = readNumber (filename,f);
100
101 // check values
102 if (fi->width < 1 || fi->height < 1)
103 vs_log_error("TEST", "bad image file '%s'", filename);
104 if (max_value != 255)
105 vs_log_error("TEST", "image file '%s' must have color range 255", filename);
106
107 // read either nothing, LF (10), or CR,LF (13,10)
108 int c = fgetc(f);
109 if (c == 10) {
110 // LF
111 }
112 else if (c == 13) {
113 // CR
114 c = fgetc(f);
115 if (c != 10) ungetc (c,f);
116 }
117 else ungetc (c,f);
118
119
120 // read in rest of data
121 vsFrameAllocate(frame,fi);
122 if (fread( frame->data[0], fi->width*fi->height, 1, f) != 1){
123 vs_log_error("TEST", "Can't read data from image file '%s'", filename);
124 return 0;
125 }
126 fclose (f);
127 return 1;
128}
129
130
131int storePGMImage(const char* filename, const uint8_t* data, VSFrameInfo fi ) {
132 FILE *f = fopen (filename,"wb");
133 if (!f) {
134 vs_log_error("TEST", "Can't open image file '%s'", filename);
135 return 0;
136 }
137
138 // write header
139 fprintf(f,"P5\n");
140 fprintf(f,"# CREATOR test suite of vid.stab\n");
141 fprintf(f,"%i %i\n", fi.width, fi.height);
142 fprintf(f,"255\n");
143
144 // write data
145 if (fwrite( data, fi.width*fi.height, 1, f) != 1){
146 vs_log_error("TEST", "Can't write to image file '%s'", filename);
147 return 0;
148 }
149 fclose (f);
150 return 1;
151}
152