Scroom  0.14
Operations Class Reference

#include <layeroperations.hh>

Inheritance diagram for Operations:
Inheritance graph
Collaboration diagram for Operations:
Collaboration graph

Public Member Functions

 Operations (ColormapProvider::Ptr colormapProvider, int bpp)
 
int getBpp () override
 
Scroom::Utils::Stuff cache (const ConstTile::Ptr &tile) override
 
void reduce (Tile::Ptr target, ConstTile::Ptr source, int x, int y) 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 CommonOperations
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
 

Static Public Member Functions

static Ptr create (ColormapProvider::Ptr colormapProvider, int bpp)
 
- Static Public Member Functions inherited from CommonOperations
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)
 

Protected Attributes

ColormapProvider::Ptr colormapProvider
 
const unsigned bpp
 
const unsigned pixelsPerByte
 
const unsigned pixelOffset
 
const unsigned pixelMask
 

Additional Inherited Members

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

Constructor & Destructor Documentation

◆ Operations()

Operations::Operations ( ColormapProvider::Ptr  colormapProvider,
int  bpp 
)
580  : colormapProvider(std::move(colormapProvider_))
581  , bpp(bpp_)
582  , pixelsPerByte(8 / bpp_)
583  , pixelOffset(bpp_)
584  , pixelMask((1 << bpp_) - 1)
585 {
586 }

Referenced by create().

Here is the caller graph for this function:

Member Function Documentation

◆ cache()

Scroom::Utils::Stuff Operations::cache ( const ConstTile::Ptr tile)
overridevirtual

Cache data for use during later cacheZoom() calls.

This function is called for a given tile, and then the results are passed to the cacheZoom() 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

Reimplemented from LayerOperations.

Reimplemented in OperationsColormapped.

591 {
592  const int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, tile->width);
593  boost::shared_ptr<unsigned char> const data = shared_malloc(stride * tile->height);
594  Colormap::Ptr const colormap = colormapProvider->getColormap();
595 
596  unsigned char* row = data.get();
597  for(int j = 0; j < tile->height; j++, row += stride)
598  {
599  SampleIterator<const byte> pixelIn(tile->data.get() + j * tile->width / pixelsPerByte, 0, bpp);
600 
601  auto* pixelOut = reinterpret_cast<uint32_t*>(row);
602  for(int i = 0; i < tile->width; i++)
603  {
604  *pixelOut = colormap->colors[*pixelIn].getARGB32();
605  pixelOut++;
606  ++pixelIn;
607  }
608  }
609 
610  return BitmapSurface::create(tile->width, tile->height, CAIRO_FORMAT_ARGB32, stride, data);
611 }

Referenced by draw().

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

◆ create()

LayerOperations::Ptr Operations::create ( ColormapProvider::Ptr  colormapProvider,
int  bpp 
)
static
575 {
576  return Ptr(new Operations(std::move(colormapProvider), bpp));
577 }

Referenced by Scroom::TiledBitmap::ColormappedBitmap(), Scroom::TiledBitmap::GreyscaleBitmap(), setupTest2bpp(), setupTest4bpp(), and setupTest8bppColormapped().

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

◆ draw()

void Operations::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()

Reimplemented from CommonOperations.

683 {
684  cairo_save(cr);
685  CommonOperations::draw(cr, tile, tileArea, viewArea, zoom, cache);
686  cairo_restore(cr);
687 
688  // Draw pixelvalues at 32:1 zoom
689  if(zoom == 5)
690  {
691  const int multiplier = 1 << zoom;
692  const int stride = tile->width / pixelsPerByte;
693  cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
694  cairo_set_font_size(cr, 12.0);
695 
696  Colormap::Ptr const colormap = colormapProvider->getColormap();
697 
698  auto tileAreaInt = roundOutward(tileArea).to<int>();
699  auto offset = tileAreaInt.getTopLeft() - tileArea.getTopLeft();
700  viewArea += offset * multiplier;
701 
702  for(int y = 0; y < tileAreaInt.height(); y++)
703  {
704  const byte* const data = tile->data.get();
705  SampleIterator<const byte> current(data + (tileAreaInt.y() + y) * stride, tileAreaInt.x(), bpp);
706 
707  for(int x = 0; x < tileAreaInt.width(); x++, ++current)
708  {
709  const int value = *current;
710 
711  cairo_save(cr);
713  cr, viewArea.x() + multiplier * x, viewArea.y() + multiplier * y, multiplier, value, colormap->colors[value]);
714  cairo_restore(cr);
715  }
716  }
717  }
718 }
Here is the call graph for this function:

◆ getBpp()

int Operations::getBpp ( )
overridevirtual

Return the number of bits per pixel that the layer will use.

This number will be used to compute the amount of memory required to store one tile

Implements LayerOperations.

Reimplemented in OperationsColormapped.

588 { return bpp; }

◆ reduce()

void Operations::reduce ( Tile::Ptr  target,
ConstTile::Ptr  source,
int  x,
int  y 
)
overridevirtual

Reduce the source tile by a factor of 8

The target tile will contain data for 8*8 source tiles. Offsets x and y indicate which of those 64 source tiles is currently being processed

