File: itkOpenCLImage.h

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (293 lines) | stat: -rw-r--r-- 11,170 bytes parent folder | download
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkOpenCLImage_h
#define itkOpenCLImage_h

#include "itkOpenCLMemoryObject.h"
#include "itkOpenCLImageFormat.h"
#include "itkOpenCLEvent.h"
#include "itkOpenCLSize.h"

namespace itk
{
/** \class OpenCLImage
 * \brief The OpenCLImage class represents an image object is used to store a
 * one, two or three dimensional texture, frame-buffer or image.
 *
 * \ingroup OpenCL
 */

// Forward declaration
class OpenCLBuffer;

class ITKOpenCL_EXPORT OpenCLImage : public OpenCLMemoryObject
{
public:
  /** Standard class typedefs. */
  using Self = OpenCLImage;
  using Superclass = OpenCLMemoryObject;

  /** Constructs a null OpenCL image object. */
  OpenCLImage() = default;

  /** Constructs a OpenCL image object that is initialized with the
   * native OpenCL identifier \a id, and associates it with \a context.
   * This class will take over ownership of \a id and will release
   * it in the destructor. */
  OpenCLImage(OpenCLContext * context, const cl_mem id)
    : OpenCLMemoryObject(context, id)
  {}

  /** Constructs a copy of \a other. */
  OpenCLImage(const OpenCLImage & other);

  /** Assigns \a other to this object. */
  OpenCLImage &
  operator=(const OpenCLImage & other);

  /** Return image format descriptor specified when image is created. */
  OpenCLImageFormat
  GetFormat() const;

  /** Return size of each element of the image memory object given by image.
   * An element is made up of n channels. The value of n is given in
   * cl_image_format descriptor. */
  std::size_t
  GetElementSizeInBytes() const;

  /** Return size in bytes of a row of elements of the image object given by image. */
  std::size_t
  GetRowSizeInBytes() const;

  /** Return calculated slice pitch in bytes of a 2D slice for the 3D image object
   * or size of each image in a 1D or 2D image array given by image.
   * \note For a 1D image, 1D image buffer and 2D image object return 0. */
  std::size_t
  GetSliceSizeInBytes() const;

  /** Returns the dimension for this image, 1, 2, or 3. */
  std::size_t
  GetDimension() const;

  /** Return width of image in pixels.
   *\sa GetHeight(), GetDepth() */
  std::size_t
  GetWidth() const;

  /** Return height of image in pixels.
   * \note For a 1D image, 1D image buffer and 1D image array object, height = 0.
   * \sa GetWidth(), GetDepth() */
  std::size_t
  GetHeight() const;

  /** Return depth of the image in pixels.
   * \note For a 1D image, 1D image buffer, 2D image or 1D and
   * 2D image array object, depth = 0
   * \sa GetWidth(), GetHeight() */
  std::size_t
  GetDepth() const;

  /** Reads from an image or image array object to host memory,
   * starting at \a origin and range \a region into \a data.
   * Returns true if the read was successful, false otherwise.
   * This method does not return until the buffer data has been read into memory
   * pointed to \a data.
   * \sa ReadAsync(), Write(), WriteAsync() */
  bool
  Read(void *             data,
       const OpenCLSize & origin,
       const OpenCLSize & region,
       const std::size_t  rowPitch = 0,
       const std::size_t  slicePitch = 0);

  /** Asynchronous version of the Read() method.
   * This function will queue the request and return immediately. Returns an
   * OpenCLEvent object that can be used to wait for the request to finish.
   * The request will not start until all of the events in \a event_list
   * have been signaled as completed.
   * \sa Read(), Write(), WriteAsync() */
  OpenCLEvent
  ReadAsync(void *                  data,
            const OpenCLSize &      origin,
            const OpenCLSize &      region,
            const OpenCLEventList & event_list = OpenCLEventList(),
            const std::size_t       rowPitch = 0,
            const std::size_t       slicePitch = 0);

  /** Write an image or image array object from host memory,
   * starting at \a origin and range \a region into \a data.
   * Returns true if the read was successful, false otherwise.
   * This method does not return until the buffer data has been written
   * into memory pointed to \a data.
   * \sa Read(), ReadAsync(), WriteAsync() */
  bool
  Write(const void *       data,
        const OpenCLSize & origin,
        const OpenCLSize & region,
        const std::size_t  rowPitch = 0,
        const std::size_t  slicePitch = 0);

  /** Asynchronous version of the Write() method.
   * This function will queue the request and return immediately. Returns an
   * OpenCLEvent object that can be used to wait for the request to finish.
   * The request will not start until all of the events in \a event_list
   * have been signaled as completed.
   * \sa Read(), ReadAsync(), Write() */
  OpenCLEvent
  WriteAsync(const void *            data,
             const OpenCLSize &      origin,
             const OpenCLSize &      region,
             const OpenCLEventList & event_list = OpenCLEventList(),
             const std::size_t       rowPitch = 0,
             const std::size_t       slicePitch = 0);

