Merge pull request #185 from jpirnay/feat-pr1775
feat: Add and refactor carousel theme (Upstream 1775 by zgredex)
This commit is contained in:
@@ -810,6 +810,9 @@ bool Epub::generateCoverBmp(bool cropped) const {
|
||||
|
||||
std::string Epub::getThumbBmpPath() const { return cachePath + "/thumb_[HEIGHT].bmp"; }
|
||||
std::string Epub::getThumbBmpPath(int height) const { return cachePath + "/thumb_" + std::to_string(height) + ".bmp"; }
|
||||
std::string Epub::getThumbBmpPath(int width, int height) const {
|
||||
return cachePath + "/thumb_" + std::to_string(width) + "x" + std::to_string(height) + ".bmp";
|
||||
}
|
||||
|
||||
bool Epub::generateThumbBmp(int height) const {
|
||||
// Already generated, return true
|
||||
@@ -902,6 +905,79 @@ bool Epub::generateThumbBmp(int height) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Epub::generateThumbBmp(int width, int height) const {
|
||||
if (Storage.exists(getThumbBmpPath(width, height).c_str())) return true;
|
||||
|
||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
|
||||
LOG_ERR("EBP", "Cannot generate thumb BMP, cache not loaded");
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto coverImageHref = bookMetadataCache->coreMetadata.coverItemHref;
|
||||
if (coverImageHref.empty()) {
|
||||
LOG_DBG("EBP", "No known cover image for thumbnail");
|
||||
} else {
|
||||
const auto coverTempPath = getCachePath() + "/.cover";
|
||||
FsFile coverTemp;
|
||||
if (!Storage.openFileForWrite("EBP", coverTempPath, coverTemp)) return false;
|
||||
if (!readItemContentsToStream(coverImageHref, coverTemp, 1024)) {
|
||||
coverTemp.close();
|
||||
Storage.remove(coverTempPath.c_str());
|
||||
return false;
|
||||
}
|
||||
coverTemp.close();
|
||||
|
||||
if (!Storage.openFileForRead("EBP", coverTempPath, coverTemp)) {
|
||||
Storage.remove(coverTempPath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto detectedFormat = detectCoverImageFormat(coverTemp);
|
||||
bool success = false;
|
||||
FsFile thumbBmp;
|
||||
if (!Storage.openFileForWrite("EBP", getThumbBmpPath(width, height), thumbBmp)) {
|
||||
coverTemp.close();
|
||||
Storage.remove(coverTempPath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (detectedFormat == CoverImageFormat::Jpeg) {
|
||||
LOG_DBG("EBP", "Generating %dx%d thumb BMP from JPEG cover image", width, height);
|
||||
success = JpegToBmpConverter::jpegFileTo1BitBmpStreamWithSize(coverTemp, thumbBmp, width, height);
|
||||
} else if (detectedFormat == CoverImageFormat::Png) {
|
||||
LOG_DBG("EBP", "Generating %dx%d thumb BMP from PNG cover image", width, height);
|
||||
success = PngToBmpConverter::pngFileTo1BitBmpStreamWithSize(coverTemp, thumbBmp, width, height);
|
||||
} else {
|
||||
LOG_DBG("EBP", "Cover image format unknown, attempting JPEG then PNG: %s", coverImageHref.c_str());
|
||||
success = JpegToBmpConverter::jpegFileTo1BitBmpStreamWithSize(coverTemp, thumbBmp, width, height);
|
||||
if (!success) {
|
||||
thumbBmp.close();
|
||||
Storage.remove(getThumbBmpPath(width, height).c_str());
|
||||
if (!Storage.openFileForWrite("EBP", getThumbBmpPath(width, height), thumbBmp)) {
|
||||
coverTemp.close();
|
||||
Storage.remove(coverTempPath.c_str());
|
||||
return false;
|
||||
}
|
||||
coverTemp.seek(0);
|
||||
success = PngToBmpConverter::pngFileTo1BitBmpStreamWithSize(coverTemp, thumbBmp, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
coverTemp.close();
|
||||
thumbBmp.close();
|
||||
Storage.remove(coverTempPath.c_str());
|
||||
if (!success) Storage.remove(getThumbBmpPath(width, height).c_str());
|
||||
LOG_DBG("EBP", "Generated %dx%d thumb BMP from cover image, success: %s", width, height, success ? "yes" : "no");
|
||||
return success;
|
||||
}
|
||||
|
||||
// Write empty sentinel to avoid repeated generation attempts
|
||||
FsFile thumbBmp;
|
||||
Storage.openFileForWrite("EBP", getThumbBmpPath(width, height), thumbBmp);
|
||||
thumbBmp.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t* Epub::readItemContentsToBytes(const std::string& itemHref, size_t* size, const bool trailingNullByte) const {
|
||||
if (itemHref.empty()) {
|
||||
LOG_DBG("EBP", "Failed to read item, empty href");
|
||||
|
||||
@@ -62,7 +62,9 @@ class Epub {
|
||||
bool generateCoverBmp(bool cropped = false) const;
|
||||
std::string getThumbBmpPath() const;
|
||||
std::string getThumbBmpPath(int height) const;
|
||||
std::string getThumbBmpPath(int width, int height) const;
|
||||
bool generateThumbBmp(int height) const;
|
||||
bool generateThumbBmp(int width, int height) const;
|
||||
uint8_t* readItemContentsToBytes(const std::string& itemHref, size_t* size = nullptr,
|
||||
bool trailingNullByte = false) const;
|
||||
bool readItemContentsToStream(const std::string& itemHref, Print& out, size_t chunkSize) const;
|
||||
|
||||
@@ -1482,6 +1482,61 @@ void GfxRenderer::drawIcon(const uint8_t bitmap[], const int x, const int y, con
|
||||
display.drawImageTransparent(bitmap, y, getScreenWidth() - width - x, height, width);
|
||||
}
|
||||
|
||||
void GfxRenderer::drawIconInverted(const uint8_t bitmap[], const int x, const int y, const int width,
|
||||
const int height) const {
|
||||
// Portrait-mode coordinate transform (x↔y swap), matching drawIcon.
|
||||
// OR with ~srcByte sets framebuffer bits to 1 (white) wherever the icon
|
||||
// bitmap is 0 (black) — produces a white icon on a black background.
|
||||
const int physX = y;
|
||||
const int physY = getScreenWidth() - width - x;
|
||||
const int imgW = height; // dimensions swapped by portrait transform
|
||||
const int imgH = width;
|
||||
const int srcStride = (imgW + 7) / 8;
|
||||
|
||||
if (physX + imgW <= 0 || physX >= static_cast<int>(panelWidthBytes) * 8) return;
|
||||
if (physY + imgH <= 0 || physY >= static_cast<int>(panelHeight)) return;
|
||||
|
||||
const int baseByte = (physX >= 0) ? (physX >> 3) : -(((-physX) + 7) >> 3);
|
||||
const int bitShift = ((physX % 8) + 8) % 8;
|
||||
|
||||
const int trail = srcStride * 8 - imgW;
|
||||
const uint8_t trailMask = static_cast<uint8_t>(0xFF << trail);
|
||||
const int lastCol = srcStride - 1;
|
||||
|
||||
for (int row = 0; row < imgH; ++row) {
|
||||
const int destY = physY + row;
|
||||
if (destY < 0 || destY >= static_cast<int>(panelHeight)) continue;
|
||||
const int rowBase = destY * static_cast<int>(panelWidthBytes);
|
||||
const int srcOffset = row * srcStride;
|
||||
|
||||
if (bitShift == 0) {
|
||||
for (int col = 0; col < srcStride; ++col) {
|
||||
const int dst = baseByte + col;
|
||||
if (dst < 0) continue;
|
||||
if (dst >= static_cast<int>(panelWidthBytes)) break;
|
||||
uint8_t inv = ~bitmap[srcOffset + col];
|
||||
if (col == lastCol && trail > 0) inv &= trailMask;
|
||||
frameBuffer[rowBase + dst] |= inv;
|
||||
}
|
||||
} else {
|
||||
const int rsh = bitShift;
|
||||
const int lsh = 8 - bitShift;
|
||||
for (int col = 0; col < srcStride; ++col) {
|
||||
uint8_t inv = ~bitmap[srcOffset + col];
|
||||
if (col == lastCol && trail > 0) inv &= trailMask;
|
||||
const int dstHi = baseByte + col;
|
||||
const int dstLo = dstHi + 1;
|
||||
if (dstHi >= 0 && dstHi < static_cast<int>(panelWidthBytes)) {
|
||||
frameBuffer[rowBase + dstHi] |= static_cast<uint8_t>(inv >> rsh);
|
||||
}
|
||||
if (dstLo >= 0 && dstLo < static_cast<int>(panelWidthBytes)) {
|
||||
frameBuffer[rowBase + dstLo] |= static_cast<uint8_t>(inv << lsh);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, const int maxWidth, const int maxHeight,
|
||||
const float cropX, const float cropY) const {
|
||||
if (fontCacheManager_ && fontCacheManager_->isScanning()) return;
|
||||
@@ -1495,8 +1550,6 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
||||
bool isScaled = false;
|
||||
int cropPixX = std::floor(bitmap.getWidth() * cropX / 2.0f);
|
||||
int cropPixY = std::floor(bitmap.getHeight() * cropY / 2.0f);
|
||||
LOG_DBG("GFX", "Cropping %dx%d by %dx%d pix, is %s", bitmap.getWidth(), bitmap.getHeight(), cropPixX, cropPixY,
|
||||
bitmap.isTopDown() ? "top-down" : "bottom-up");
|
||||
|
||||
const float croppedWidth = (1.0f - cropX) * static_cast<float>(bitmap.getWidth());
|
||||
const float croppedHeight = (1.0f - cropY) * static_cast<float>(bitmap.getHeight());
|
||||
@@ -1518,7 +1571,6 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
||||
scale = fitScale;
|
||||
isScaled = true;
|
||||
}
|
||||
LOG_DBG("GFX", "Scaling by %f - %s", scale, isScaled ? "scaled" : "not scaled");
|
||||
|
||||
// Calculate output row size (2 bits per pixel, packed into bytes)
|
||||
// IMPORTANT: Use int, not uint8_t, to avoid overflow for images > 1020 pixels wide
|
||||
@@ -1595,13 +1647,19 @@ void GfxRenderer::drawBitmap1Bit(const Bitmap& bitmap, const int x, const int y,
|
||||
const int maxHeight) const {
|
||||
float scale = 1.0f;
|
||||
bool isScaled = false;
|
||||
if (maxWidth > 0 && bitmap.getWidth() > maxWidth) {
|
||||
scale = static_cast<float>(maxWidth) / static_cast<float>(bitmap.getWidth());
|
||||
isScaled = true;
|
||||
if (maxWidth > 0) {
|
||||
const float s = static_cast<float>(maxWidth) / static_cast<float>(bitmap.getWidth());
|
||||
if (s != 1.0f) {
|
||||
scale = s;
|
||||
isScaled = true;
|
||||
}
|
||||
}
|
||||
if (maxHeight > 0 && bitmap.getHeight() > maxHeight) {
|
||||
scale = std::min(scale, static_cast<float>(maxHeight) / static_cast<float>(bitmap.getHeight()));
|
||||
isScaled = true;
|
||||
if (maxHeight > 0) {
|
||||
const float s = static_cast<float>(maxHeight) / static_cast<float>(bitmap.getHeight());
|
||||
if (s < scale || (scale == 1.0f && s != 1.0f)) {
|
||||
scale = s;
|
||||
isScaled = (scale != 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
// For 1-bit BMP, output is still 2-bit packed (for consistency with readNextRow)
|
||||
|
||||
@@ -155,6 +155,7 @@ class GfxRenderer {
|
||||
bool roundBottomLeft, bool roundBottomRight, Color color) const;
|
||||
void drawImage(const uint8_t bitmap[], int x, int y, int width, int height) const;
|
||||
void drawIcon(const uint8_t bitmap[], int x, int y, int width, int height) const;
|
||||
void drawIconInverted(const uint8_t bitmap[], int x, int y, int width, int height) const;
|
||||
void drawBitmap(const Bitmap& bitmap, int x, int y, int maxWidth, int maxHeight, float cropX = 0,
|
||||
float cropY = 0) const;
|
||||
void drawBitmap1Bit(const Bitmap& bitmap, int x, int y, int maxWidth, int maxHeight) const;
|
||||
|
||||
@@ -326,6 +326,7 @@ STR_UI_THEME: "UI Theme"
|
||||
STR_THEME_CLASSIC: "Classic"
|
||||
STR_THEME_LYRA: "Lyra"
|
||||
STR_THEME_LYRA_EXTENDED: "Lyra Extended"
|
||||
STR_THEME_LYRA_CAROUSEL: "Lyra Carousel"
|
||||
STR_SUNLIGHT_FADING_FIX: "Sunlight Fading Fix"
|
||||
STR_REMAP_FRONT_BUTTONS: "Remap Front Buttons"
|
||||
STR_OPDS_BROWSER: "OPDS Browser"
|
||||
|
||||
+40
-78
@@ -262,62 +262,63 @@ bool Xtc::generateCoverBmp() const {
|
||||
|
||||
std::string Xtc::getThumbBmpPath() const { return cachePath + "/thumb_[HEIGHT].bmp"; }
|
||||
std::string Xtc::getThumbBmpPath(int height) const { return cachePath + "/thumb_" + std::to_string(height) + ".bmp"; }
|
||||
std::string Xtc::getThumbBmpPath(int width, int height) const {
|
||||
return cachePath + "/thumb_" + std::to_string(width) + "x" + std::to_string(height) + ".bmp";
|
||||
}
|
||||
|
||||
bool Xtc::generateThumbBmp(int height) const {
|
||||
// Already generated
|
||||
if (Storage.exists(getThumbBmpPath(height).c_str())) {
|
||||
return true;
|
||||
}
|
||||
const std::string destPath = getThumbBmpPath(height);
|
||||
if (Storage.exists(destPath.c_str())) return true;
|
||||
const int width = static_cast<int>(height * 0.6f);
|
||||
if (!generateThumbBmp(width, height)) return false;
|
||||
const std::string srcPath = getThumbBmpPath(width, height);
|
||||
Storage.rename(srcPath.c_str(), destPath.c_str());
|
||||
return Storage.exists(destPath.c_str());
|
||||
}
|
||||
|
||||
bool Xtc::generateThumbBmp(int width, int height) const {
|
||||
if (Storage.exists(getThumbBmpPath(width, height).c_str())) return true;
|
||||
|
||||
if (!loaded || !parser) {
|
||||
LOG_ERR("XTC", "Cannot generate thumb BMP, file not loaded");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (parser->getPageCount() == 0) {
|
||||
LOG_ERR("XTC", "No pages in XTC file");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup cache directory
|
||||
setupCacheDir();
|
||||
|
||||
// Get first page info for cover
|
||||
xtc::PageInfo pageInfo;
|
||||
if (!parser->getPageInfo(0, pageInfo)) {
|
||||
LOG_DBG("XTC", "Failed to get first page info");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get bit depth
|
||||
const uint8_t bitDepth = parser->getBitDepth();
|
||||
const int THUMB_TARGET_WIDTH = width;
|
||||
const int THUMB_TARGET_HEIGHT = height;
|
||||
|
||||
// Calculate target dimensions for thumbnail (fit within 240x400 Continue Reading card)
|
||||
int THUMB_TARGET_WIDTH = height * 0.6;
|
||||
int THUMB_TARGET_HEIGHT = height;
|
||||
|
||||
// Calculate scale factor
|
||||
float scaleX = static_cast<float>(THUMB_TARGET_WIDTH) / pageInfo.width;
|
||||
float scaleY = static_cast<float>(THUMB_TARGET_HEIGHT) / pageInfo.height;
|
||||
float scale = (scaleX > scaleY) ? scaleX : scaleY; // for cropping
|
||||
float scale = (scaleX < scaleY) ? scaleX : scaleY;
|
||||
|
||||
// Only scale down, never up
|
||||
if (scale >= 1.0f) {
|
||||
// Page is already small enough, just use cover.bmp
|
||||
// Copy cover.bmp to thumb.bmp
|
||||
if (generateCoverBmp()) {
|
||||
FsFile src, dst;
|
||||
if (Storage.openFileForRead("XTC", getCoverBmpPath(), src)) {
|
||||
if (Storage.openFileForWrite("XTC", getThumbBmpPath(height), dst)) {
|
||||
if (Storage.openFileForWrite("XTC", getThumbBmpPath(width, height), dst)) {
|
||||
uint8_t buffer[512];
|
||||
while (src.available()) {
|
||||
size_t bytesRead = src.read(buffer, sizeof(buffer));
|
||||
dst.write(buffer, bytesRead);
|
||||
}
|
||||
dst.close();
|
||||
}
|
||||
src.close();
|
||||
}
|
||||
LOG_DBG("XTC", "Copied cover to thumb (no scaling needed)");
|
||||
return Storage.exists(getThumbBmpPath(height).c_str());
|
||||
return Storage.exists(getThumbBmpPath(width, height).c_str());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -325,10 +326,6 @@ bool Xtc::generateThumbBmp(int height) const {
|
||||
uint16_t thumbWidth = static_cast<uint16_t>(pageInfo.width * scale);
|
||||
uint16_t thumbHeight = static_cast<uint16_t>(pageInfo.height * scale);
|
||||
|
||||
LOG_DBG("XTC", "Generating thumb BMP: %dx%d -> %dx%d (scale: %.3f)", pageInfo.width, pageInfo.height, thumbWidth,
|
||||
thumbHeight, scale);
|
||||
|
||||
// Allocate buffer for page data
|
||||
size_t bitmapSize;
|
||||
if (bitDepth == 2) {
|
||||
bitmapSize = ((static_cast<size_t>(pageInfo.width) * pageInfo.height + 7) / 8) * 2;
|
||||
@@ -341,7 +338,6 @@ bool Xtc::generateThumbBmp(int height) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load first page (cover)
|
||||
size_t bytesRead = const_cast<xtc::XtcParser*>(parser.get())->loadPage(0, pageBuffer, bitmapSize);
|
||||
if (bytesRead == 0) {
|
||||
LOG_ERR("XTC", "Failed to load cover page for thumb");
|
||||
@@ -349,32 +345,27 @@ bool Xtc::generateThumbBmp(int height) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create thumbnail BMP file - use 1-bit format for fast home screen rendering (no gray passes)
|
||||
const std::string thumbPath = getThumbBmpPath(width, height);
|
||||
FsFile thumbBmp;
|
||||
if (!Storage.openFileForWrite("XTC", getThumbBmpPath(height), thumbBmp)) {
|
||||
LOG_DBG("XTC", "Failed to create thumb BMP file");
|
||||
if (!Storage.openFileForWrite("XTC", thumbPath, thumbBmp)) {
|
||||
free(pageBuffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write 1-bit BMP header (top-down row order)
|
||||
const uint32_t rowSize = (thumbWidth + 31) / 32 * 4;
|
||||
BmpHeader bmpHeader;
|
||||
createBmpHeader(&bmpHeader, thumbWidth, thumbHeight, BmpRowOrder::TopDown);
|
||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&bmpHeader), sizeof(bmpHeader));
|
||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&bmpHeader), sizeof(BmpHeader));
|
||||
|
||||
const uint32_t rowSize = (thumbWidth + 31) / 32 * 4;
|
||||
|
||||
// Allocate row buffer for 1-bit output
|
||||
uint8_t* rowBuffer = static_cast<uint8_t*>(malloc(rowSize));
|
||||
if (!rowBuffer) {
|
||||
free(pageBuffer);
|
||||
thumbBmp.close();
|
||||
Storage.remove(thumbPath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fixed-point scale factor (16.16)
|
||||
uint32_t scaleInv_fp = static_cast<uint32_t>(65536.0f / scale);
|
||||
|
||||
// Pre-calculate plane info for 2-bit mode
|
||||
const size_t planeSize = (bitDepth == 2) ? ((static_cast<size_t>(pageInfo.width) * pageInfo.height + 7) / 8) : 0;
|
||||
const uint8_t* plane1 = (bitDepth == 2) ? pageBuffer : nullptr;
|
||||
const uint8_t* plane2 = (bitDepth == 2) ? pageBuffer + planeSize : nullptr;
|
||||
@@ -382,9 +373,7 @@ bool Xtc::generateThumbBmp(int height) const {
|
||||
const size_t srcRowBytes = (bitDepth == 1) ? ((pageInfo.width + 7) / 8) : 0;
|
||||
|
||||
for (uint16_t dstY = 0; dstY < thumbHeight; dstY++) {
|
||||
memset(rowBuffer, 0xFF, rowSize); // Start with all white (bit 1)
|
||||
|
||||
// Calculate source Y range with bounds checking
|
||||
memset(rowBuffer, 0xFF, rowSize);
|
||||
uint32_t srcYStart = (static_cast<uint32_t>(dstY) * scaleInv_fp) >> 16;
|
||||
uint32_t srcYEnd = (static_cast<uint32_t>(dstY + 1) * scaleInv_fp) >> 16;
|
||||
if (srcYStart >= pageInfo.height) srcYStart = pageInfo.height - 1;
|
||||
@@ -393,7 +382,6 @@ bool Xtc::generateThumbBmp(int height) const {
|
||||
if (srcYEnd > pageInfo.height) srcYEnd = pageInfo.height;
|
||||
|
||||
for (uint16_t dstX = 0; dstX < thumbWidth; dstX++) {
|
||||
// Calculate source X range with bounds checking
|
||||
uint32_t srcXStart = (static_cast<uint32_t>(dstX) * scaleInv_fp) >> 16;
|
||||
uint32_t srcXEnd = (static_cast<uint32_t>(dstX + 1) * scaleInv_fp) >> 16;
|
||||
if (srcXStart >= pageInfo.width) srcXStart = pageInfo.width - 1;
|
||||
@@ -401,82 +389,56 @@ bool Xtc::generateThumbBmp(int height) const {
|
||||
if (srcXEnd <= srcXStart) srcXEnd = srcXStart + 1;
|
||||
if (srcXEnd > pageInfo.width) srcXEnd = pageInfo.width;
|
||||
|
||||
// Area averaging: sum grayscale values (0-255 range)
|
||||
uint32_t graySum = 0;
|
||||
uint32_t totalCount = 0;
|
||||
|
||||
uint32_t graySum = 0, totalCount = 0;
|
||||
for (uint32_t srcY = srcYStart; srcY < srcYEnd && srcY < pageInfo.height; srcY++) {
|
||||
for (uint32_t srcX = srcXStart; srcX < srcXEnd && srcX < pageInfo.width; srcX++) {
|
||||
uint8_t grayValue = 255; // Default: white
|
||||
|
||||
uint8_t grayValue = 255;
|
||||
if (bitDepth == 2) {
|
||||
// XTH 2-bit mode: pixel value 0-3
|
||||
// Bounds check for column index
|
||||
if (srcX < pageInfo.width) {
|
||||
const size_t colIndex = pageInfo.width - 1 - srcX;
|
||||
const size_t byteInCol = srcY / 8;
|
||||
const size_t bitInByte = 7 - (srcY % 8);
|
||||
const size_t byteOffset = colIndex * colBytes + byteInCol;
|
||||
// Bounds check for buffer access
|
||||
if (byteOffset < planeSize) {
|
||||
const uint8_t bit1 = (plane1[byteOffset] >> bitInByte) & 1;
|
||||
const uint8_t bit2 = (plane2[byteOffset] >> bitInByte) & 1;
|
||||
const uint8_t pixelValue = (bit1 << 1) | bit2;
|
||||
// Convert 2-bit (0-3) to grayscale: 0=black, 3=white
|
||||
// pixelValue: 0=white, 1=light gray, 2=dark gray, 3=black (XTC polarity)
|
||||
grayValue = (3 - pixelValue) * 85; // 0->255, 1->170, 2->85, 3->0
|
||||
grayValue = (3 - ((bit1 << 1) | bit2)) * 85;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 1-bit mode
|
||||
const size_t byteIdx = srcY * srcRowBytes + srcX / 8;
|
||||
const size_t bitIdx = 7 - (srcX % 8);
|
||||
// Bounds check for buffer access
|
||||
if (byteIdx < bitmapSize) {
|
||||
const uint8_t pixelBit = (pageBuffer[byteIdx] >> bitIdx) & 1;
|
||||
// XTC 1-bit polarity: 0=black, 1=white (same as BMP palette)
|
||||
grayValue = pixelBit ? 255 : 0;
|
||||
grayValue = ((pageBuffer[byteIdx] >> bitIdx) & 1) ? 255 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
graySum += grayValue;
|
||||
totalCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate average grayscale and quantize to 1-bit with noise dithering
|
||||
uint8_t avgGray = (totalCount > 0) ? static_cast<uint8_t>(graySum / totalCount) : 255;
|
||||
|
||||
// Hash-based noise dithering for 1-bit output
|
||||
uint32_t hash = static_cast<uint32_t>(dstX) * 374761393u + static_cast<uint32_t>(dstY) * 668265263u;
|
||||
hash = (hash ^ (hash >> 13)) * 1274126177u;
|
||||
const int threshold = static_cast<int>(hash >> 24); // 0-255
|
||||
const int adjustedThreshold = 128 + ((threshold - 128) / 2); // Range: 64-192
|
||||
|
||||
// Quantize to 1-bit: 0=black, 1=white
|
||||
const int threshold = static_cast<int>(hash >> 24);
|
||||
const int adjustedThreshold = 128 + ((threshold - 128) / 2);
|
||||
uint8_t oneBit = (avgGray >= adjustedThreshold) ? 1 : 0;
|
||||
|
||||
// Pack 1-bit value into row buffer (MSB first, 8 pixels per byte)
|
||||
const size_t byteIndex = dstX / 8;
|
||||
const size_t bitOffset = 7 - (dstX % 8);
|
||||
// Bounds check for row buffer access
|
||||
if (byteIndex < rowSize) {
|
||||
if (oneBit) {
|
||||
rowBuffer[byteIndex] |= (1 << bitOffset); // Set bit for white
|
||||
} else {
|
||||
rowBuffer[byteIndex] &= ~(1 << bitOffset); // Clear bit for black
|
||||
}
|
||||
if (oneBit)
|
||||
rowBuffer[byteIndex] |= (1 << bitOffset);
|
||||
else
|
||||
rowBuffer[byteIndex] &= ~(1 << bitOffset);
|
||||
}
|
||||
}
|
||||
|
||||
// Write row (already padded to 4-byte boundary by rowSize)
|
||||
thumbBmp.write(rowBuffer, rowSize);
|
||||
}
|
||||
|
||||
free(rowBuffer);
|
||||
thumbBmp.close();
|
||||
free(pageBuffer);
|
||||
|
||||
LOG_DBG("XTC", "Generated thumb BMP (%dx%d): %s", thumbWidth, thumbHeight, getThumbBmpPath(height).c_str());
|
||||
LOG_DBG("XTC", "Generated thumb BMP (%dx%d): %s", thumbWidth, thumbHeight, getThumbBmpPath(width, height).c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,9 @@ class Xtc {
|
||||
// Thumbnail support (for Continue Reading card)
|
||||
std::string getThumbBmpPath() const;
|
||||
std::string getThumbBmpPath(int height) const;
|
||||
std::string getThumbBmpPath(int width, int height) const;
|
||||
bool generateThumbBmp(int height) const;
|
||||
bool generateThumbBmp(int width, int height) const;
|
||||
|
||||
// Page access
|
||||
uint32_t getPageCount() const;
|
||||
|
||||
Reference in New Issue
Block a user