Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImageConvertThreaded.cpp
1 #include "ImageConvertThreaded.hh"
2 
3 #include <Base/Debug/Exception.hh>
4 
5 //TODO pthread DLL is linked into this DLL!
6 //#ifdef BIASImageBase_EXPORTS
7 //# define PTW32_BUILD
8 //# define _DLL
9 //#endif // BIASImageBase_EXPORTS
10 #include <pthread.h>
11 
12 
13 
14 using namespace BIAS;
15 using namespace std;
16 
19 {}
20 
21 
24 {}
25 
26 /**\cond HIDDEN_SYMBOLS
27  * \struct ThreadArg__ */
28 struct ThreadArg__ {
29  ImageBase *src, *dst;
30  ImageBase::EColorModel colormodel;
31  bool planar;
32 };
33 /** \endcond */
34 
37  enum BIAS::ImageBase::EColorModel targetColorModel,
38  bool bPlanar, const int num_threads)
39 {
40  vector<ImageBase> src(num_threads), dst(num_threads);
41  vector<ThreadArg__> arg(num_threads);
42  vector<pthread_t> thread(num_threads);
43 
44  SplitImage_(source, num_threads, src);
45 
46  for (int i=0; i<num_threads; i++){
47  arg[i].src = &(src[i]);
48  arg[i].dst = &(dst[i]);
49  arg[i].colormodel = targetColorModel;
50  arg[i].planar = bPlanar;
51  if (pthread_create(&(thread[i]), NULL, ConvertThread_, &(arg[i]))!=0){
52  BEXCEPTION("error creating thread "<<i);
53  }
54  }
55 
56  int overall_result = 0;
57  vector<int> res(num_threads);
58  void *result=NULL;
59  for (int i=0; i<num_threads; i++){
60  if (pthread_join(thread[i], &result)!=0){
61  BEXCEPTION("error joining thread "<<i);
62  }
63  res[i] = *(int *)result;
64  if (res[i]!=0) { overall_result--; }
65  delete (int *)result;
66  result = NULL;
67  }
68 
69  int tlx, tly, brx, bry;
70  source.GetROI()->GetCorners(tlx, tly, brx, bry);
71  if (!source.SamePixelAndChannelCount(dest)){
72  if (!dest.IsEmpty()){ dest.Release(); }
73  }
74  if (dest.IsEmpty()){
75  dest.Init(source.GetWidth(), source.GetHeight(), dst[0].GetChannelCount(),
76  dst[0].GetStorageType(), dst[0].IsInterleaved());
77  }
78  dest.SetColorModel(dst[0].GetColorModel());
79  dest.GetROI()->SetCorners(tlx, tly, brx, bry);
80  MergeImage_(dst, dest);
81 
82  return overall_result;
83 }
84 
86 ConvertThread_(void *arg)
87 {
88  ThreadArg__ *targ = static_cast<ThreadArg__ *>(arg);
89  int *res = new int;
90  *res = ImageConvert::Convert(*(targ->src), *(targ->dst),
91  targ->colormodel, targ->planar);
92  return static_cast<void *>(res);
93 }
94 
96 SplitImage_(const ImageBase &source, const int num,
97  std::vector<ImageBase> &parts)
98 {
99  if ((int)parts.size()!=num){
100  parts.resize(num);
101  }
102  ImageBase src(source); // de-const
103  int tlx, tly, brx, bry;
104  switch (num){
105  case 2:
106  src.GetROI()->GetCorners(tlx, tly, brx, bry);
107  src.GetROI()->SetCorners(tlx, tly, brx, bry/2);
108  if (src.GetCopyOfROI(parts[0])!=0){
109  BEXCEPTION("error copying ROI");
110  }
111  src.GetROI()->SetCorners(tlx, bry/2, brx, bry);
112  if (src.GetCopyOfROI(parts[1])!=0){
113  BEXCEPTION("error copying ROI");
114  }
115  break;
116  case 4:
117  src.GetROI()->GetCorners(tlx, tly, brx, bry);
118  src.GetROI()->SetCorners(tlx, tly, brx/2, bry/2); //top left quarter
119  if (src.GetCopyOfROI(parts[0])!=0){
120  BEXCEPTION("error copying ROI");
121  }
122  src.GetROI()->SetCorners(brx/2, tly, brx, bry/2); //top right quarter
123  if (src.GetCopyOfROI(parts[1])!=0){
124  BEXCEPTION("error copying ROI");
125  }
126  src.GetROI()->SetCorners(tlx, bry/2, brx/2, bry); //bottom left quarter
127  if (src.GetCopyOfROI(parts[2])!=0){
128  BEXCEPTION("error copying ROI");
129  }
130  src.GetROI()->SetCorners(brx/2, bry/2, brx, bry); //bottom right quarter
131  if (src.GetCopyOfROI(parts[3])!=0){
132  BEXCEPTION("error copying ROI");
133  }
134  break;
135  default:
136  BEXCEPTION("only 2 or 4 threads are supported");
137  break;
138  }
139 }
140 
142 MergeImage_(const std::vector<ImageBase>& parts,
143  ImageBase& dst)
144 {
145  int tlx, tly, brx, bry;
146  switch (parts.size()){
147  case 2:
148  dst.GetROI()->GetCorners(tlx, tly, brx, bry);
149  dst.GetROI()->SetCorners(tlx, tly, brx, bry/2);
150  dst.Paste2ROI(parts[0]);
151  dst.GetROI()->SetCorners(tlx, bry/2, brx, bry);
152  dst.Paste2ROI(parts[1]);
153  dst.GetROI()->SetCorners(tlx, tly, brx, bry);
154  break;
155  case 4:
156  dst.GetROI()->GetCorners(tlx, tly, brx, bry);
157  dst.GetROI()->SetCorners(tlx, tly, brx/2, bry/2);
158  dst.Paste2ROI(parts[0]);
159  dst.GetROI()->SetCorners(brx/2, tly, brx, bry/2);
160  dst.Paste2ROI(parts[1]);
161  dst.GetROI()->SetCorners(tlx, bry/2, brx/2, bry);
162  dst.Paste2ROI(parts[2]);
163  dst.GetROI()->SetCorners(brx/2, bry/2, brx, bry);
164  dst.Paste2ROI(parts[3]);
165  dst.GetROI()->SetCorners(tlx, tly, brx, bry);
166  break;
167  default:
168  BEXCEPTION("only 2 or 4 threads are supported");
169  break;
170  }
171 }
172 
int GetCopyOfROI(ImageBase &copy) const
returns a copy of ROI (a new image) lower right point excluded, only interleaved images ! ...
Definition: ImageBase.cpp:568
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
static void SplitImage_(const ImageBase &source, const int num, std::vector< ImageBase > &parts)
static void MergeImage_(const std::vector< ImageBase > &parts, ImageBase &dest)
int SetCorners(unsigned UpperLeftX, unsigned UpperLeftY, unsigned LowerRightX, unsigned LowerRightY)
Sets a rectangular region of interest.
Definition: ROI.cpp:287
void GetCorners(unsigned &UpperLeftX, unsigned &UpperLeftY, unsigned &LowerRightX, unsigned &LowerRightY) const
Return the region of interest, by saving the coordinates within the variables defined by the paramete...
Definition: ROI.hh:443
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
unsigned int GetWidth() const
Definition: ImageBase.hh:312
ROI * GetROI()
Returns a pointer to the roi object.
Definition: ImageBase.hh:615
static int ConvertThreaded(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false, const int num_threads=2)
Convert using num_threads threads.
unsigned int GetHeight() const
Definition: ImageBase.hh:319
bool SamePixelAndChannelCount(const ImageBase &Image) const
checks if data area has same &quot;size&quot; as Image of other type
Definition: ImageBase.hh:73
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...
Definition: ImageBase.cpp:350
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.
Definition: ImageBase.cpp:229
static void * ConvertThread_(void *arg)
static int Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false)
main general conversion function, calls desired specialized functions, always initializes the destIma...
int Paste2ROI(const ImageBase &Image)
paste Image to current ROI
Definition: ImageBase.cpp:603
This is the base class for images in BIAS.
Definition: ImageBase.hh:102