  /** Map a region of an image object starting at \a origin and range \a region
   * into the host address space for the specified \a access mode and returns
   * a pointer to this mapped region.
   * This method does not return until the specified region in image is mapped
   * into the host address space and the application can access the contents
   * of the mapped region.
   * \sa Read(), ReadAsync(), Write(), WriteAsync(), MapAsync() */
  void *
  Map(const OpenCLMemoryObject::Access access,
      const OpenCLSize &               origin,
      const OpenCLSize &               region,
      std::size_t *                    rowPitch = 0,
      std::size_t *                    slicePitch = 0);

  /** Asynchronous version of the Map() method.
   * This function will queue the request and return immediately. Returns an
   * OpenCLEvent object that can be used to wait for the request to finish.
   * The request will not start until all of the events in \a event_list
   * have been signaled as completed.
   * \sa Read(), ReadAsync(), Write(), WriteAsync(), Map() */
  OpenCLEvent
  MapAsync(void **                          data,
           const OpenCLMemoryObject::Access access,
           const OpenCLSize &               origin,
           const OpenCLSize &               region,
           const OpenCLEventList &          event_list = OpenCLEventList(),
           std::size_t *                    rowPitch = 0,
           std::size_t *                    slicePitch = 0);

  /** Copies a region of an image object starting at \a origin and range \a region
   * to \a destOrigin in \a dest. Returns true if the copy was successful,
   * false otherwise. This function will block until the request finishes.
   * The request is executed on the active command queue for context().
   * \sa CopyAsync() */
  bool
  Copy(const OpenCLImage & dest, const OpenCLSize & origin, const OpenCLSize & region, const OpenCLSize & destOrigin);

  /** Asynchronous version of the Copy() method.
   * This function will queue the request and return immediately. Returns an
   * OpenCLEvent object that can be used to wait for the request to finish.
   * The request will not start until all of the events in \a event_list
   * have been signaled as completed.
   * \sa Copy() */
  OpenCLEvent
  CopyAsync(const OpenCLImage &     dest,
            const OpenCLSize &      origin,
            const OpenCLSize &      region,
            const OpenCLSize &      destOrigin,
            const OpenCLEventList & event_list = OpenCLEventList());

  /** Copies a region of an image object starting at \a origin and range \a region
   * to \a destOrigin in \a dest. Returns true if the copy was successful,
   * false otherwise. This function will block until the request finishes.
   * The request is executed on the active command queue for context().
   * \sa CopyAsync() */
  bool
  Copy(const OpenCLBuffer & dest,
       const OpenCLSize &   origin,
       const OpenCLSize &   region,
       const std::size_t    dst_offset = 0);

  /** Asynchronous version of the Copy() method.
   * This function will queue the request and return immediately. Returns an
   * OpenCLEvent object that can be used to wait for the request to finish.
   * The request will not start until all of the events in \a event_list
   * have been signaled as completed.
   * \sa Copy() */
  OpenCLEvent
  CopyAsync(const OpenCLBuffer &    dest,
            const OpenCLSize &      origin,
            const OpenCLSize &      region,
            const OpenCLEventList & event_list = OpenCLEventList(),
            const std::size_t       dst_offset = 0);

#ifdef CL_VERSION_1_2
  /** */
  static void
  SetImageDescription(cl_image_desc & imageDescription, const OpenCLImageFormat & format, const OpenCLSize & size);

#endif

protected:
  /** Get information specific to an image object created with clCreateImage */
  std::size_t
  GetImageInfo(const cl_image_info name) const;

  /** Set the image origin information */
  void
  SetOrigin(std::size_t * origin_t, const OpenCLSize & origin) const;

  /** Set the image region information */
  void
  SetRegion(std::size_t * region_t, const OpenCLSize & region) const;

  /** Set the image size information */
  void
  SetSize(std::size_t * region_t, const OpenCLSize & region, const std::size_t value) const;

  /** friends from OpenCL core */
  friend class OpenCLBuffer;
};

/** Stream out operator for OpenCLImage2D */
template <typename charT, typename traits>
inline std::basic_ostream<charT, traits> &
operator<<(std::basic_ostream<charT, traits> & strm, const OpenCLImage & image)
{
  if (image.IsNull())
  {
    strm << "OpenCLImage(null)";
    return strm;
  }

  const char indent = ' ';

  strm << "OpenCLImage\n"
       << indent << "Element size(bytes): " << image.GetElementSizeInBytes() << '\n'
       << indent << "Row size(bytes): " << image.GetRowSizeInBytes() << '\n'
       << indent << "Slice size(bytes): " << image.GetSliceSizeInBytes() << '\n'
       << indent << "Dimension: " << image.GetDimension() << '\n'
       << indent << "Width: " << image.GetWidth() << '\n'
       << indent << "Height: " << image.GetHeight() << '\n'
       << indent << "Depth: " << image.GetDepth() << std::endl;

  // Stream OpenCLMemoryObject
  const OpenCLMemoryObject & memObj = image;
  strm << memObj;

  return strm;
}


} // end namespace itk

#endif /* itkOpenCLImage_h */