Scroom 0.14-49-gb7ae7a6d
Loading...
Searching...
No Matches
CommonOperations Class Reference

#include <layeroperations.hh>

Inheritance diagram for CommonOperations:
Inheritance graph
Collaboration diagram for CommonOperations:
Collaboration graph

Public Member Functions

void initializeCairo (cairo_t *cr) override
 
void drawState (cairo_t *cr, TileState s, Scroom::Utils::Rectangle< double > viewArea) override
 
Scroom::Utils::Stuff cacheZoom (const ConstTile::Ptr &tile, int zoom, Scroom::Utils::Stuff &cache) override
 
void draw (cairo_t *cr, const ConstTile::Ptr &tile, Scroom::Utils::Rectangle< double > tileArea, Scroom::Utils::Rectangle< double > viewArea, int zoom, Scroom::Utils::Stuff cache) override
 
- Public Member Functions inherited from LayerOperations
virtual int getBpp ()=0
 
virtual Scroom::Utils::Stuff cache (const ConstTile::Ptr &)
 
virtual void reduce (Tile::Ptr target, ConstTile::Ptr source, int x, int y)=0
 

Static Public Member Functions

static void drawPixelValue (cairo_t *cr, int x, int y, int size, int value)
 
static void drawPixelValue (cairo_t *cr, int x, int y, int size, int value, Color const &bgColor)
 

Additional Inherited Members

- Public Types inherited from LayerOperations
using Ptr = std::shared_ptr< LayerOperations >
 

Member Function Documentation

◆ cacheZoom()

Scroom::Utils::Stuff CommonOperations::cacheZoom ( const ConstTile::Ptr ,
int  ,
Scroom::Utils::Stuff  
)
overridevirtual

Cache data for use during later draw() calls.

This function is called for a given tile and zoom level, and then the results are passed to the draw() function. Because draw() is called relatively frequently (i.e. when scrolling), it is recommended to do cpu-intensive work in the cache() and cacheZoom() functions, and then re-use the data to make the draw() faster.

The default implementation returns an empty reference, meaning nothing is cached. As a result, the draw() function will receive an empty reference.

Parameters
tilethe Tile for which caching is requested
zoomthe requested zoom level
cachethe output of cache(const ConstTile::Ptr)

Reimplemented from LayerOperations.

Reimplemented in Operations1bppClipped.

128{
129 // In: Cairo surface at zoom level 0
130 // Out: Cairo surface at requested zoom level
132 if(zoom >= 0)
133 {
134 // Don't zoom in. It is a waste of space
135 result = cache;
136 }
137 else if(!cache)
138 {
139 defect_message("Base caching failed to return anything");
140 }
141 else
142 {
143 const int divider = 1 << -zoom;
144 BitmapSurface::Ptr const source = std::static_pointer_cast<BitmapSurface>(cache);
145 BitmapSurface::Ptr const target = BitmapSurface::create(tile->width / divider, tile->height / divider, CAIRO_FORMAT_ARGB32);
146 result = target;
147
148 cairo_surface_t* surface = target->get();
149 cairo_t* cr = cairo_create(surface);
151 cairo_scale(cr, 1.0 / divider, 1.0 / divider);
152 cairo_set_source_surface(cr, source->get(), 0, 0);
153 cairo_paint(cr);
154
156 }
157
158 return result;
159}
#define defect_message(m)
Definition assertions.hh:49
void initializeCairo(cairo_t *cr) override
Definition layeroperations.cc:66
virtual Scroom::Utils::Stuff cache(const ConstTile::Ptr &)
Definition tiledbitmapinterface.hh:127
std::shared_ptr< BitmapSurface > Ptr
Definition bitmap-helpers.hh:25
static Ptr create(int width, int height, cairo_format_t format)
Definition bitmap-helpers.cc:13
std::shared_ptr< void > Stuff
Definition stuff.hh:17
SampleIterator< const uint8_t > result
Definition sampleiterator-tests.cc:94
cairo_destroy(cr)
cairo_surface_t * surface
Definition transformpresentation_test.cc:71
cairo_t * cr
Definition transformpresentation_test.cc:72
Here is the call graph for this function:

◆ draw()

void CommonOperations::draw ( cairo_t *  cr,
const ConstTile::Ptr tile,
Scroom::Utils::Rectangle< double >  tileArea,
Scroom::Utils::Rectangle< double >  viewArea,
int  zoom,
Scroom::Utils::Stuff  cache 
)
overridevirtual

Draw the given tileArea into the given viewArea at the requested zoom level

Parameters
crThe canvas on which to draw
tileThe tile to take data from
tileAreaArea of the tile that needs to be drawn
viewAreaArea of the canvas that the tile needs to be drawn in
zoomThe requested zoom level. One pixel of your presentation should have size 2**zoom when drawn. zoom will be negative for all but the first layer.
cacheDepending on whether the cacheZoom() function finished already, this may either be an empty reference, or a reference to the value returned by cacheZoom()

Implements LayerOperations.

Reimplemented in Operations1bpp, Operations8bpp, and Operations.