Parameters
targetTile that will contain the reduced bitmap
sourceTile that is to be reduced
xx-offset (0..7) of the source tile in the target tile
yy-offset (0..7) of the source tile in the target tile
Note
The target tile belongs to a different layer, and hence possibly has a different bpp than the current one, depending on the LayerSpec given to createTiledBitmap()

Implements LayerOperations.

Reimplemented in OperationsColormapped.

614 {
615  // Reducing by a factor 8. Target is 2*bpp and expects two indices into the colormap
616  const int sourceStride = source->width / pixelsPerByte;
617  const byte* sourceBase = source->data.get();
618 
619  const int targetMultiplier = 2; // target is 2*bpp
620  const int targetStride = targetMultiplier * target->width / pixelsPerByte;
621  byte* targetBase =
622  target->data.get() + target->height * targetStride * y / 8 + targetMultiplier * target->width * x / 8 / pixelsPerByte;
623 
624  for(int j = 0; j < source->height / 8; j++, targetBase += targetStride, sourceBase += sourceStride * 8)
625  {
626  // Iterate vertically over target
627  const byte* sourcePtr = sourceBase;
628  SampleIterator<uint16_t> targetPtr(reinterpret_cast<uint16_t*>(targetBase), 0, targetMultiplier * bpp);
629 
630  for(int i = 0; i < source->width / 8; i++, sourcePtr += 8 / pixelsPerByte, ++targetPtr)
631  {
632  // Iterate horizontally over target
633 
634  // Goal is to determine which values occurs most often in a 8*8
635  // rectangle, and pick the top two.
636  const byte* base = sourcePtr;
637  byte lookup[pixelMask + 1];
638  memset(lookup, 0, sizeof(lookup));
639 
640  for(int k = 0; k < 8; k++, base += sourceStride)
641  {
643  for(int l = 0; l < 8; l++, ++current)
644  {
645  ++(lookup[*current]);
646  }
647  }
648  unsigned first = 0;
649  unsigned second = 1;
650  if(lookup[1] > lookup[0])
651  {
652  first = 1;
653  second = 0;
654  }
655  for(unsigned c = 2; c < pixelMask + 1; c++)
656  {
657  if(lookup[c] > lookup[first])
658  {
659  second = first;
660  first = c;
661  }
662  else if(lookup[c] > lookup[second])
663  {
664  second = c;
665  }
666  }
667  if(lookup[second] == 0)
668  {
669  second = first;
670  }
671 
672  targetPtr.set(first << pixelOffset | second);
673  }
674  }
675 }
Here is the call graph for this function:

Member Data Documentation

◆ bpp

◆ colormapProvider

ColormapProvider::Ptr Operations::colormapProvider
protected

◆ pixelMask

const unsigned Operations::pixelMask
protected

◆ pixelOffset

const unsigned Operations::pixelOffset
protected

◆ pixelsPerByte

const unsigned Operations::pixelsPerByte
protected

The documentation for this class was generated from the following files:
Scroom::Utils::Rectangle::getTopLeft
xy_type getTopLeft() const
Definition: rectangle.hh:133
Scroom::Utils::Rectangle::x
value_type x() const
Definition: rectangle.hh:125
Operations::pixelOffset
const unsigned pixelOffset
Definition: layeroperations.hh:126
Operations::bpp
const unsigned bpp
Definition: layeroperations.hh:124
Operations::pixelsPerByte
const unsigned pixelsPerByte
Definition: layeroperations.hh:125
Scroom::Bitmap::SampleIterator
Definition: bitmap-helpers.hh:61
Operations::pixelMask
const unsigned pixelMask
Definition: layeroperations.hh:127
CommonOperations::draw
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
Definition: layeroperations.cc:161
current
static unsigned int current
Definition: measure-framerate-callbacks.cc:17
Operations::Operations
Operations(ColormapProvider::Ptr colormapProvider, int bpp)
Definition: layeroperations.cc:579
Colormap::Ptr
boost::shared_ptr< Colormap > Ptr
Definition: colormappable.hh:31
colormap
const Colormap::Ptr colormap
Definition: colormaphelpers_test.cc:54
CommonOperations::drawPixelValue
static void drawPixelValue(cairo_t *cr, int x, int y, int size, int value)
Definition: layeroperations.cc:96
LayerOperations::Ptr
boost::shared_ptr< LayerOperations > Ptr
Definition: tiledbitmapinterface.hh:53
Scroom::Utils::Rectangle::y
value_type y() const
Definition: rectangle.hh:127
anonymous_namespace{layeroperations.cc}::shared_malloc
boost::shared_ptr< unsigned char > shared_malloc(size_t size)
Definition: layeroperations.cc:28
Operations::colormapProvider
ColormapProvider::Ptr colormapProvider
Definition: layeroperations.hh:123
Operations::cache
Scroom::Utils::Stuff cache(const ConstTile::Ptr &tile) override
Definition: layeroperations.cc:590
Scroom::Utils::roundOutward
Segment< double > roundOutward(Segment< double > s)
Definition: linearsegment.hh:240
create
void create(NewPresentationInterface *interface)
Definition: loader.cc:175