ufo-lamino-ramp-task.c 9.5 KB

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