3 * svrt is the ``Synthetic Visual Reasoning Test'', an image
4 * generator for evaluating classification performance of machine
5 * learning systems, humans and primates.
7 * Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/
8 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
10 * This file is part of svrt.
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.
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.
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/>.
28 #include "svrt_generator.h"
30 THByteStorage *compress(THByteStorage *x) {
36 while(k < x->size && x->data[k] == 255 && g < 255) { g++; k++; }
38 if(k < x->size && g < 255) { k++; }
41 if(x->data[k-1] == 0) {
45 THByteStorage *result = THByteStorage_newWithSize(n);
50 while(k < x->size && x->data[k] == 255 && g < 255) { g++; k++; }
51 result->data[n++] = g;
52 if(k < x->size && g < 255) { k++; }
54 if(x->data[k-1] == 0) {
55 result->data[n++] = 0;
61 THByteStorage *uncompress(THByteStorage *x) {
65 for(n = 0; n < x->size - 1; n++) {
67 if(x->data[n] < 255) { k++; }
71 THByteStorage *result = THByteStorage_newWithSize(k);
74 for(n = 0; n < x->size - 1; n++) {
75 for(g = 0; g < x->data[n]; g++) {
76 result->data[k++] = 255;
78 if(x->data[n] < 255) {
79 result->data[k++] = 0;
82 for(g = 0; g < x->data[n]; g++) {
83 result->data[k++] = 255;
89 THByteTensor *generate_vignettes(long n_problem, THLongTensor *labels) {
90 struct VignetteSet vs;
97 if(THLongTensor_nDimension(labels) != 1) {
98 printf("Label tensor has to be of dimension 1.\n");
102 nb_vignettes = THLongTensor_size(labels, 0);
103 m = THLongTensor_storage(labels)->data + THLongTensor_storageOffset(labels);
104 st0 = THLongTensor_stride(labels, 0);
105 l = (long *) malloc(sizeof(long) * nb_vignettes);
106 for(v = 0; v < nb_vignettes; v++) {
111 svrt_generate_vignettes(n_problem, nb_vignettes, l, &vs);
114 THLongStorage *size = THLongStorage_newWithSize(3);
115 size->data[0] = vs.nb_vignettes;
116 size->data[1] = vs.height;
117 size->data[2] = vs.width;
119 THByteTensor *result = THByteTensor_newWithSize(size, NULL);
120 THLongStorage_free(size);
122 st0 = THByteTensor_stride(result, 0);
123 st1 = THByteTensor_stride(result, 1);
124 st2 = THByteTensor_stride(result, 2);
126 unsigned char *r = vs.data;
127 for(v = 0; v < vs.nb_vignettes; v++) {
128 a = THByteTensor_storage(result)->data + THByteTensor_storageOffset(result) + v * st0;
129 for(i = 0; i < vs.height; i++) {
131 for(j = 0; j < vs.width; j++) {
132 *b = (unsigned char) (*r);