CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Format/PNGLoad.cpp
Views: 1401
#include <cstdio>1#include <cstdlib>2#include <cstring>3#include <png.h>45#include "Common/Data/Format/PNGLoad.h"6#include "Common/Log.h"78// *image_data_ptr should be deleted with free()9// return value of 1 == success.10int pngLoad(const char *file, int *pwidth, int *pheight, unsigned char **image_data_ptr) {11png_image png;12memset(&png, 0, sizeof(png));13png.version = PNG_IMAGE_VERSION;1415png_image_begin_read_from_file(&png, file);1617if (PNG_IMAGE_FAILED(png))18{19WARN_LOG(Log::IO, "pngLoad: %s (%s)", png.message, file);20*image_data_ptr = nullptr;21return 0;22}23*pwidth = png.width;24*pheight = png.height;25png.format = PNG_FORMAT_RGBA;2627int stride = PNG_IMAGE_ROW_STRIDE(png);28*image_data_ptr = (unsigned char *)malloc(PNG_IMAGE_SIZE(png));29png_image_finish_read(&png, NULL, *image_data_ptr, stride, NULL);30return 1;31}3233int pngLoadPtr(const unsigned char *input_ptr, size_t input_len, int *pwidth, int *pheight, unsigned char **image_data_ptr) {34png_image png{};35png.version = PNG_IMAGE_VERSION;3637png_image_begin_read_from_memory(&png, input_ptr, input_len);3839if (PNG_IMAGE_FAILED(png)) {40WARN_LOG(Log::IO, "pngLoad: %s", png.message);41*image_data_ptr = nullptr;42return 0;43}44*pwidth = png.width;45*pheight = png.height;46png.format = PNG_FORMAT_RGBA;4748int stride = PNG_IMAGE_ROW_STRIDE(png);4950size_t size = PNG_IMAGE_SIZE(png);51if (!size) {52ERROR_LOG(Log::IO, "pngLoad: empty image");53*image_data_ptr = nullptr;54return 0;55}5657*image_data_ptr = (unsigned char *)malloc(size);58png_image_finish_read(&png, NULL, *image_data_ptr, stride, NULL);59return 1;60}6162bool PNGHeaderPeek::IsValidPNGHeader() const {63if (magic != 0x474e5089 || ihdrTag != 0x52444849) {64return false;65}66// Reject crazy sized images, too.67if (Width() > 32768 && Height() > 32768) {68return false;69}70return true;71}727374