X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=svrt.c;h=4969d5aac43f22750088c309850e97433217a8cc;hb=HEAD;hp=0f53642e4b749a7af9ab2ac50351b1632cfe6aff;hpb=f542d0542b1e51ca7dd12bc6b96f6a299371ae8d;p=pysvrt.git
diff --git a/svrt.c b/svrt.c
index 0f53642..4969d5a 100644
--- a/svrt.c
+++ b/svrt.c
@@ -19,7 +19,7 @@
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with selector. If not, see .
+ * along with svrt. If not, see .
*
*/
@@ -27,23 +27,120 @@
#include "svrt_generator.h"
-THByteTensor *generate_vignettes(long n_problem, long nb_vignettes) {
+THByteStorage *compress(THByteStorage *x) {
+ long k, g, n;
+
+ k = 0; n = 0;
+ while(k < x->size) {
+ g = 0;
+ while(k < x->size && x->data[k] == 255 && g < 255) { g++; k++; }
+ n++;
+ if(k < x->size && g < 255) { k++; }
+ }
+
+ if(x->data[k-1] == 0) {
+ n++;
+ }
+
+ THByteStorage *result = THByteStorage_newWithSize(n);
+
+ k = 0; n = 0;
+ while(k < x->size) {
+ g = 0;
+ while(k < x->size && x->data[k] == 255 && g < 255) { g++; k++; }
+ result->data[n++] = g;
+ if(k < x->size && g < 255) { k++; }
+ }
+ if(x->data[k-1] == 0) {
+ result->data[n++] = 0;
+ }
+
+ return result;
+}
+
+THByteStorage *uncompress(THByteStorage *x) {
+ long k, g, n;
+
+ k = 0;
+ for(n = 0; n < x->size - 1; n++) {
+ k = k + x->data[n];
+ if(x->data[n] < 255) { k++; }
+ }
+ k = k + x->data[n];
+
+ THByteStorage *result = THByteStorage_newWithSize(k);
+
+ k = 0;
+ for(n = 0; n < x->size - 1; n++) {
+ for(g = 0; g < x->data[n]; g++) {
+ result->data[k++] = 255;
+ }
+ if(x->data[n] < 255) {
+ result->data[k++] = 0;
+ }
+ }
+ for(g = 0; g < x->data[n]; g++) {
+ result->data[k++] = 255;
+ }
+
+ return result;
+}
+
+void seed(long s) {
+ srand48(s);
+}
+
+THByteTensor *generate_vignettes(long n_problem, THLongTensor *labels) {
struct VignetteSet vs;
+ long nb_vignettes;
+ long st0, st1, st2;
+ long v, i, j;
+ long *m, *l;
+ unsigned char *a, *b;
+
+ if(THLongTensor_nDimension(labels) != 1) {
+ printf("Label tensor has to be of dimension 1.\n");
+ exit(1);
+ }
+
+ nb_vignettes = THLongTensor_size(labels, 0);
+ m = THLongTensor_storage(labels)->data + THLongTensor_storageOffset(labels);
+ st0 = THLongTensor_stride(labels, 0);
+ l = (long *) malloc(sizeof(long) * nb_vignettes);
+ for(v = 0; v < nb_vignettes; v++) {
+ l[v] = *m;
+ m += st0;
+ }
- svrt_generate_vignettes(n_problem, nb_vignettes, &vs);
- printf("SANITY %d %d %d\n", vs.nb_vignettes, vs.width, vs.height);
+ svrt_generate_vignettes(n_problem, nb_vignettes, l, &vs);
+ free(l);
THLongStorage *size = THLongStorage_newWithSize(3);
- size->data[0] = nb_vignettes;
+ size->data[0] = vs.nb_vignettes;
size->data[1] = vs.height;
size->data[2] = vs.width;
THByteTensor *result = THByteTensor_newWithSize(size, NULL);
THLongStorage_free(size);
- /* st0 = THByteTensor_stride(result, 0); */
- /* st1 = THByteTensor_stride(result, 1); */
- /* st2 = THByteTensor_stride(result, 2); */
+ st0 = THByteTensor_stride(result, 0);
+ st1 = THByteTensor_stride(result, 1);
+ st2 = THByteTensor_stride(result, 2);
+
+ unsigned char *r = vs.data;
+ for(v = 0; v < vs.nb_vignettes; v++) {
+ a = THByteTensor_storage(result)->data + THByteTensor_storageOffset(result) + v * st0;
+ for(i = 0; i < vs.height; i++) {
+ b = a + i * st1;
+ for(j = 0; j < vs.width; j++) {
+ *b = (unsigned char) (*r);
+ r++;
+ b += st2;
+ }
+ }
+ }
+
+ free(vs.data);
return result;
}