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