ufo-lamino-ramp-task.c 9.6 KB

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