Beatmup
crop.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 "abstract_bitmap.h"
20 #include "internal_bitmap.h"
21 #include "bitmap_access.h"
22 #include "processing.h"
23 #include "crop.h"
24 #include "../debug.h"
25 #include <cstring>
26 
27 using namespace Beatmup;
28 
29 
30 namespace Kernels {
31  template<class in_t, class out_t> class Cropping {
32  public:
33  static inline void process(AbstractBitmap& input, AbstractBitmap& output, const IntRectangle& rect, const IntPoint& outOrigin) {
34  const unsigned char
36  ppb = 8 / bpp; // pixels per byte
37 
38  // test if output origin and clip rect horizontal borders are byte-aligned and the pixel formats are identical
39  const bool mayCopy = (input.getPixelFormat() == output.getPixelFormat()) &&
40  (bpp >= 8 || (outOrigin.x % ppb == 0 && rect.a.x % ppb == 0 && rect.b.x % ppb == 0));
41 
42  in_t in(input);
43  out_t out(output);
44 
45  if (mayCopy) {
46  // direct copying
47  const msize lineSizeBytes = bpp >= 8 ? rect.width() * bpp / 8 : rect.width() / ppb;
48  for (int y = rect.a.y; y < rect.b.y; ++y) {
49  out.goTo(outOrigin.x, outOrigin.y + y - rect.a.y);
50  in.goTo(rect.a.x, y);
51  memcpy(*out, *in, lineSizeBytes);
52  }
53  }
54  else
55  // projecting
56  for (int y = rect.a.y; y < rect.b.y; ++y) {
57  out.goTo(outOrigin.x, outOrigin.y + y - rect.a.y);
58  in.goTo(rect.a.x, y);
59  for (int x = rect.a.x; x < rect.b.x; ++x, in++, out++)
60  out = in();
61  }
62  }
63  };
64 }
65 
66 
67 Crop::Crop() : outOrigin(0, 0), cropRect(0, 0, 0, 0)
68 {}
69 
70 
71 bool Crop::process(TaskThread& thread) {
72  BitmapProcessing::pipeline<Kernels::Cropping>(*input, *output, cropRect, outOrigin);
73  return true;
74 }
75 
76 
78  NullTaskInput::check(input, "input bitmap");
79  NullTaskInput::check(output, "output bitmap");
81  if (!isFit()) {
82  BEATMUP_DEBUG_E("Crop rectangle does not fit to bitmaps: ((%d,%d),(%d,%d)) from %d x %d to put at (%d,%d) in %d x %d.",
87  throw RuntimeError("Crop rectangle does not fit to bitmaps");
88  }
89  lock<ProcessingTarget::CPU>(gpu, input, output);
90 }
91 
92 
93 void Crop::afterProcessing(ThreadIndex threadCount, GraphicPipeline* gpu, bool aborted) {
95 }
96 
97 
99  this->input = input;
100 }
101 
102 
104  this->output = output;
105 }
106 
107 
109  cropRect = rect;
110 }
111 
112 
114  outOrigin = pos;
115 }
116 
117 
118 bool Crop::isFit() const {
119  if (!input || !output)
120  return false;
122  return false;
123  IntPoint corner = cropRect.b - cropRect.a - 1 + outOrigin;
124  if (!output->getSize().closedRectangle().isInside(corner))
125  return false;
126  return true;
127 }
128 
129 
131  AbstractBitmap* out = new InternalBitmap(bitmap.getContext(), bitmap.getPixelFormat(), clipRect.width(), clipRect.height());
132  // after byte-aligning out must be zeroed (not very optimal...)
133  if (out->getSize().numPixels() != clipRect.getArea())
134  out->zero();
135  Crop clip;
136  clip.setInput(&bitmap);
137  clip.setOutput(out);
138  clip.setCropRect(clipRect);
139  bitmap.getContext().performTask(clip);
140  return out;
141 }
A very basic class for any image.
static const unsigned char BITS_PER_PIXEL[NUM_PIXEL_FORMATS]
number of bits per pixel for each pixel format
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.
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
A task to clip images on CPU.
Definition: crop.h:29
virtual bool process(TaskThread &)
Executes the task on CPU within a given thread.
Definition: crop.cpp:71
IntRectangle cropRect
clip rect on input bitmap
Definition: crop.h:33
void setOutput(AbstractBitmap *output)
Definition: crop.cpp:103
virtual void afterProcessing(ThreadIndex, GraphicPipeline *, bool)
Instruction called after the task is executed.
Definition: crop.cpp:93
static AbstractBitmap * run(AbstractBitmap &bitmap, IntRectangle clipRect)
Copies out a specified rect of a bitmap into another bitmap.
Definition: crop.cpp:130
IntPoint outOrigin
origin on output bitmap
Definition: crop.h:32
void setOutputOrigin(IntPoint)
Sets top-left position of the clip rectangle in output bitmap.
Definition: crop.cpp:113
bool isFit() const
Checks if everything is fitted to make cropping.
Definition: crop.cpp:118
AbstractBitmap * input
Definition: crop.h:31
virtual void beforeProcessing(ThreadIndex, ProcessingTarget target, GraphicPipeline *)
Instruction called before the task is executed.
Definition: crop.cpp:77
void setCropRect(IntRectangle)
Sets crop rectangle in input bitmap.
Definition: crop.cpp:108
AbstractBitmap * output
input and output bitmaps
Definition: crop.h:31
void setInput(AbstractBitmap *input)
Definition: crop.cpp:98
numeric height() const
Definition: geometry.h:178
CustomPoint< numeric > b
Definition: geometry.h:131
numeric getArea() const
Computes the rectangle area.
Definition: geometry.h:185
numeric width() const
Definition: geometry.h:174
CustomPoint< numeric > a
Definition: geometry.h:131
bool isInside(const CustomPoint< numeric > &point) const
Test if a point is inside the rectangle (or on its the border)
Definition: geometry.h:252
void normalize()
Flips corners coordinates guaranteeing that it has a non negative area, i.e.
Definition: geometry.h:192
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
IntRectangle closedRectangle() const
Bitmap whose memory is managed by the Beatmup engine.
const PixelFormat getPixelFormat() const
Pixel format of the bitmap.
static void check(const void *pointer, const char *which)
Definition: exception.h:115
Thread executing tasks.
Definition: parallelism.h:154
static void process(AbstractBitmap &input, AbstractBitmap &output, const IntRectangle &rect, const IntPoint &outOrigin)
Definition: crop.cpp:33
#define BEATMUP_DEBUG_E(...)
Definition: debug.h:34
uint32_t msize
memory size
Definition: basic_types.h:30
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
ProcessingTarget
Definition: basic_types.h:55
Operations kernels.
Definition: basic_types.h:85
jobject jlong jint jint y
jobject jlong jint x
JNIEnv jlong jint out
Beatmup::InternalBitmap * bitmap
Beatmup::IntPoint outOrigin(left, top)
Beatmup::IntRectangle rect(x1, y1, x2, y2)