169{
170 // In: Cairo surface at requested zoom level
171 // Out: given surface rendered to the canvas
172 setClip(cr, viewArea);
173
174 if(!cache)
175 {
176 drawState(cr, TILE_UNLOADED, viewArea);
177 }
178 else
179 {
180 BitmapSurface::Ptr const source = std::static_pointer_cast<BitmapSurface>(cache);
181
182 if(zoom > 0)
183 {
184 // Ask Cairo to zoom in for us
185 const int multiplier = 1 << zoom;
186 auto origin = viewArea.getTopLeft() / multiplier - tileArea.getTopLeft();
187
188 cairo_scale(cr, multiplier, multiplier);
189 cairo_set_source_surface(cr, source->get(), origin.x, origin.y);
190 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);
191 cairo_paint(cr);
192 }
193 else
194 {
195 // Cached bitmap is to scale
196 const int divider = 1 << -zoom;
197
198 auto origin = viewArea.getTopLeft() - tileArea.getTopLeft() / divider;
199
200 cairo_set_source_surface(cr, source->get(), origin.x, origin.y);
201 cairo_paint(cr);
202 }
203 }
204}
void setClip(cairo_t *cr, double x, double y, double width, double height)
Definition cairo-helpers.cc:47
void drawState(cairo_t *cr, TileState s, Scroom::Utils::Rectangle< double > viewArea) override
Definition layeroperations.cc:72
xy_type getTopLeft() const
Definition rectangle.hh:133
@ TILE_UNLOADED
Definition tiledbitmapinterface.hh:34

Referenced by Operations1bpp::draw(), Operations8bpp::draw(), and Operations::draw().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawPixelValue() [1/2]

void CommonOperations::drawPixelValue ( cairo_t *  cr,
int  x,
int  y,
int  size,
int  value 
)
static
97{
98 std::ostringstream s;
99 s << value;
100 const std::string v = s.str();
101 const char* cstr = v.c_str();
102
103 cairo_move_to(cr, x, y);
104 cairo_line_to(cr, x + size, y);
105 cairo_line_to(cr, x + size, y + size);
106 cairo_line_to(cr, x, y + size);
107 cairo_line_to(cr, x, y);
108 cairo_clip(cr);
109 // cairo_stroke(cr);
110
111 cairo_text_extents_t extents;
112 cairo_text_extents(cr, cstr, &extents);
113
114 const double xx = x + size / 2 - (extents.width / 2 + extents.x_bearing);
115 const double yy = y + size / 2 - (extents.height / 2 + extents.y_bearing);
116
117 cairo_move_to(cr, xx, yy);
118 cairo_show_text(cr, cstr);
119}
const uint8_t value
Definition blob-tests.cc:114
BitmapSurface::Ptr const s
Definition transformpresentation_test.cc:70

Referenced by Operations1bpp::draw(), Operations8bpp::draw(), Operations::draw(), and drawPixelValue().

Here is the caller graph for this function:

◆ drawPixelValue() [2/2]

void CommonOperations::drawPixelValue ( cairo_t *  cr,
int  x,
int  y,
int  size,
int  value,
Color const &  bgColor 
)
static
122{
123 bgColor.getContrastingBlackOrWhite().setColor(cr);
124 drawPixelValue(cr, x, y, size, value);
125}
static void drawPixelValue(cairo_t *cr, int x, int y, int size, int value)
Definition layeroperations.cc:96
Here is the call graph for this function:

◆ drawState()

void CommonOperations::drawState ( cairo_t *  cr,
TileState  s,
Scroom::Utils::Rectangle< double >  viewArea 
)
overridevirtual

Draw the given state into the given viewArea

The associated tile is likely not loaded or not initialized or in whatever other TileState that doesn't allow its contents to be drawn.

Something needs to be drawn in the given viewArea anyway. Implementors might want to just draw an empty rectangle, maybe color-coded to reflect the state of the tile.

Implements LayerOperations.

73{
74 Color c;
75
76 switch(s)
77 {
79 c = Color(1, 1, 0.5); // Yellow
80 break;
81 case TILE_UNLOADED:
82 c = Color(0.5, 1, 0.5); // Green
83 break;
84 case TILE_LOADED:
85 c = Color(1, 0.5, 0.5); // Red
86 break;
88 default:
90 break;
91 }
92
93 drawRectangle(cr, c, viewArea);
94}
void drawRectangle(cairo_t *cr, Color const &c, Scroom::Utils::Rectangle< double > const &viewArea)
Definition cairo-helpers.cc:62
Definition color.hh:37
const Color OUT_OF_BOUNDS
Semaphore c(0)
@ TILE_LOADED
Definition tiledbitmapinterface.hh:35
@ TILE_UNINITIALIZED
Definition tiledbitmapinterface.hh:33
@ TILE_OUT_OF_BOUNDS
Definition tiledbitmapinterface.hh:36

Referenced by draw().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initializeCairo()

void CommonOperations::initializeCairo ( cairo_t *  cr)
overridevirtual

Initialize the canvas for drawing the bitmap

When TiledBitmapInterface::redraw() is called, (a portion of) the Layer needs to be redrawn. TiledBitmap will compute which tiles are involved in the redraw, and call draw() or drawState() for each of them, as appropriate. However, before doing so, it will first call initializeCairo(). You can take this opportunity to set various properties that are needed in all subsequent calls to draw() and drawState(), such as anti-aliasing and line caps.

Implements LayerOperations.

67{
68 cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
69 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
70}

Referenced by cacheZoom().

Here is the caller graph for this function:

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