Update.
[pysvrt.git] / svrt.c
1
2 /*
3  *  svrt is the ``Synthetic Visual Reasoning Test'', an image
4  *  generator for evaluating classification performance of machine
5  *  learning systems, humans and primates.
6  *
7  *  Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/
8  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
9  *
10  *  This file is part of svrt.
11  *
12  *  svrt is free software: you can redistribute it and/or modify it
13  *  under the terms of the GNU General Public License version 3 as
14  *  published by the Free Software Foundation.
15  *
16  *  svrt is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with selector.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25
26 #include <TH/TH.h>
27
28 #include "svrt_generator.h"
29
30 THByteTensor *generate_vignettes(long n_problem, THLongTensor *labels) {
31   struct VignetteSet vs;
32   long nb_vignettes;
33   long st0, st1, st2;
34   long v, i, j;
35   long *m, *l;
36   unsigned char *a, *b;
37
38   if(THLongTensor_nDimension(labels) != 1) {
39     printf("Label tensor has to be of dimension 1.\n");
40     exit(1);
41   }
42
43   nb_vignettes = THLongTensor_size(labels, 0);
44   m = THLongTensor_storage(labels)->data + THLongTensor_storageOffset(labels);
45   st0 = THLongTensor_stride(labels, 0);
46   l = (long *) malloc(sizeof(long) * nb_vignettes);
47   for(v = 0; v < nb_vignettes; v++) {
48     l[v] = *m;
49     m += st0;
50   }
51
52   svrt_generate_vignettes(n_problem, nb_vignettes, l, &vs);
53   free(l);
54
55   THLongStorage *size = THLongStorage_newWithSize(3);
56   size->data[0] = vs.nb_vignettes;
57   size->data[1] = vs.height;
58   size->data[2] = vs.width;
59
60   THByteTensor *result = THByteTensor_newWithSize(size, NULL);
61   THLongStorage_free(size);
62
63   st0 = THByteTensor_stride(result, 0);
64   st1 = THByteTensor_stride(result, 1);
65   st2 = THByteTensor_stride(result, 2);
66
67   unsigned char *r = vs.data;
68   for(v = 0; v < vs.nb_vignettes; v++) {
69     a = THByteTensor_storage(result)->data + THByteTensor_storageOffset(result) + v * st0;
70     for(i = 0; i < vs.height; i++) {
71       b = a + i * st1;
72       for(j = 0; j < vs.width; j++) {
73         *b = (unsigned char) (*r);
74         r++;
75         b += st2;
76       }
77     }
78   }
79
80   free(vs.data);
81
82   return result;
83 }