ufo-lamino-ramp-task.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * SECTION:ufo-reader-task
  3. * @Short_description: Read TIFF and EDF files
  4. * @Title: reader
  5. *
  6. * The reader node loads single files from disk and provides them as a stream
  7. * The nominal resolution can be decreased by specifying the #UfoLaminoRampTask:x
  8. * and #UfoLaminoRampTask:y coordinates, and the #UfoLaminoRampTask:width and
  9. * #UfoLaminoRampTask:height of a region of interest.
  10. */
  11. #include <gmodule.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <glob.h>
  15. #ifdef __APPLE__
  16. #include <OpenCL/cl.h>
  17. #else
  18. #include <CL/cl.h>
  19. #endif
  20. #include "ufo-lamino-ramp-task.h"
  21. struct _UfoLaminoRampTaskPrivate {
  22. guint width;
  23. guint height;
  24. guint fill_width;
  25. gfloat theta;
  26. gfloat tau;
  27. cl_kernel kernel;
  28. gboolean done;
  29. };
  30. static void ufo_task_interface_init (UfoTaskIface *iface);
  31. static void ufo_gpu_task_interface_init (UfoGpuTaskIface *iface);
  32. G_DEFINE_TYPE_WITH_CODE (UfoLaminoRampTask, ufo_lamino_ramp_task, UFO_TYPE_TASK_NODE,
  33. G_IMPLEMENT_INTERFACE (UFO_TYPE_TASK,
  34. ufo_task_interface_init)
  35. G_IMPLEMENT_INTERFACE (UFO_TYPE_GPU_TASK,
  36. ufo_gpu_task_interface_init))
  37. #define UFO_LAMINO_RAMP_TASK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_LAMINO_RAMP_TASK, UfoLaminoRampTaskPrivate))
  38. enum {
  39. PROP_0,
  40. PROP_WIDTH,
  41. PROP_FILL_WIDTH,
  42. PROP_HEIGHT,
  43. PROP_THETA,
  44. PROP_TAU,
  45. N_PROPERTIES
  46. };
  47. static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  48. UfoNode *
  49. ufo_lamino_ramp_task_new (void)
  50. {
  51. return UFO_NODE (g_object_new (UFO_TYPE_LAMINO_RAMP_TASK, NULL));
  52. }
  53. static int
  54. is_power_of_two (guint x)
  55. {
  56. return ((x != 0) && !(x & (x - 1)));
  57. }
  58. static void
  59. ufo_lamino_ramp_task_setup (UfoTask *task,
  60. UfoResources *resources,
  61. GError **error)
  62. {
  63. UfoLaminoRampTask *node;
  64. UfoLaminoRampTaskPrivate *priv;
  65. node = UFO_LAMINO_RAMP_TASK (task);
  66. priv = node->priv;
  67. if (!is_power_of_two (priv->width)) {
  68. g_set_error (error, UFO_TASK_ERROR, UFO_TASK_ERROR_SETUP,
  69. "Filter width `%i' is not a power of two", priv->width);
  70. return;
  71. }
  72. if (!is_power_of_two (priv->height)) {
  73. g_set_error (error, UFO_TASK_ERROR, UFO_TASK_ERROR_SETUP,
  74. "Filter height `%i' is not a power of two", priv->height);
  75. return;
  76. }
  77. priv->kernel = ufo_resources_get_kernel (resources,
  78. "lamino_ramp.cl",
  79. "lamino_ramp_create_filter",
  80. error);
  81. if (priv->kernel != NULL)
  82. clRetainKernel (priv->kernel);
  83. }
  84. static void
  85. ufo_lamino_ramp_task_get_requisition (UfoTask *task,
  86. UfoBuffer **inputs,
  87. UfoRequisition *requisition)
  88. {
  89. UfoLaminoRampTaskPrivate *priv;
  90. priv = UFO_LAMINO_RAMP_TASK_GET_PRIVATE (UFO_LAMINO_RAMP_TASK (task));
  91. requisition->n_dims = 2;
  92. requisition->dims[0] = priv->width;
  93. requisition->dims[1] = priv->height;
  94. }
  95. static void
  96. ufo_lamino_ramp_task_get_structure (UfoTask *task,
  97. guint *n_inputs,
  98. UfoInputParam **in_params,
  99. UfoTaskMode *mode)
  100. {
  101. *n_inputs = 0;
  102. *mode = UFO_TASK_MODE_GENERATOR;
  103. }
  104. static gboolean
  105. ufo_lamino_ramp_task_generate (UfoGpuTask *task,
  106. UfoBuffer *output,
  107. UfoRequisition *requisition)
  108. {
  109. UfoLaminoRampTaskPrivate *priv;
  110. UfoGpuNode *node;
  111. cl_command_queue cmd_queue;
  112. cl_mem out_mem;
  113. priv = UFO_LAMINO_RAMP_TASK_GET_PRIVATE (UFO_LAMINO_RAMP_TASK (task));
  114. if (priv->done)
  115. return FALSE;
  116. node = UFO_GPU_NODE (ufo_task_node_get_proc_node (UFO_TASK_NODE (task)));
  117. cmd_queue = ufo_gpu_node_get_cmd_queue (node);
  118. out_mem = ufo_buffer_get_device_array (output, cmd_queue);
  119. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 0, sizeof(cl_mem), (void *) &out_mem));
  120. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 1, sizeof(int), &priv->width));
  121. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 2, sizeof(int), &priv->fill_width));
  122. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 3, sizeof(int), &priv->height));
  123. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 4, sizeof(float), &priv->theta));
  124. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (priv->kernel, 5, sizeof(float), &priv->tau));
  125. UFO_RESOURCES_CHECK_CLERR (clEnqueueNDRangeKernel (cmd_queue, priv->kernel,
  126. 2, NULL, requisition->dims, NULL,
  127. 0, NULL, NULL));
  128. priv->done = TRUE;
  129. return TRUE;
  130. }
  131. static void
  132. ufo_lamino_ramp_task_set_property (GObject *object,
  133. guint property_id,
  134. const GValue *value,
  135. GParamSpec *pspec)
  136. {
  137. UfoLaminoRampTaskPrivate *priv = UFO_LAMINO_RAMP_TASK_GET_PRIVATE (object);
  138. switch (property_id) {
  139. case PROP_WIDTH:
  140. priv->width = g_value_get_uint (value);
  141. break;
  142. case PROP_FILL_WIDTH:
  143. priv->fill_width = g_value_get_uint (value);
  144. break;
  145. case PROP_HEIGHT:
  146. priv->height = g_value_get_uint (value);
  147. break;
  148. case PROP_THETA:
  149. priv->theta = (gfloat) g_value_get_double (value);
  150. break;
  151. case PROP_TAU:
  152. priv->tau = (gfloat) g_value_get_double (value);
  153. break;
  154. default:
  155. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  156. break;
  157. }
  158. }
  159. static void
  160. ufo_lamino_ramp_task_get_property (GObject *object,
  161. guint property_id,
  162. GValue *value,
  163. GParamSpec *pspec)
  164. {
  165. UfoLaminoRampTaskPrivate *priv = UFO_LAMINO_RAMP_TASK_GET_PRIVATE (object);
  166. switch (property_id) {
  167. case PROP_WIDTH:
  168. g_value_set_uint (value, priv->width);
  169. break;
  170. case PROP_FILL_WIDTH:
  171. g_value_set_uint (value, priv->fill_width);
  172. break;
  173. case PROP_HEIGHT:
  174. g_value_set_uint (value, priv->height);
  175. break;
  176. case PROP_THETA:
  177. g_value_set_double (value, priv->theta);
  178. break;
  179. case PROP_TAU:
  180. g_value_set_double (value, priv->tau);
  181. break;
  182. default:
  183. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  184. break;
  185. }
  186. }
  187. static void
  188. ufo_lamino_ramp_task_finalize (GObject *object)
  189. {
  190. UfoLaminoRampTaskPrivate *priv = UFO_LAMINO_RAMP_TASK_GET_PRIVATE (object);
  191. if (priv->kernel != NULL) {
  192. clReleaseKernel (priv->kernel);
  193. priv->kernel = NULL;
  194. }
  195. G_OBJECT_CLASS (ufo_lamino_ramp_task_parent_class)->finalize (object);
  196. }
  197. static void
  198. ufo_task_interface_init (UfoTaskIface *iface)
  199. {
  200. iface->setup = ufo_lamino_ramp_task_setup;
  201. iface->get_structure = ufo_lamino_ramp_task_get_structure;
  202. iface->get_requisition = ufo_lamino_ramp_task_get_requisition;
  203. }
  204. static void
  205. ufo_gpu_task_interface_init (UfoGpuTaskIface *iface)
  206. {
  207. iface->generate = ufo_lamino_ramp_task_generate;
  208. }
  209. static void
  210. ufo_lamino_ramp_task_class_init (UfoLaminoRampTaskClass *klass)
  211. {
  212. GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  213. gobject_class->set_property = ufo_lamino_ramp_task_set_property;
  214. gobject_class->get_property = ufo_lamino_ramp_task_get_property;
  215. gobject_class->finalize = ufo_lamino_ramp_task_finalize;
  216. properties[PROP_WIDTH] =
  217. g_param_spec_uint("width",
  218. "Width of the 2D image filter (power of 2)",
  219. "Width of the 2D image filter (power of 2)",
  220. 1, 32768, 1.0,
  221. G_PARAM_READWRITE);
  222. properties[PROP_FILL_WIDTH] =
  223. g_param_spec_uint("fwidth",
  224. "Filling width of the 2D image filter",
  225. "Filling width of the 2D image filter",
  226. 1, 32768, 1.0,
  227. G_PARAM_READWRITE);
  228. properties[PROP_HEIGHT] =
  229. g_param_spec_uint("height",
  230. "Height of the 2D image filter",
  231. "Height of the 2D image filter",
  232. 1, 16384, 1.0,
  233. G_PARAM_READWRITE);
  234. properties[PROP_THETA] =
  235. g_param_spec_double("theta",
  236. "Laminographic angle in radians",
  237. "Resolution (pixel size) in microns",
  238. -4.0 * G_PI, +4.0 * G_PI, 0.0,
  239. G_PARAM_READWRITE);
  240. properties[PROP_TAU] =
  241. g_param_spec_double("tau",
  242. "Resolution (pixel size) in microns",
  243. "Resolution (pixel size) in microns",
  244. 0.0, /* minimum */
  245. 100000.0, /* maximum */
  246. 10.0, /* default */
  247. G_PARAM_READWRITE);
  248. for (guint i = PROP_0 + 1; i < N_PROPERTIES; i++)
  249. g_object_class_install_property (gobject_class, i, properties[i]);
  250. g_type_class_add_private (gobject_class, sizeof(UfoLaminoRampTaskPrivate));
  251. }
  252. static void
  253. ufo_lamino_ramp_task_init(UfoLaminoRampTask *self)
  254. {
  255. UfoLaminoRampTaskPrivate *priv = NULL;
  256. self->priv = priv = UFO_LAMINO_RAMP_TASK_GET_PRIVATE (self);
  257. priv->width = 4;
  258. priv->fill_width=2;
  259. priv->height = 1;
  260. priv->theta = 0.0;
  261. priv->tau = 10.0;
  262. priv->kernel = NULL;
  263. priv->done = FALSE;
  264. }