Blue Brain BioExplorer
ImageGenerator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2017, EPFL/Blue Brain Project
3  *
4  * The Blue Brain BioExplorer is a tool for scientists to extract and analyse
5  * scientific data from visualization
6  *
7  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
8  *
9  * This library is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License version 3.0 as published
11  * by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include "ImageGenerator.h"
24 
29 
30 namespace core
31 {
33 {
34  if (_compressor)
35  tjDestroy(_compressor);
36 }
37 
39  const std::string& format,
40  const uint8_t quality)
41 {
42  return {freeimage::getBase64Image(frameBuffer.getImage(), format, quality)};
43 }
44 
45 ImageGenerator::ImageBase64 ImageGenerator::createImage(const std::vector<FrameBufferPtr>& frameBuffers,
46  const std::string& format,
47  const uint8_t quality)
48 {
49  if (frameBuffers.size() == 1)
50  return createImage(*frameBuffers[0], format, quality);
51 
52  std::vector<freeimage::ImagePtr> images;
53  for (auto frameBuffer : frameBuffers)
54  images.push_back(frameBuffer->getImage());
55  return {freeimage::getBase64Image(freeimage::mergeImages(images), format, quality)};
56 }
57 
59  const uint8_t quality)
60 {
61  frameBuffer.map();
62  const auto colorBuffer = frameBuffer.getColorBuffer();
63  if (!colorBuffer)
64  {
65  frameBuffer.unmap();
66  return ImageJPEG();
67  }
68 
69  int32_t pixelFormat = TJPF_RGBX;
70  switch (frameBuffer.getFrameBufferFormat())
71  {
73  pixelFormat = TJPF_BGRX;
74  break;
76  default:
77  pixelFormat = TJPF_RGBX;
78  }
79 
80  const auto& frameSize = frameBuffer.getSize();
81  ImageJPEG image;
82  image.data = _encodeJpeg(frameSize.x, frameSize.y, colorBuffer, pixelFormat, quality, image.size);
83  frameBuffer.unmap();
84  return image;
85 }
86 
87 ImageGenerator::ImageJPEG::JpegData ImageGenerator::_encodeJpeg(const uint32_t width, const uint32_t height,
88  const uint8_t* rawData, const int32_t pixelFormat,
89  const uint8_t quality, unsigned long& dataSize)
90 {
91  uint8_t* tjSrcBuffer = const_cast<uint8_t*>(rawData);
92  const int32_t color_components = 4; // Color Depth
93  const int32_t tjPitch = width * color_components;
94  const int32_t tjPixelFormat = pixelFormat;
95 
96  uint8_t* tjJpegBuf = 0;
97  const int32_t tjJpegSubsamp = TJSAMP_444;
98  const int32_t tjFlags = TJXOP_ROT180;
99 
100  const int32_t success = tjCompress2(_compressor, tjSrcBuffer, width, tjPitch, height, tjPixelFormat, &tjJpegBuf,
101  &dataSize, tjJpegSubsamp, quality, tjFlags);
102 
103  if (success != 0)
104  {
105  CORE_ERROR("libjpeg-turbo image conversion failure");
106  return 0;
107  }
108  return ImageJPEG::JpegData{tjJpegBuf};
109 }
110 } // namespace core
This class represents a frame buffer for an engine specific code. It provides an API for utilizing an...
Definition: FrameBuffer.h:39
PLATFORM_API FrameBufferFormat getFrameBufferFormat() const
Get the Frame Buffer Format object.
Definition: FrameBuffer.h:160
virtual PLATFORM_API const uint8_t * getColorBuffer() const =0
Get the Color Buffer object.
virtual PLATFORM_API void map()=0
Map the buffer for reading with get*Buffer().
PLATFORM_API freeimage::ImagePtr getImage()
Get the Image object.
Definition: FrameBuffer.cpp:49
virtual PLATFORM_API Vector2ui getSize() const
Get the Size object.
Definition: FrameBuffer.h:97
virtual PLATFORM_API void unmap()=0
Unmap the buffer for reading with get*Buffer().
ImageJPEG createJPEG(FrameBuffer &frameBuffer, uint8_t quality)
ImageBase64 createImage(FrameBuffer &frameBuffer, const std::string &format, uint8_t quality)
std::string getBase64Image(ImagePtr image, const std::string &format, const int quality)
Definition: ImageUtils.cpp:62
ImagePtr mergeImages(const std::vector< ImagePtr > &images)
Definition: ImageUtils.cpp:86
#define CORE_ERROR(__msg)
Definition: Logs.h:31
std::unique_ptr< uint8_t, tjDeleter > JpegData