Beatmup
Beatmup::FormatConverter Class Reference

Converts bitmap content from one pixel format to another one. More...

#include <converter.h>

Inheritance diagram for Beatmup::FormatConverter:
Beatmup::AbstractTask Beatmup::BitmapContentLock Beatmup::Object

Public Member Functions

 FormatConverter ()
 
void setBitmaps (AbstractBitmap *input, AbstractBitmap *output)
 
ThreadIndex getMaxThreads () const
 Gives the upper limint on the number of threads the task may be performed by. More...
 
TaskDeviceRequirement getUsedDevices () const
 Communicates devices (CPU and/or GPU) the task is run on. More...
 
- Public Member Functions inherited from Beatmup::AbstractTask
virtual bool processOnGPU (GraphicPipeline &gpu, TaskThread &thread)
 Executes the task on GPU. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Static Public Member Functions

static void convert (AbstractBitmap &input, AbstractBitmap &output)
 
- Static Public Member Functions inherited from Beatmup::AbstractTask
static ThreadIndex validThreadCount (int number)
 Valid thread count from a given integer value. More...
 

Protected Member Functions

virtual bool process (TaskThread &thread)
 Executes the task on CPU within a given thread. More...
 
virtual void beforeProcessing (ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
 Instruction called before the task is executed. More...
 
virtual void afterProcessing (ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
 Instruction called after the task is executed. More...
 

Private Member Functions

void doConvert (int outX, int outY, msize nPix)
 
- Private Member Functions inherited from Beatmup::BitmapContentLock
 BitmapContentLock ()
 
 ~BitmapContentLock ()
 
void readLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for reading using a specific processing target device. More...
 
void writeLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for writing using a specific processing target device. More...
 
void unlock (AbstractBitmap *bitmap)
 Drops a lock to the bitmap. More...
 
void unlockAll ()
 Unlocks all the locked bitmaps unconditionally. More...
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, AbstractBitmap *input, AbstractBitmap *output)
 
void lock (GraphicPipeline *gpu, ProcessingTarget target, AbstractBitmap *input, AbstractBitmap *output)
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, std::initializer_list< AbstractBitmap * > read, std::initializer_list< AbstractBitmap * > write)
 
template<typename ... Args>
void unlock (AbstractBitmap *first, Args ... others)
 

Private Attributes

const int MIN_PIXEL_COUNT_PER_THREAD = 1000
 minimum number of pixels per worker More...
 
AbstractBitmapinput
 
AbstractBitmapoutput
 input and output bitmaps More...
 

Additional Inherited Members

- Public Types inherited from Beatmup::AbstractTask
enum class  TaskDeviceRequirement { CPU_ONLY , GPU_OR_CPU , GPU_ONLY }
 Specifies which device (CPU and/or GPU) is used to run the task. More...
 

Detailed Description

Converts bitmap content from one pixel format to another one.

Definition at line 28 of file converter.h.

Constructor & Destructor Documentation

◆ FormatConverter()

FormatConverter::FormatConverter ( )

Definition at line 38 of file converter.cpp.

38  :
39  input(nullptr), output(nullptr)
40 {}
AbstractBitmap * input
Definition: converter.h:31
AbstractBitmap * output
input and output bitmaps
Definition: converter.h:31

Member Function Documentation

◆ doConvert()

void FormatConverter::doConvert ( int  outX,
int  outY,
msize  nPix 
)
private

Definition at line 110 of file converter.cpp.

110  {
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 }
virtual const PixelFormat getPixelFormat() const =0
Pixel format of the bitmap.
Exception thrown when an implementation restriction is encountered.
Definition: exception.h:124
A generic to access sub-byte mask bitmap data.
Quad byte bitmap reader.
Quad byte bitmap writer.
Quad float bitmap reader.
Quad float bitmap writer.
Single byte bitmap reader.
Single byte bitmap writer.
Single float bitmap reader.
Single float bitmap writer.
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)
@ 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

◆ process()

bool FormatConverter::process ( TaskThread thread)
protectedvirtual

