loolwsd/ChildSession.cpp | 3 + loolwsd/LOKitClient.cpp | 7 +-- loolwsd/LOOLKit.cpp | 3 + loolwsd/Png.hpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++ loolwsd/Util.cpp | 73 ------------------------------------ loolwsd/Util.hpp | 8 ---- 6 files changed, 100 insertions(+), 87 deletions(-)
New commits: commit 3f03860a792bca54c63462627c00abdec4f2a469 Author: Ashod Nakashian <[email protected]> Date: Sat May 21 11:31:04 2016 -0400 loolwsd: moved and localized png bits to Png.hpp Change-Id: I4f27143bc2e5f638c8e84c32616b2820136a20e5 Reviewed-on: https://gerrit.libreoffice.org/25266 Reviewed-by: Ashod Nakashian <[email protected]> Tested-by: Ashod Nakashian <[email protected]> diff --git a/loolwsd/ChildSession.cpp b/loolwsd/ChildSession.cpp index c3b948b..785f6f2 100644 --- a/loolwsd/ChildSession.cpp +++ b/loolwsd/ChildSession.cpp @@ -28,6 +28,7 @@ #include "LOKitHelper.hpp" #include "LOOLProtocol.hpp" #include "Log.hpp" +#include "Png.hpp" #include "Rectangle.hpp" #include "Util.hpp" @@ -656,7 +657,7 @@ void ChildSession::sendFontRendering(const char* /*buffer*/, int /*length*/, Str if (pixmap != nullptr) { - if (!Util::encodeBufferToPNG(pixmap, width, height, output, LOK_TILEMODE_RGBA)) + if (!png::encodeBufferToPNG(pixmap, width, height, output, LOK_TILEMODE_RGBA)) { sendTextFrame("error: cmd=renderfont kind=failure"); delete[] pixmap; diff --git a/loolwsd/LOKitClient.cpp b/loolwsd/LOKitClient.cpp index 488644f..937cc6d 100644 --- a/loolwsd/LOKitClient.cpp +++ b/loolwsd/LOKitClient.cpp @@ -18,8 +18,6 @@ #include <LibreOfficeKit/LibreOfficeKit.h> #include <LibreOfficeKit/LibreOfficeKitInit.h> -#include <png.h> - #include <Poco/Buffer.h> #include <Poco/Process.h> #include <Poco/Random.h> @@ -30,6 +28,7 @@ #include <Poco/Util/Application.h> #include "LOKitHelper.hpp" +#include "Png.hpp" #include "Util.hpp" using Poco::StringTokenizer; @@ -170,9 +169,9 @@ protected: continue; std::vector<char> png; - LibreOfficeKitTileMode mode = static_cast<LibreOfficeKitTileMode>(loKitDocument->pClass->getTileMode(loKitDocument)); + const auto mode = static_cast<LibreOfficeKitTileMode>(loKitDocument->pClass->getTileMode(loKitDocument)); - Util::encodeBufferToPNG(pixmap.data(), canvasWidth, canvasHeight, png, mode); + png::encodeBufferToPNG(pixmap.data(), canvasWidth, canvasHeight, png, mode); TemporaryFile pngFile; std::ofstream pngStream(pngFile.path(), std::ios::binary); diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp index daf58b1..8cd6329 100644 --- a/loolwsd/LOOLKit.cpp +++ b/loolwsd/LOOLKit.cpp @@ -50,6 +50,7 @@ #include "LOOLProtocol.hpp" #include "LibreOfficeKit.hpp" #include "Log.hpp" +#include "Png.hpp" #include "QueueHandler.hpp" #include "TileDesc.hpp" #include "Unit.hpp" @@ -601,7 +602,7 @@ public: << " rendered in " << (timestamp.elapsed()/1000.) << " ms" << Log::end; const auto mode = static_cast<LibreOfficeKitTileMode>(_loKitDocument->getTileMode()); - if (!Util::encodeBufferToPNG(pixmap.data(), tile.getWidth(), tile.getHeight(), output, mode)) + if (!png::encodeBufferToPNG(pixmap.data(), tile.getWidth(), tile.getHeight(), output, mode)) { //FIXME: Return error. //sendTextFrame("error: cmd=tile kind=failure"); diff --git a/loolwsd/Png.hpp b/loolwsd/Png.hpp index 23d86d4..19bda55 100644 --- a/loolwsd/Png.hpp +++ b/loolwsd/Png.hpp @@ -1,3 +1,12 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + /* cairo - a vector graphics library with display and print output * * Copyright © 2003 University of Southern California @@ -36,6 +45,33 @@ * Chris Wilson <[email protected]> */ +#define PNG_SKIP_SETJMP_CHECK +#include <png.h> + +namespace png +{ + +// Callback functions for libpng +extern "C" +{ + static void user_write_status_fn(png_structp, png_uint_32, int) + { + } + + static void user_write_fn(png_structp png_ptr, png_bytep data, png_size_t length) + { + std::vector<char> *outputp = (std::vector<char> *) png_get_io_ptr(png_ptr); + const size_t oldsize = outputp->size(); + outputp->resize(oldsize + length); + std::memcpy(outputp->data() + oldsize, data, length); + } + + static void user_flush_fn(png_structp) + { + } +} + + /* Unpremultiplies data and converts native endian ARGB => RGBA bytes */ static void unpremultiply_data (png_structp /*png*/, png_row_infop row_info, png_bytep data) @@ -64,4 +100,61 @@ unpremultiply_data (png_structp /*png*/, png_row_infop row_info, png_bytep data) } } +// Sadly, older libpng headers don't use const for the pixmap pointer parameter to +// png_write_row(), so can't use const here for pixmap. +inline +bool encodeSubBufferToPNG(unsigned char* pixmap, int startX, int startY, + int width, int height, + int bufferWidth, int bufferHeight, + std::vector<char>& output, LibreOfficeKitTileMode mode) +{ + if (bufferWidth < width || bufferHeight < height) + { + return false; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (setjmp(png_jmpbuf(png_ptr))) + { + png_destroy_write_struct(&png_ptr, nullptr); + return false; + } + + png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + + png_set_write_fn(png_ptr, &output, user_write_fn, user_flush_fn); + png_set_write_status_fn(png_ptr, user_write_status_fn); + + png_write_info(png_ptr, info_ptr); + + if (mode == LOK_TILEMODE_BGRA) + { + png_set_write_user_transform_fn (png_ptr, unpremultiply_data); + } + + for (int y = 0; y < height; ++y) + { + size_t position = ((startY + y) * bufferWidth * 4) + (startX * 4); + png_write_row(png_ptr, pixmap + position); + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + return true; +} + +inline +bool encodeBufferToPNG(unsigned char* pixmap, int width, int height, + std::vector<char>& output, LibreOfficeKitTileMode mode) +{ + return encodeSubBufferToPNG(pixmap, 0, 0, width, height, width, height, output, mode); +} + +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp index 5b6ad9f..5c5ffe9 100644 --- a/loolwsd/Util.cpp +++ b/loolwsd/Util.cpp @@ -7,10 +7,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -// PNG headers are testy and don't like us including anything -// they include. Must be first until moved and restrained. -#include <png.h> - #include "Util.hpp" #include "config.h" @@ -44,28 +40,6 @@ #include "Common.hpp" #include "Log.hpp" -#include "Png.hpp" - -// Callback functions for libpng - -extern "C" -{ - static void user_write_status_fn(png_structp, png_uint_32, int) - { - } - - static void user_write_fn(png_structp png_ptr, png_bytep data, png_size_t length) - { - std::vector<char> *outputp = (std::vector<char> *) png_get_io_ptr(png_ptr); - const size_t oldsize = outputp->size(); - outputp->resize(oldsize + length); - std::memcpy(outputp->data() + oldsize, data, length); - } - - static void user_flush_fn(png_structp) - { - } -} volatile bool TerminationFlag = false; @@ -145,53 +119,6 @@ namespace Util return std::getenv("DISPLAY") != nullptr; } - bool encodeBufferToPNG(unsigned char *pixmap, int width, int height, std::vector<char>& output, LibreOfficeKitTileMode mode) - { - - return encodeSubBufferToPNG(pixmap, 0, 0, width, height, width, height, output, mode); - } - - bool encodeSubBufferToPNG(unsigned char *pixmap, int startX, int startY, int width, int height, - int bufferWidth, int bufferHeight, std::vector<char>& output, LibreOfficeKitTileMode mode) - { - if (bufferWidth < width || bufferHeight < height) - return false; - - png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); - - png_infop info_ptr = png_create_info_struct(png_ptr); - - if (setjmp(png_jmpbuf(png_ptr))) - { - png_destroy_write_struct(&png_ptr, nullptr); - return false; - } - - png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); - - png_set_write_fn(png_ptr, &output, user_write_fn, user_flush_fn); - png_set_write_status_fn(png_ptr, user_write_status_fn); - - png_write_info(png_ptr, info_ptr); - - if (mode == LOK_TILEMODE_BGRA) - { - png_set_write_user_transform_fn (png_ptr, unpremultiply_data); - } - - for (int y = 0; y < height; ++y) - { - size_t position = ((startY + y) * bufferWidth * 4) + (startX * 4); - png_write_row(png_ptr, pixmap + position); - } - - png_write_end(png_ptr, info_ptr); - - png_destroy_write_struct(&png_ptr, &info_ptr); - - return true; - } - const char *signalName(const int signo) { switch (signo) diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp index d364e1b..38b2268 100644 --- a/loolwsd/Util.hpp +++ b/loolwsd/Util.hpp @@ -47,14 +47,6 @@ namespace Util bool windowingAvailable(); - // Sadly, older libpng headers don't use const for the pixmap pointer parameter to - // png_write_row(), so can't use const here for pixmap. - bool encodeBufferToPNG(unsigned char* pixmap, int width, int height, - std::vector<char>& output, LibreOfficeKitTileMode mode); - bool encodeSubBufferToPNG(unsigned char* pixmap, int startX, int startY, int width, int height, - int bufferWidth, int bufferHeight, - std::vector<char>& output, LibreOfficeKitTileMode mode); - /// Assert that a lock is already taken. template <typename T> void assertIsLocked(T& lock)
_______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
