bool RenderDocument(BaseEngine *engine, const WCHAR *renderPath, float zoom=1.f, bool silent=false) { if (!CheckRenderPath(renderPath)) return false; if (str::EndsWithI(renderPath, L".txt")) { str::Str<WCHAR> text(1024); for (int pageNo = 1; pageNo <= engine->PageCount(); pageNo++) text.AppendAndFree(engine->ExtractPageText(pageNo, L"\r\n", NULL, Target_Export)); if (silent) return true; ScopedMem<WCHAR> txtFilePath(str::Format(renderPath, 0)); ScopedMem<char> textUTF8(str::conv::ToUtf8(text.Get())); ScopedMem<char> textUTF8BOM(str::Join(UTF8_BOM, textUTF8)); return file::WriteAll(txtFilePath, textUTF8BOM, str::Len(textUTF8BOM)); } if (str::EndsWithI(renderPath, L".pdf")) { if (silent) return false; ScopedMem<WCHAR> pdfFilePath(str::Format(renderPath, 0)); return engine->SaveFileAsPDF(pdfFilePath, true) || PdfCreator::RenderToFile(pdfFilePath, engine); } bool success = true; for (int pageNo = 1; pageNo <= engine->PageCount(); pageNo++) { RenderedBitmap *bmp = engine->RenderBitmap(pageNo, zoom, 0); success &= bmp != NULL; if (!bmp && !silent) ErrOut("Error: Failed to render page %d for %s!", pageNo, engine->FileName()); if (!bmp || silent) { delete bmp; continue; } ScopedMem<WCHAR> pageBmpPath(str::Format(renderPath, pageNo)); if (str::EndsWithI(pageBmpPath, L".png")) { Bitmap gbmp(bmp->GetBitmap(), NULL); CLSID pngEncId = GetEncoderClsid(L"image/png"); gbmp.Save(pageBmpPath, &pngEncId); } else if (str::EndsWithI(pageBmpPath, L".bmp")) { size_t bmpDataLen; ScopedMem<char> bmpData((char *)SerializeBitmap(bmp->GetBitmap(), &bmpDataLen)); if (bmpData) file::WriteAll(pageBmpPath, bmpData, bmpDataLen); } else { // render as TGA for all other file extensions size_t tgaDataLen; ScopedMem<unsigned char> tgaData(tga::SerializeBitmap(bmp->GetBitmap(), &tgaDataLen)); if (tgaData) file::WriteAll(pageBmpPath, tgaData, tgaDataLen); } delete bmp; } return success; }
static void bg_update_proc(Layer *layer, GContext *ctx) { if(!is_encoding) { is_encoding = true; GBitmap *fb = graphics_capture_frame_buffer(ctx); APP_LOG(APP_LOG_LEVEL_DEBUG, "Captured framebuffer"); bmpData(fb); APP_LOG(APP_LOG_LEVEL_DEBUG, "Converted bitmap"); png_encode((const unsigned char*)imagedata); APP_LOG(APP_LOG_LEVEL_DEBUG, "Encoded PNG"); graphics_release_frame_buffer(ctx, fb); APP_LOG(APP_LOG_LEVEL_DEBUG, "Released framebuffer"); //is_encoding = false; } }
void UpdateBitmapColors(HBITMAP hbmp, COLORREF textColor, COLORREF bgColor) { if ((textColor & 0xFFFFFF) == WIN_COL_BLACK && (bgColor & 0xFFFFFF) == WIN_COL_WHITE) return; // color order in DIB is blue-green-red-alpha int base[4] = { GetBValueSafe(textColor), GetGValueSafe(textColor), GetRValueSafe(textColor), 0 }; int diff[4] = { GetBValueSafe(bgColor) - base[0], GetGValueSafe(bgColor) - base[1], GetRValueSafe(bgColor) - base[2], 255 }; HDC hDC = GetDC(NULL); BITMAPINFO bmi = { 0 }; SizeI size = GetBitmapSize(hbmp); bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); bmi.bmiHeader.biWidth = size.dx; bmi.bmiHeader.biHeight = size.dy; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = BI_RGB; int bmpBytes = size.dx * size.dy * 4; ScopedMem<unsigned char> bmpData((unsigned char *)malloc(bmpBytes)); CrashIf(!bmpData); if (GetDIBits(hDC, hbmp, 0, size.dy, bmpData, &bmi, DIB_RGB_COLORS)) { for (int i = 0; i < bmpBytes; i++) { int k = i % 4; bmpData[i] = (uint8_t)(base[k] + mul255(bmpData[i], diff[k])); } SetDIBits(hDC, hbmp, 0, size.dy, bmpData, &bmi, DIB_RGB_COLORS); } ReleaseDC(NULL, hDC); }