25 #include <Base/Common/BIASpragma.hh>
26 #include <OpenCLFramework/clfImage2D.hh>
27 #include <OpenCLFramework/clfException.hh>
28 #include <Base/Debug/Error.hh>
32 std::vector<cl::ImageFormat> clfImage2D::imageFormatsReadable_;
33 std::vector<cl::ImageFormat> clfImage2D::imageFormatsWritable_;
34 std::vector<cl::ImageFormat> clfImage2D::imageFormatsReadWrite_;
48 if (imageFormatsReadable_.empty()) {
49 context_->getSupportedImageFormats(CL_MEM_READ_ONLY, CL_MEM_OBJECT_IMAGE2D, &imageFormatsReadable_);
51 if (imageFormatsWritable_.empty()) {
52 context_->getSupportedImageFormats(CL_MEM_READ_ONLY, CL_MEM_OBJECT_IMAGE2D, &imageFormatsWritable_);
54 if (imageFormatsReadWrite_.empty()) {
55 context_->getSupportedImageFormats(CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, &imageFormatsReadWrite_);
64 unsigned int width,
unsigned int height,
unsigned int stride,
65 bool readonly,
bool writeonly,
const void *hostptr,
bool copy) {
78 cl::ImageFormat format = DetermineImageFormat_(st,cm);
79 if (!GetFormatSupported_(format, readonly, writeonly)) {
80 THROW_CL_EXCEPTION(cl::Error(-100,
"requested image format is not supported by device."));
102 THROW_CL_EXCEPTION(cl::Error(-100,
"please provide stride for your data type or extend code for automatic support."));
113 size_ = stride*height;
114 buffer_ = cl::Image2D(*
context_, cl_mem_flags, format, width_, height_, 0, (
void*)hostptr);
116 }
catch(cl::Error &err) {
117 THROW_CL_EXCEPTION(err);
137 readonly, writeonly);
149 DetermineBIASFormat_(format, st, cm);
150 Allocate(st,cm, src.width_, src.height_, src.stride_, readonly, writeonly);
152 Allocate(src.st_, src.cm_, src.width_, src.height_, src.stride_, readonly, writeonly);
160 #ifdef CL_VERSION_1_2
166 buffer_.getInfo(CL_MEM_SIZE, &memsize);
180 BIASWARNONCE(
"cl image memory size reported 0. nvidia bug?");
183 }
catch(cl::Error &e) {
184 THROW_CL_EXCEPTION(e);
195 void clfImage2D::WriteToImage(
const void *data,
unsigned int originX,
unsigned int originY,
unsigned int regionX,
unsigned int regionY,
bool blocking) {
202 cl::size_t<3> origin, region;
203 MakeDim_(origin, originX, originY, 0);
204 MakeDim_(region, regionX, regionY, 1);
208 queue_->enqueueWriteImage(
image(), CL_TRUE, origin, region, 0, 0, (
void*)data);
210 queue_->enqueueWriteImage(
image(), CL_FALSE, origin, region, 0, 0, (
void*)data);
211 }
catch (cl::Error &error) {
212 THROW_CL_EXCEPTION(error);
223 cl::size_t<3> origin, region;
224 MakeDim_(origin, originX, originY, 0);
225 MakeDim_(region, regionX, regionY, 1);
227 queue_->enqueueReadImage(
image(), CL_TRUE, origin, region, 0, 0, data);
228 }
catch (cl::Error &error) {
229 THROW_CL_EXCEPTION(error);
233 void clfImage2D::CopyToImage(
clfImage2D &outputimage,
unsigned int srcoriginX,
unsigned int srcoriginY,
unsigned int dstoriginX,
unsigned int dstoriginY,
unsigned int regionX,
unsigned int regionY,
bool blocking) {
240 cl::size_t<3> srcorigin, dstorigin, region;
241 MakeDim_(srcorigin, srcoriginX, srcoriginY, 0);
242 MakeDim_(dstorigin, dstoriginX, dstoriginY, 0);
243 MakeDim_(region, regionX, regionY, 1);
247 queue_->enqueueCopyImage(
image(), outputimage.
image(), srcorigin, dstorigin, region, NULL, &event);
250 queue_->enqueueCopyImage(
image(), outputimage.
image(), srcorigin, dstorigin, region);
252 }
catch (cl::Error &error) {
254 THROW_CL_EXCEPTION(error);
259 void*
clfImage2D::MapImage(
bool write,
unsigned int originX,
unsigned int originY,
unsigned int regionX,
unsigned int regionY) {
261 cl_map_flags mapflags = 0;
263 mapflags = CL_MAP_WRITE;
265 mapflags = CL_MAP_READ;
273 cl::size_t<3> origin, region;
274 MakeDim_(origin, originX, originY, 0);
275 MakeDim_(region, regionX, regionY, 1);
276 return queue_->enqueueMapImage(
image(), CL_TRUE, mapflags, origin, region, 0, NULL);
277 }
catch (cl::Error &error) {
278 THROW_CL_EXCEPTION(error);
284 unsigned int originX,
unsigned int originY,
285 unsigned int regionX,
unsigned int regionY) {
297 DetermineBIASFormat_(format, st_, cm_);
322 cl::Error err(-100,
"only for rgba->rgb conversion");
323 THROW_CL_EXCEPTION(err);
326 unsigned char *cldata = (
unsigned char*)
MapImage(
false);
327 unsigned char *data = (
unsigned char*)image.
GetImageData();
328 for (
unsigned int x=0;x<width_*height_;x++) {
329 for (
unsigned int byte=0;byte<depth;byte++) {
332 for (
unsigned int byte=0;byte<depth;byte++) {
335 for (
unsigned int byte=0;byte<depth;byte++) {
338 for (
unsigned int byte=0;byte<depth;byte++) {
351 cl::Error err(-100,
"only for shared gl textures");
352 THROW_CL_EXCEPTION(err);
357 std::vector<cl::ImageFormat> *dat = NULL;
359 dat = &clfImage2D::imageFormatsReadable_;
360 }
else if (writeonly) {
361 dat = &clfImage2D::imageFormatsWritable_;
363 dat = &clfImage2D::imageFormatsReadWrite_;
365 std::vector<std::string> result( dat->size() );
366 for (
unsigned int i=0;i<dat->size();i++) {
367 switch ((*dat)[i].image_channel_order) {
369 result[i] =
"one channel (R) \t";
372 result[i] =
"one channel (A) \t";
375 result[i] =
"one channel (Intensity)\t";
378 result[i] =
"one channel (Luminance)\t";
381 result[i] =
"two channels (RG) \t";
384 result[i] =
"two channels (RA) \t";
387 result[i] =
"two channels (Rx) \t";
390 result[i] =
"three channels (RGB) \t";
393 result[i] =
"three channels (RGx) \t";
396 result[i] =
"four channels (RGBA) \t";
399 result[i] =
"four channels (BGRA) \t";
402 result[i] =
"four channels (ARGB) \t";
405 result[i] =
"four channels (RGBx) \t";
408 switch ((*dat)[i].image_channel_data_type) {
410 result[i] +=
"signed normalized char";
413 result[i] +=
"signed normalized short";
416 result[i] +=
"unsigned normalized char";
419 result[i] +=
"unsigned normalized short";
421 case CL_UNORM_SHORT_565:
422 result[i] +=
"unsigned normalized short 565";
424 case CL_UNORM_SHORT_555:
425 result[i] +=
"unsigned normalized short 555";
427 case CL_UNORM_INT_101010:
428 result[i] +=
"unsigned normalized int 101010";
431 result[i] +=
"signed char";
433 case CL_SIGNED_INT16:
434 result[i] +=
"signed short";
436 case CL_SIGNED_INT32:
437 result[i] +=
"signed int";
439 case CL_UNSIGNED_INT8:
440 result[i] +=
"unsigned char";
442 case CL_UNSIGNED_INT16:
443 result[i] +=
"unsigned short";
445 case CL_UNSIGNED_INT32:
446 result[i] +=
"unsigned int";
449 result[i] +=
"half float";
452 result[i] +=
"float";
460 cl::ImageFormat format;
463 format.image_channel_data_type = CL_SIGNED_INT8;
466 format.image_channel_data_type = CL_UNSIGNED_INT16;
469 format.image_channel_data_type = CL_SIGNED_INT16;
472 format.image_channel_data_type = CL_UNSIGNED_INT32;
475 format.image_channel_data_type = CL_SIGNED_INT32;
478 format.image_channel_data_type = CL_FLOAT;
481 format.image_channel_data_type = CL_UNSIGNED_INT8;
486 THROW_CL_EXCEPTION(cl::Error(-100,
"image storage type not supported!"));
497 format.image_channel_order = CL_R;
501 format.image_channel_order = CL_INTENSITY;
508 format.image_channel_order = CL_RG;
519 format.image_channel_order = CL_RGB;
525 format.image_channel_order = CL_RGBA;
528 THROW_CL_EXCEPTION(cl::Error(-100,
"color model not supported!"));
535 switch (format.image_channel_data_type) {
536 case CL_UNSIGNED_INT8:
539 case CL_UNSIGNED_INT16:
542 case CL_UNSIGNED_INT32:
553 switch (format.image_channel_order) {
575 bool clfImage2D::GetFormatSupported_(cl::ImageFormat test,
bool readonly,
bool writeonly) {
576 std::vector<cl::ImageFormat> *dat = NULL;
578 dat = &clfImage2D::imageFormatsReadable_;
579 }
else if (writeonly) {
580 dat = &clfImage2D::imageFormatsWritable_;
582 dat = &clfImage2D::imageFormatsReadWrite_;
584 for (
unsigned int i=0;i<dat->size();i++) {
585 if (test.image_channel_data_type == (*dat)[i].image_channel_data_type) {
586 if (test.image_channel_order == (*dat)[i].image_channel_order) {
594 void clfImage2D::MakeDim_(cl::size_t<3> &dest,
int x,
int y,
int z) {
EColorModel
These are the most often used color models.
YUYV422, 2 channels, full luminance Y, subsampled half U,V.
std::vector< std::string > GetSupportedImageFormats(bool readonly=false, bool writeonly=false)
Bayer_GRBG, 1 channel RGB image Bayer tile.
hsl, similar to HSL but euclidean (h,s) for CNCC
LAB, 3 channels, http://en.wikipedia.org/wiki/Lab_color_space.
(16bit) unsigned integer image storage type
void AllocateFromTexture2D(BIAS::glfTexture2D &tex, bool readonly=false, bool writeonly=false)
Allocation of a memory buffer from a GL Texture2D (works only on shared context!) ...
void ReadFromImage(void *data, unsigned int originX=0, unsigned int originY=0, unsigned int regionX=0, unsigned int regionY=0)
read from image to host memory
void * MapImage(bool write=false, unsigned int originX=0, unsigned int originY=0, unsigned int regionX=0, unsigned int regionY=0)
HSL, similar to HSV but space is a double tipped cone.
void Allocate(BIAS::ImageBase::EStorageType st, BIAS::ImageBase::EColorModel cm, unsigned int width, unsigned int height, unsigned int stride=0, bool readonly=false, bool writeonly=false, const void *hostptr=NULL, bool copy=false)
Allocation of a memory buffer as 2D image, either call directly or use wrapper for BIAS::ImageBase...
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
(8bit) signed char image storage type
unsigned int GetWidthStep() const
returns the number of bytes per line
static int GetStorageSizeByte(const unsigned int &width, const unsigned int &height, const unsigned int nChannels, const enum EStorageType storageType)
computes the storage data size in Byte required for a given video format.
clfImage2D(cl::Context *context, cl::CommandQueue *queue)
void SetColorModel(EColorModel Model)
void SetMetaData(const BIAS::MetaData &m)
Bayer_RGGB, 1 channel RGB image Bayer tile.
static int GetChannelcount(const enum BIAS::ImageBase::EColorModel &colormodel)
get the number of channels corresponding to the enum ColorModel Determines the number of (packed) dat...
double image storage type
XYZ, 3 channels, http://en.wikipedia.org/wiki/Xyz_color_space.
unsigned int GetWidth() const
unsigned int GetBitDepth() const
returns the bits per channel Is not necessairily 8*sizeof(StorageType), could be fewer bits...
void AllocateFromBiasImage(const BIAS::ImageBase &image, bool readonly=false, bool writeonly=false, bool copy=false)
Allocation of a memory buffer as 2D image, either call directly or use wrapper for BIAS::ImageBase...
int DetermineMemFlags_(bool readonly, bool writeonly, const void *hostptr=NULL, bool copy=false)
invalid not set image storage type
GLuint GetTextureID() const
Returns the OpenGL texture id.
(16bit) signed integer image storage type
color values, 3 channels, order: blue,green,red
Bayer_BGGR, 1 channel RGB image Bayer tile.
const void * GetImageData() const
void CopyToBiasImageUncheckedRGBAToRGB(BIAS::ImageBase &image)
Disparity images Q: should disp and depth be treated separately, if not what would be a good name to ...
void AllocateFromBiasTemplate(const BIAS::ImageBase &image, bool readonly=false, bool writeonly=false)
Bayer_GBRG, 1 channel RGB image Bayer tile.
cl::CommandQueue * queue_
color values, 3 channels, order: red,green,blue
CIELUV color space, 3 channels, http://en.wikipedia.org/wiki/CIELUV_color_space.
unsigned int GetHeight() const
void SetStorageAndColorModelInfo(const BIAS::ImageBase::EStorageType &st, const BIAS::ImageBase::EColorModel &cm)
cl::Image2D & image() const
UYVY422, 2 channels, full luminance Y, subsampled half U,V inverse order.
RGBA, 4 channels, order: red,green,blue,alpha.
void Release(const bool reset_storage_type=false)
Free the allocated data structures Hands off: Do !!NOT!! change the default of reset_storage_type: Im...
enum EColorModel GetColorModel() const
(32bit) signed integer image storage type
RGBE color values, 4 channels, RADIANCE hdr format, four low dynamic channels meaning: 3x mantissa (r...
void CopyToBiasImage(BIAS::ImageBase &image, unsigned int originX=0, unsigned int originY=0, unsigned int regionX=0, unsigned int regionY=0)
void Init(unsigned int width, unsigned int height, unsigned int nChannels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
Initialize image size and channels.
invalid (not set) image format
enum EStorageType GetStorageType() const
(8bit) unsigned char image storage type
HSV, 3 channels, order: hue, sat , value.
void AllocateFromTemplate(const clfImage2D &src, bool readonly=false, bool writeonly=false)
GLenum GetTextureTarget() const
Returns the OpenGL texture target.
void SetMetaData(const MetaData &m)
Todo: Unclear, I think one channel float, why isn't grey used for that?
GreyA, 2 channels, grey plus Alpha.
This is the base class for images in BIAS.
Depth images A: separated for now.
unsigned int GetByteDepth() const
void WriteToImage(const void *data, unsigned int originX=0, unsigned int originY=0, unsigned int regionX=0, unsigned int regionY=0, bool blocking=true)
write from host memory to image
void CopyToImage(clfImage2D &outputimage, unsigned int srcoriginX=0, unsigned int srcoriginY=0, unsigned int dstoriginX=0, unsigned int dstoriginY=0, unsigned int regionX=0, unsigned int regionY=0, bool blocking=true)
BGRA color values, 4 channels, order: blue,green,red,alpha.
(32bit) unsigned integer image storage type