Executes the task on CPU within a given thread.

Generally called by multiple threads.

Parameters
threadassociated task execution context
Returns
true if the execution is finished correctly, false otherwise

Implements Beatmup::AbstractTask.

Definition at line 74 of file converter.cpp.

74  {
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 }
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.
void doConvert(int outX, int outY, msize nPix)
Definition: converter.cpp:110
virtual const int getHeight() const =0
Height of the texture in pixels.
virtual const int getWidth() const =0
Width of the texture in pixels.
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.
uint32_t msize
memory size
Definition: basic_types.h:30
jlong jstring jint jint jint jint w
jlong jint start
jlong jint jint jint jint jint jint jint outx
jlong jint jint jint jint jint jint jint jint outy

◆ beforeProcessing()

void FormatConverter::beforeProcessing ( ThreadIndex  threadCount,
ProcessingTarget  target,
GraphicPipeline gpu 
)
protectedvirtual

Instruction called before the task is executed.

Parameters
threadCountNumber of threads used to perform the task
targetDevice used to perform the task
gpuA graphic pipeline instance; may be null.

Reimplemented from Beatmup::AbstractTask.

Definition at line 60 of file converter.cpp.

60  {
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 }
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
static void check(const void *pointer, const char *which)
Definition: exception.h:115
static void check(const bool condition, const std::string &message)
Definition: exception.h:64

◆ afterProcessing()

void FormatConverter::afterProcessing ( ThreadIndex  threadCount,
GraphicPipeline gpu,
bool  aborted 
)
protectedvirtual

Instruction called after the task is executed.

Parameters
threadCountNumber of threads used to perform the task
gpuGPU to be used to execute the task; may be null.
abortedtrue if the task was aborted

Reimplemented from Beatmup::AbstractTask.

Definition at line 69 of file converter.cpp.

69  {
71 }
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.

◆ setBitmaps()

void FormatConverter::setBitmaps ( AbstractBitmap input,
AbstractBitmap output 
)

Definition at line 43 of file converter.cpp.

43  {
44  this->input = input;
45  this->output = output;
46 }

◆ getMaxThreads()

ThreadIndex FormatConverter::getMaxThreads ( ) const
virtual

Gives the upper limint on the number of threads the task may be performed by.

The actual number of threads running a specific task may be less or equal to the returned value, depending on the number of workers in ThreadPool running the task.

Reimplemented from Beatmup::AbstractTask.

Definition at line 49 of file converter.cpp.

49  {
51 }
static ThreadIndex validThreadCount(int number)
Valid thread count from a given integer value.
Definition: parallelism.cpp:45
const int MIN_PIXEL_COUNT_PER_THREAD
minimum number of pixels per worker
Definition: converter.h:30

◆ getUsedDevices()

AbstractTask::TaskDeviceRequirement FormatConverter::getUsedDevices ( ) const
virtual

Communicates devices (CPU and/or GPU) the task is run on.

Reimplemented from Beatmup::AbstractTask.

Definition at line 54 of file converter.cpp.

54  {
55  // it does not make sense to convert bitmaps on GPU
57 }
@ CPU_ONLY
this task does not use GPU

◆ convert()

void FormatConverter::convert ( AbstractBitmap input,
AbstractBitmap output 
)
static

Definition at line 209 of file converter.cpp.

209  {
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 }
Context & getContext() const
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
void setBitmaps(AbstractBitmap *input, AbstractBitmap *output)
Definition: converter.cpp:43

Member Data Documentation

◆ MIN_PIXEL_COUNT_PER_THREAD

const int Beatmup::FormatConverter::MIN_PIXEL_COUNT_PER_THREAD = 1000
private

minimum number of pixels per worker

Definition at line 30 of file converter.h.

◆ input

AbstractBitmap* Beatmup::FormatConverter::input
private

Definition at line 31 of file converter.h.

◆ output

AbstractBitmap * Beatmup::FormatConverter::output
private

input and output bitmaps

Definition at line 31 of file converter.h.


The documentation for this class was generated from the following files: