gem5  v21.1.0.2
pngwriter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
42 #include "base/pngwriter.hh"
43 
44 extern "C"
45 {
46 #include <png.h>
47 }
48 
49 #include <cstdio>
50 #include <cstdlib>
51 
52 #include "base/logging.hh"
53 
54 namespace gem5
55 {
56 
57 const char* PngWriter::_imgExtension = "png";
58 
66 static void
67 writePng(png_structp pngPtr, png_bytep data, png_size_t length)
68 {
69  // Here we get our IO pointer back from the write struct
70  // and we cast it into a ostream* type.
71  std::ostream* strmPtr = reinterpret_cast<std::ostream*>(
72  png_get_io_ptr(pngPtr)
73  );
74 
75  // Write length bytes to data
76  strmPtr->write(reinterpret_cast<const char *>(data), length);
77 }
78 
80 {
81  private:
82  // Make PngStructHandle uncopyable
83  PngStructHandle(const PngStructHandle&) = delete;
84  PngStructHandle& operator=(const PngStructHandle&) = delete;
85  public:
86 
88  pngWriteP(NULL), pngInfoP(NULL)
89  {
90  // Creating write structure
91  pngWriteP = png_create_write_struct(
92  PNG_LIBPNG_VER_STRING, NULL, NULL, NULL
93  );
94 
95  if (pngWriteP) {
96  // Creating info structure
97  pngInfoP = png_create_info_struct(pngWriteP);
98  }
99  }
100 
102  {
103  if (pngWriteP) {
104  png_destroy_write_struct(&pngWriteP, &pngInfoP);
105  }
106  }
107 
109  png_structp pngWriteP;
110 
112  png_infop pngInfoP;
113 };
114 
115 void
116 PngWriter::write(std::ostream &png) const
117 {
118 
119  // Height of the frame buffer
120  unsigned height = fb.height();
121  unsigned width = fb.width();
122 
123  // Do not write if frame buffer is empty
124  if (!fb.area()) {
125  png.flush();
126  return;
127  }
128 
129  // Initialize Png structures
130  PngStructHandle handle;
131 
132  // Png info/write pointers.
133  png_structp pngPtr = handle.pngWriteP;
134  png_infop infoPtr = handle.pngInfoP;
135 
136  if (!pngPtr) {
137  warn("Frame buffer dump aborted: Unable to create"
138  "Png Write Struct\n");
139  return;
140  }
141 
142  if (!infoPtr) {
143  warn("Frame buffer dump aborted: Unable to create"
144  "Png Info Struct\n");
145  return;
146  }
147 
148  // We cannot use default libpng write function since it requires
149  // a file pointer (FILE*), whereas we want to use the ostream.
150  // The following function replaces the write function with a custom
151  // one provided by us (writePng)
152  png_set_write_fn(pngPtr, (png_voidp)&png, writePng, NULL);
153 
154  png_set_IHDR(pngPtr, infoPtr, width, height, 8,
155  PNG_COLOR_TYPE_RGB,
156  PNG_INTERLACE_NONE,
157  PNG_COMPRESSION_TYPE_DEFAULT,
158  PNG_FILTER_TYPE_DEFAULT);
159 
160  png_write_info(pngPtr, infoPtr);
161 
162  // libpng requires an array of pointers to the frame buffer's rows.
163  std::vector<PixelType> rowPacked(width);
164  for (unsigned y=0; y < height; ++y) {
165  for (unsigned x=0; x < width; ++x) {
166  rowPacked[x] = fb.pixel(x, y);
167  }
168 
169  png_write_row(pngPtr,
170  reinterpret_cast<png_bytep>(rowPacked.data())
171  );
172  }
173 
174  // End of write
175  png_write_end(pngPtr, NULL);
176 }
177 
178 } // namespace gem5
gem5::PngWriter::_imgExtension
static const char * _imgExtension
Definition: pngwriter.hh:108
warn
#define warn(...)
Definition: logging.hh:245
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::PngWriter::PngStructHandle::pngWriteP
png_structp pngWriteP
Pointer to PNG Write struct.
Definition: pngwriter.cc:109
gem5::FrameBuffer::area
unsigned area() const
Total number of pixels in frame buffer.
Definition: framebuffer.hh:103
gem5::FrameBuffer::width
unsigned width() const
Frame buffer width in pixels.
Definition: framebuffer.hh:99
std::vector
STL vector class.
Definition: stl.hh:37
gem5::writePng
static void writePng(png_structp pngPtr, png_bytep data, png_size_t length)
Write callback to use with libpng APIs.
Definition: pngwriter.cc:67
gem5::PngWriter::PngStructHandle::operator=
PngStructHandle & operator=(const PngStructHandle &)=delete
gem5::PngWriter::PngStructHandle::PngStructHandle
PngStructHandle()
Definition: pngwriter.cc:87
gem5::FrameBuffer::height
unsigned height() const
Frame buffer height in pixels.
Definition: framebuffer.hh:101
pngwriter.hh
gem5::ArmISA::width
Bitfield< 4 > width
Definition: misc_types.hh:71
gem5::FrameBuffer::pixel
const Pixel & pixel(unsigned x, unsigned y) const
Get a pixel from an (x, y) coordinate.
Definition: framebuffer.hh:160
gem5::PngWriter::PngStructHandle::pngInfoP
png_infop pngInfoP
Pointer to PNG Info struct.
Definition: pngwriter.cc:112
gem5::RiscvISA::x
Bitfield< 3 > x
Definition: pagetable.hh:73
gem5::PngWriter::PngStructHandle::~PngStructHandle
~PngStructHandle()
Definition: pngwriter.cc:101
gem5::PngWriter::PngStructHandle
Definition: pngwriter.cc:79
logging.hh
gem5::PngWriter::write
void write(std::ostream &png) const override
Write the frame buffer data into the provided ostream.
Definition: pngwriter.cc:116
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::ImgWriter::fb
const FrameBuffer & fb
Definition: imgwriter.hh:74

Generated on Tue Sep 21 2021 12:24:57 for gem5 by doxygen 1.8.17