Welcome to mirror list, hosted at ThFree Co, Russian Federation.

qttorch.cpp « generic - github.com/torch/qttorch.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2968b0ff92713fdb1787756ef6b4d87ea974412a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/qttorch.cpp"
#else

static int qttorch_(qimage_fromtensor)(lua_State *L)
{
  THTensor *Tsrc = (THTensor*)luaT_checkudata(L,1,torch_Tensor);
  double scale = 255.0 / luaL_optnumber(L, 2, 1);
  long depth = 1;
  int ddim, wdim = 1, hdim = 0;

  if ( Tsrc->nDimension == 3)
  {
    ddim = 0;
    hdim = 1;
    wdim = 2;
    depth = Tsrc->size[ddim];
  }
  else if (Tsrc->nDimension != 2)
    luaL_error(L, "tensor must have 2 or 3 dimensions");
  if (depth != 1 && depth != 3 && depth != 4)
    luaL_error(L, "tensor first dimension must be 1, 3, or 4.");
  // create image
  if (Tsrc->size[wdim] >= INT_MAX || Tsrc->size[hdim] >= INT_MAX)
    luaL_error(L, "image is too large");
  int width = (int)(Tsrc->size[wdim]);
  int height = (int)(Tsrc->size[hdim]);
  QImage image(width, height, QImage::Format_ARGB32_Premultiplied);
  // fill image
  long sw = Tsrc->stride[wdim];
  long sh = Tsrc->stride[hdim];
  long sd = (depth > 1) ? Tsrc->stride[ddim] : 0;
  real *tdata = THTensor_(data)(Tsrc);
  for(int j=0; j<height; j++)
  {
    QRgb *ip = (QRgb*)image.scanLine(j);
    real *tp = tdata + sh * j;
    if (depth == 1)
    {
      for (int i=0; i<width; i++)
      {
        int g = (int)(tp[0] * scale) & 0xff;
        tp += sw;
        ip[i] = qRgb(g,g,g);
      }
    }
    else if (depth == 3)
    {
      for (int i=0; i<width; i++)
      {
        int r = (int)(tp[0] * scale) & 0xff;
        int g = (int)(tp[sd] * scale) & 0xff;
        int b = (int)(tp[sd+sd] * scale) & 0xff;
        tp += sw;
        ip[i] = qRgb(r,g,b);
      }
    }
    else if (depth == 4)
    {
      for (int i=0; i<width; i++)
      {
        int a = (int)(tp[sd+sd+sd] * scale) & 0xff;
        int r = (int)(tp[0] * scale) & 0xff;
        int g = (int)(tp[sd] * scale) & 0xff;
        int b = (int)(tp[sd+sd] * scale) & 0xff;
        tp += sw;
        ip[i] = qRgba(r,g,b,a);
      }
    }
  }
  // return
  luaQ_pushqt(L, image);
  return 1;
}

static int qttorch_(qimage_totensor)(lua_State *L)
{
  THTensor *Tdst = 0;
  QImage image = luaQ_checkqvariant<QImage>(L, 1);
  int width = image.width();
  int height = image.height();
  int depth = 1;
  int ddim, wdim = 1, hdim = 0;
  int tpos = 0;
  double scale = 255.0 / luaL_optnumber(L, 3, 1);

  // validate arguments
  if (lua_type(L, 2) == LUA_TUSERDATA)
  {
    tpos = 2;
    Tdst = (THTensor*)luaT_checkudata(L,2,torch_Tensor);
    if (Tdst->nDimension == 3)
    {
      ddim = 0;
      hdim = 1;
      wdim = 2;
      depth = Tdst->size[ddim];
    }
    else if (Tdst->nDimension != 2)
      luaL_error(L, "tensor must have 2 or 3 dimensions");
    if (depth != 1 && depth != 3 && depth != 4)
      luaL_error(L, "tensor third dimension must be 1, 3, or 4.");
    if (width != Tdst->size[wdim] || height != Tdst->size[hdim])
      luaL_error(L, "tensor dimensions must match the image size.");
  }
  else
  {
    depth = luaL_optinteger(L, 4, 3);
    if (depth != 1 && depth != 3 && depth != 4)
      luaL_error(L, "depth must be 1, 3, or 4.");
    if (depth == 1)
      Tdst = THTensor_(newWithSize2d)(height, width);
    else
    {
      ddim = 0;
      hdim = 1;
      wdim = 2;
      Tdst = THTensor_(newWithSize3d)(depth, height, width);
    }
  }

  // convert image
  if (image.format() != QImage::Format_ARGB32)
    image = image.convertToFormat(QImage::Format_ARGB32);
  if (image.format() != QImage::Format_ARGB32)
    luaL_error(L, "Cannot convert image to format ARGB32");

  // fill tensor
  long sw = Tdst->stride[wdim];
  long sh = Tdst->stride[hdim];
  long sd = (depth > 1) ? Tdst->stride[ddim] : 0;
  real *tdata = THTensor_(data)(Tdst);
  for(int j=0; j<height; j++) 
  {
    QRgb *ip = (QRgb*)image.scanLine(j);
    real *tp = tdata + sh * j;
    if (depth == 1)
    {
      for (int i=0; i<width; i++)
      {
        QRgb v = ip[i];
        tp[0] = (real)qGray(v) / scale;
        tp += sw;
      }
    }
    else if (depth == 3)
    {
      for (int i=0; i<width; i++)
      {
        QRgb v = ip[i];
        tp[0] = (real)qRed(v) / scale;
        tp[sd] = (real)qGreen(v) / scale;
        tp[sd+sd] = (real)qBlue(v) / scale;
        tp += sw;
      }
    }
    else if (depth == 4)
    {
      for (int i=0; i<width; i++)
      {
        QRgb v = ip[i];
        tp[0] = (real)qRed(v) / scale;
        tp[sd] = (real)qGreen(v) / scale;
        tp[sd+sd] = (real)qBlue(v) / scale;
        tp[sd+sd+sd] = (real)qAlpha(v) / scale;
        tp += sw;
      }
    }
  }
  // return
  if (tpos > 0)
    lua_pushvalue(L, tpos);
  else
    luaT_pushudata(L, (void*)Tdst, torch_Tensor);
  return 1;
}


static struct luaL_Reg qttorch_(qimage_lib)[] = {
  {"QImageFromTensor", qttorch_(qimage_fromtensor)},
  {"QImageToTensor", qttorch_(qimage_totensor)},
  {0,0}
};

static void qttorch_(Tensor_init)(lua_State *L)
{
  luaT_pushmetatable(L, torch_Tensor);
  luaT_registeratname(L, qttorch_(qimage_lib), "qttorch");
  lua_pop(L,1);
}

#endif