renderbug-cpp/src/JsonCoordinateMapping.cpp

71 lines
2.0 KiB
C++

#include "./JsonCoordinateMapping.h"
unsigned int
JsonCoordinateMapping::physicalPixelCount() const {
int total = 0;
for(int i = 0; i < strideCount; i++) {
total += displayMap[i].length;
}
return total;
}
int
JsonCoordinateMapping::physicalCoordsToIndex(const PhysicalCoordinates localCoords) const {
uint8_t idx = 0;
bool inverse = false;
for(int i = 0; i < localCoords.x; i++) {
idx += displayMap[i].length;
inverse = !inverse;
}
if (inverse) {
idx += std::max(0, displayMap[localCoords.x].length - 1 - std::max(0, (int)localCoords.y - displayMap[localCoords.x].y));
} else {
idx += std::min((int)displayMap[localCoords.x].length - 1, std::max(0, (int)localCoords.y - displayMap[localCoords.x].y));
}
return idx;
}
PhysicalCoordinates
JsonCoordinateMapping::virtualToPhysicalCoords(const VirtualCoordinates virtualCoords) const {
const uint8_t spanIdx = scale8(strideCount-1, virtualCoords.x);
const uint8_t spanOffset = scale8(maxStrideSize - 1, virtualCoords.y);
return PhysicalCoordinates{spanIdx, spanOffset};
}
VirtualCoordinates
JsonCoordinateMapping::physicalToVirtualCoords(const PhysicalCoordinates localCoords) const {
int offset = localCoords.x;
for(int i = 0; i < strideCount; i++) {
if (offset > displayMap[i].length) {
offset -= displayMap[i].length;
} else {
return VirtualCoordinates{i, offset};
}
}
return VirtualCoordinates{0, 0};
}
void
JsonCoordinateMapping::load(const JsonArray& strideList)
{
maxStrideSize = 0;
strideCount = strideList.size();
for(int i = 0; i < strideList.size();i++) {
JsonObject strideObj = strideList[i];
int length = strideObj["pixels"];
int x = strideObj["x"];
int y = strideObj["y"];
displayMap[i] = Span{length, x, y};
Log.verbose("config: Found stride (%d, %d): %d", x, y, length);
maxStrideSize = length > maxStrideSize ? length : maxStrideSize;
}
}
void
JsonCoordinateMapping::loadDefault()
{
displayMap[0] = Span{255, 0, 0};
maxStrideSize = 255;
strideCount = 1;
}