Update.
[flatland.git] / mylib.c
1
2 #include <TH/TH.h>
3
4 /*
5
6   Example of FFI extension I started from:
7
8     https://github.com/pytorch/extension-ffi.git
9
10   There is this tutorial
11
12     https://github.com/pytorch/tutorials/blob/master/Creating%20Extensions%20using%20FFI.md
13
14   And TH's Tensor definition are here in my install:
15
16     anaconda3/lib/python3.5/site-packages/torch/lib/include/TH/generic/THTensor.h
17
18  */
19
20 #include "flatland.h"
21
22 int generate_sequence(long nb_sequences, THByteTensor *output) {
23   long nb_images_per_sequence = 5;
24   long depth = 3;
25   long width = 64;
26   long height = 64;
27   long s;
28   unsigned char *a, *b;
29   int c, k, i, j, st0, st1, st2, st3, st4;
30
31   THByteTensor_resize5d(output, nb_sequences, nb_images_per_sequence, depth, height, width);
32
33   st0 = THByteTensor_stride(output, 0);
34   st1 = THByteTensor_stride(output, 1);
35   st2 = THByteTensor_stride(output, 2);
36   st3 = THByteTensor_stride(output, 3);
37   st4 = THByteTensor_stride(output, 4);
38
39   a =
40     THByteTensor_storage(output)->data + THByteTensor_storageOffset(output);
41
42   for(s = 0; s < nb_sequences; s++) {
43     unsigned char result[nb_images_per_sequence * depth * width * height];
44     unsigned char *r = result;
45     fl_generate_sequences(1, nb_images_per_sequence, width, height, result);
46     for(k = 0; k < nb_images_per_sequence; k++) {
47       for(c = 0; c < depth; c++) {
48         for(i = 0; i < height; i++) {
49           b = a
50             + s * st0 + k * st1 + c * st2 + i * st3;
51           for(j = 0; j < width; j++) {
52             *b = (unsigned char) (*r);
53             r++;
54             b += st4;
55           }
56         }
57       }
58     }
59   }
60
61   return 1;
62 }