ufo-lamino-ramp-task.c 9.6 KB

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