Beatmup
converter.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "../context.h"
20 #include "../bitmap/converter.h"
21 #include "../bitmap/bitmap_access.h"
22 #include "../bitmap/mask_bitmap_access.h"
23 #include "../exception.h"
24 #include <algorithm>
25 #include <cstring>
26 
27 using namespace Beatmup;
28 
29 
30 template<class in_t, class out_t> inline void convertBlock(AbstractBitmap& input, AbstractBitmap& output, int startx, int starty, msize nPix) {
31  in_t in(input, startx, starty);
32  out_t out(output, startx, starty);
33  for (msize n = 0; n < nPix; ++n, in++, out++)
34  out = in();
35 }
36 
37 
39  input(nullptr), output(nullptr)
40 {}
41 
42 
44  this->input = input;
45  this->output = output;
46 }
47 
48 
51 }
52 
53 
55  // it does not make sense to convert bitmaps on GPU
57 }
58 
59 
61  NullTaskInput::check(input, "input bitmap");
62  NullTaskInput::check(output, "output bitmap");
64  "Input and output bitmap must be of the same size.");
65  lock<ProcessingTarget::CPU>(gpu, input, output);
66 }
67 
68 
69 void FormatConverter::afterProcessing(ThreadIndex threadCount, GraphicPipeline* gpu, bool aborted) {
71 }
72 
73 
75  // if the bitmaps are equal, say done
76  if (input == output)
77  return true;
78 
79  // if pixel formats are identical, just copy
81  if (thread.currentThread() == 0)
82  memcpy(
83  output->getData(0,0),
84  input->getData(0,0),
86  );
87  return true;
88  }
89 
90  // compute splitting accordingly to the current thread index
91  int w = output->getWidth();
92  msize
93  npix = w * output->getHeight(),
94  start = npix * thread.currentThread() / thread.numThreads(),
95  stop = npix * (1 + thread.currentThread()) / thread.numThreads();
96 
97  // computing initial position
98  int outx = (int)(start % w), outy = (int)(start / w);
99 
100  const int LOOK_AROUND_INTERVAL = 123456;
101  while (start < stop && !thread.isTaskAborted()) {
102  doConvert(outx, outy, stop-start);
103  start += LOOK_AROUND_INTERVAL;
104  }
105 
106  return true;
107 }
108 
109 
110 void FormatConverter::doConvert(int outX, int outY, msize nPix) {
111 #define CALL_CONVERT_AND_RETURN(IN_T, OUT_T) \
112  convertBlock < IN_T, OUT_T >(*input, *output, outX, outY, nPix); return;
113 
114  // for all (left) possible pairs of pixel formats, do the conversion
115  switch (input->getPixelFormat()) {
116  case SingleByte:
117  switch (output->getPixelFormat()) {
123  default: throw ImplementationUnsupported("Cannot convert given formats");
124  }
125 
126  case TripleByte:
127  switch (output->getPixelFormat()) {
133  default: throw ImplementationUnsupported("Cannot convert given formats");
134  }
135 
136  case QuadByte:
137  switch (output->getPixelFormat()) {
143  default: throw ImplementationUnsupported("Cannot convert given formats");
144  }
145 
146  case SingleFloat:
147  switch (output->getPixelFormat()) {
153  default: throw ImplementationUnsupported("Cannot convert given formats");
154  }
155 
156  case TripleFloat:
157  switch (output->getPixelFormat()) {
163  default: throw ImplementationUnsupported("Cannot convert given formats");
164  }
165 
166  case QuadFloat:
167  switch (output->getPixelFormat()) {
173  default: throw ImplementationUnsupported("Cannot convert given formats");
174  }
175 
176  case BinaryMask:
177  switch (output->getPixelFormat()) {
183  default: throw ImplementationUnsupported("Cannot convert given formats");
184  }
185 
186  case QuaternaryMask:
187  switch (output->getPixelFormat()) {
193  default: throw ImplementationUnsupported("Cannot convert given formats");
194  }
195 
196  case HexMask:
197  switch (output->getPixelFormat()) {
203  default: throw ImplementationUnsupported("Cannot convert given formats");
204  }
205  }
206 }
207 
208 
210  FormatConverter me;
211  me.setBitmaps(&input, &output);
212 
215  "Input and output bitmap must be of the same size.");
216 
217  // the bitmaps are assumed locked in this case
218  me.doConvert(0, 0, input.getSize().numPixels());
219 
220  }
221  else
223 }
A very basic class for any image.
Context & getContext() const
virtual const PixelFormat getPixelFormat() const =0
Pixel format of the bitmap.
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
virtual const pixbyte * getData(int x, int y) const =0
Returns a pointer to given pixel.
virtual const msize getMemorySize() const =0
Bitmap size in bytes.
static ThreadIndex validThreadCount(int number)
Valid thread count from a given integer value.
Definition: parallelism.cpp:45
TaskDeviceRequirement
Specifies which device (CPU and/or GPU) is used to run the task.
Definition: parallelism.h:95
@ CPU_ONLY
this task does not use GPU
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.
float performTask(AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
Performs a given task.
Definition: context.cpp:240
bool isManagingThread() const
Definition: context.cpp:336
Converts bitmap content from one pixel format to another one.
Definition: converter.h:28
virtual bool process(TaskThread &thread)
Executes the task on CPU within a given thread.
Definition: converter.cpp:74
virtual void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
Instruction called before the task is executed.
Definition: converter.cpp:60
AbstractBitmap * input
Definition: converter.h:31
const int MIN_PIXEL_COUNT_PER_THREAD
minimum number of pixels per worker
Definition: converter.h:30
TaskDeviceRequirement getUsedDevices() const
Communicates devices (CPU and/or GPU) the task is run on.
Definition: converter.cpp:54
AbstractBitmap * output
input and output bitmaps
Definition: converter.h:31
static void convert(AbstractBitmap &input, AbstractBitmap &output)
Definition: converter.cpp:209
ThreadIndex getMaxThreads() const
Gives the upper limint on the number of threads the task may be performed by.
Definition: converter.cpp:49
void doConvert(int outX, int outY, msize nPix)
Definition: converter.cpp:110
void setBitmaps(AbstractBitmap *input, AbstractBitmap *output)
Definition: converter.cpp:43
virtual void afterProcessing(ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
Instruction called after the task is executed.
Definition: converter.cpp:69
virtual const int getHeight() const =0
Height of the texture in pixels.
virtual const int getWidth() const =0
Width of the texture in pixels.
Internal low-level GPU control API.
Definition: pipeline.h:33
Exception thrown when an implementation restriction is encountered.
Definition: exception.h:124
A generic to access sub-byte mask bitmap data.
static void check(const void *pointer, const char *which)
Definition: exception.h:115
Quad byte bitmap reader.
Quad byte bitmap writer.
Quad float bitmap reader.
Quad float bitmap writer.
static void check(const bool condition, const std::string &message)
Definition: exception.h:64
Single byte bitmap reader.
Single byte bitmap writer.
Single float bitmap reader.
Single float bitmap writer.
Thread executing tasks.
Definition: parallelism.h:154
virtual ThreadIndex numThreads() const =0
ThreadIndex currentThread() const
Definition: parallelism.h:165
virtual bool isTaskAborted() const =0
Returns true if the task is asked to stop from outside.
Triple byte bitmap reader.
Triple byte bitmap writer.
Triple float bitmap reader.
Triple float bitmap writer.
#define CALL_CONVERT_AND_RETURN(IN_T, OUT_T)
void convertBlock(AbstractBitmap &input, AbstractBitmap &output, int startx, int starty, msize nPix)
Definition: converter.cpp:30
uint32_t msize
memory size
Definition: basic_types.h:30
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
@ SingleByte
single channel of 8 bits per pixel (like grayscale), unsigned integer values
@ SingleFloat
single channel of 32 bits per pixel (like grayscale), single precision floating point values
@ QuaternaryMask
2 bits per pixel
@ QuadFloat
4 channels of 32 bits per pixel, single precision floating point values,
@ TripleFloat
3 channels of 32 bits per pixel, single precision floating point values
@ QuadByte
4 channels of 8 bits per pixel (like RGBA), unsigned integer values
@ TripleByte
3 channels of 8 bits per pixel (like RGB), unsigned integer values
@ BinaryMask
1 bit per pixel
@ HexMask
4 bits per pixel
ProcessingTarget
Definition: basic_types.h:55
jlong jstring jint jint jint jint w
jlong jint start
JNIEnv jlong jint out
jlong jint jint jint jint jint jint jint outx
jlong jint jint jint jint jint jint jint jint outy
int n