aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcms <cms@beatworm.co.uk>2016-10-12 16:40:50 (GMT)
committercms <cms@beatworm.co.uk>2016-10-12 16:40:50 (GMT)
commit0519ea5647cb2a07f3300170e486290f7ef04ba8 (patch)
tree1faf8e37d8cb8805147d3bc70e77b8718f83628d
parenta327f22858d08562587a6e69ac406d2cad934a6f (diff)
downloadhydra-0519ea5647cb2a07f3300170e486290f7ef04ba8.zip
hydra-0519ea5647cb2a07f3300170e486290f7ef04ba8.tar.gz
hydra-0519ea5647cb2a07f3300170e486290f7ef04ba8.tar.bz2
fixes scaling, adds jpeg output
now the scaling is computed using dpi/pixels/mm via the units class image is scaled by 0.25 , not an aribitary 600x600 preserving aspect ratio scaling is now peformed by setting the PDF frame before rendering to any buffer (which sounds like it might be better, but doesn't seem to make much difference yet) jpeg output for the scaled pixmap now, via turbo-jpeg API this adds a new link-time dep on libturbo-jpeg8
-rw-r--r--Makefile2
-rw-r--r--src/HydraRootHandler.cpp85
2 files changed, 49 insertions, 38 deletions
diff --git a/Makefile b/Makefile
index 26bf2c7..22f47e7 100644
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ PDF_LIBS=-lpdfium -lformfiller -lfpdftext -lpdfwindow -lfxedit\
CPPFLAGS=-Wall -g -I$(PDFIUM_ROOT)/public/
LDFLAGS= -L$(PDFIUM_ROOT)/out/obj -L$(PDFIUM_ROOT)/out/obj/third_party
-LDLIBS=-lpthread -lPocoNet -lPocoFoundation -lPocoUtil
+LDLIBS=-lpthread -lPocoNet -lPocoFoundation -lPocoUtil -lturbojpeg
#FIXME library order is a massive PITA
LDLIBS+=$(PDF_LIBS) $(PDF_LIBS)
diff --git a/src/HydraRootHandler.cpp b/src/HydraRootHandler.cpp
index 56027e4..560ee17 100644
--- a/src/HydraRootHandler.cpp
+++ b/src/HydraRootHandler.cpp
@@ -5,50 +5,51 @@
#include "HydraUnit.h"
#include "fpdfview.h"
+#include "fpdf_transformpage.h"
#include <iostream>
#include <limits.h>
#include <stdio.h>
+#include <turbojpeg.h>
namespace Hydra {
- static bool CheckDimensions(int stride, int width, int height) {
- if (stride < 0 || width < 0 || height < 0)
+ static bool CheckDimensions(int width, int height) {
+ if ( width < 0 || height < 0)
return false;
if (height > 0 && width > INT_MAX / height)
return false;
return true;
}
- static void WritePpm(std::ostream& out, const void* buffer_void,
- int stride, int width, int height) {
- const char* buffer = reinterpret_cast<const char*>(buffer_void);
- if (!CheckDimensions(stride, width, height))
- return;
-
- int out_len = width * height;
- if (out_len > INT_MAX / 3)
+ static void WriteJPG(std::ostream& out, unsigned char* inbuf,
+ int width, int height)
+ {
+ if(!CheckDimensions(width,height)){
return;
- out_len *= 3;
-
- // Source data is B, G, R, unused.
- // Dest data is R, G, B.
- std::vector<char> result(out_len);
- for (int h = 0; h < height; ++h) {
- const char* src_line = buffer + (stride * h);
- char* dest_line = result.data() + (width * h * 3);
- for (int w = 0; w < width; ++w) {
- // R
- dest_line[w * 3] = src_line[(w * 4) + 2];
- // G
- dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
- // B
- dest_line[(w * 3) + 2] = src_line[w * 4];
- }
}
- out << "P6\n# PDF test render\n" << width << " " << height << "\n255\n";
- out.write(result.data(), out_len);
+
+
+ const int JPEG_QUALITY = 75;
+ long unsigned int jsiz = 0;
+ unsigned char* compressedImage = NULL;
+
+ tjhandle compressor = tjInitCompress();
+
+ tjCompress2(compressor, inbuf, width, 0, height, TJPF_BGRA,
+ &compressedImage, &jsiz, TJSAMP_444, JPEG_QUALITY,
+ TJFLAG_FASTDCT);
+
+ tjDestroy(compressor);
+
+
+ out.write((const char*)compressedImage,jsiz);
+ tjFree(compressedImage);
+
}
+
+
+
void RootHandler::handleRequest(Poco::Net::HTTPServerRequest& request,
Poco::Net::HTTPServerResponse& response)
{
@@ -70,21 +71,31 @@ namespace Hydra {
Unit height(FPDF_GetPageHeight(page),72);
Unit width(FPDF_GetPageWidth(page),72);
- std::cerr << "page width of " << width.points() << "pts " <<
- " which is " << width.pixels() << "px at 72dpi";
- FPDF_BITMAP bmp = FPDFBitmap_Create(600,600,1);
- FPDFBitmap_FillRect(bmp, 0, 0, 600, 600, 0xccccccff);
- FPDF_RenderPageBitmap(bmp,page,0,0,600,600,0,0);
+ double scale = 0.25;
+ std::cerr << "scale is " << scale << std::endl;
+ int heightPX = height.pixels() * scale;
+ int widthPX = width.pixels() * scale;
+ FPDF_BITMAP bmp = FPDFBitmap_Create(widthPX,
+ heightPX, 1);
+ FPDFBitmap_FillRect(bmp, 0, 0, widthPX, heightPX, 0xccccccff);
+
+
+ float llx, lly, urx, ury, newurx, newury;
- response.setContentType("image/x-portable-pixmap");
+ FPDFPage_GetMediaBox(page,&llx,&lly,&urx,&ury);
+ newurx = urx *scale, newury = ury *scale;
+ FPDFPage_SetMediaBox(page,llx,lly,newurx, newury);
+ FPDF_RenderPageBitmap(bmp,page,0,0,widthPX,heightPX,0,0);
+ FPDFPage_SetMediaBox(page,llx,lly,urx, ury);
+ response.setContentType("image/jpeg");
response.setChunkedTransferEncoding(true);
std::ostream& s = response.send();
- const char* buffer =
- reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bmp));
+ unsigned char* buffer =
+ reinterpret_cast<unsigned char*>(FPDFBitmap_GetBuffer(bmp));
- WritePpm(s,buffer,FPDFBitmap_GetStride(bmp),
+ WriteJPG(s,buffer,
FPDFBitmap_GetWidth(bmp),FPDFBitmap_GetHeight(bmp));
FPDF_DestroyLibrary();
}