gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Giacomo Travaglini
38  */
39 
44 #include "base/pngwriter.hh"
45 
46 extern "C"
47 {
48 #include <png.h>
49 }
50 
51 #include <cstdio>
52 #include <cstdlib>
53 
54 #include "base/logging.hh"
55 
56 const char* PngWriter::_imgExtension = "png";
57 
65 static void
66 writePng(png_structp pngPtr, png_bytep data, png_size_t length)
67 {
68  // Here we get our IO pointer back from the write struct
69  // and we cast it into a ostream* type.
70  std::ostream* strmPtr = reinterpret_cast<std::ostream*>(
71  png_get_io_ptr(pngPtr)
72  );
73 
74  // Write length bytes to data
75  strmPtr->write(reinterpret_cast<const char *>(data), length);
76 }
77 
79  private:
80  // Make PngStructHandle uncopyable
81  PngStructHandle(const PngStructHandle&) = delete;
82  PngStructHandle& operator=(const PngStructHandle&) = delete;
83  public:
84 
86  pngWriteP(NULL), pngInfoP(NULL)
87  {
88  // Creating write structure
89  pngWriteP = png_create_write_struct(
90  PNG_LIBPNG_VER_STRING, NULL, NULL, NULL
91  );
92 
93  if (pngWriteP) {
94  // Creating info structure
95  pngInfoP = png_create_info_struct(pngWriteP);
96  }
97  }
98 
100  {
101  if (pngWriteP) {
102  png_destroy_write_struct(&pngWriteP, &pngInfoP);
103  }
104  }
105 
107  png_structp pngWriteP;
108 
110  png_infop pngInfoP;
111 };
112 
113 void
114 PngWriter::write(std::ostream &png) const
115 {
116 
117  // Height of the frame buffer
118  unsigned height = fb.height();
119  unsigned width = fb.width();
120 
121  // Do not write if frame buffer is empty
122  if (!fb.area()) {
123  png.flush();
124  return;
125  }
126 
127  // Initialize Png structures
128  PngStructHandle handle;
129 
130  // Png info/write pointers.
131  png_structp pngPtr = handle.pngWriteP;
132  png_infop infoPtr = handle.pngInfoP;
133 
134  if (!pngPtr) {
135  warn("Frame buffer dump aborted: Unable to create"
136  "Png Write Struct\n");
137  return;
138  }
139 
140  if (!infoPtr) {
141  warn("Frame buffer dump aborted: Unable to create"
142  "Png Info Struct\n");
143  return;
144  }
145 
146  // We cannot use default libpng write function since it requires
147  // a file pointer (FILE*), whereas we want to use the ostream.
148  // The following function replaces the write function with a custom
149  // one provided by us (writePng)
150  png_set_write_fn(pngPtr, (png_voidp)&png, writePng, NULL);
151 
152  png_set_IHDR(pngPtr, infoPtr, width, height, 8,
153  PNG_COLOR_TYPE_RGB,
154  PNG_INTERLACE_NONE,
155  PNG_COMPRESSION_TYPE_DEFAULT,
156  PNG_FILTER_TYPE_DEFAULT);
157 
158  png_write_info(pngPtr, infoPtr);
159 
160  // libpng requires an array of pointers to the frame buffer's rows.
161  std::vector<PixelType> rowPacked(width);
162  for (unsigned y=0; y < height; ++y) {
163  for (unsigned x=0; x < width; ++x) {
164  rowPacked[x] = fb.pixel(x, y);
165  }
166 
167  png_write_row(pngPtr,
168  reinterpret_cast<png_bytep>(rowPacked.data())
169  );
170  }
171 
172  // End of write
173  png_write_end(pngPtr, NULL);
174 }
175 
PngStructHandle & operator=(const PngStructHandle &)=delete
void write(std::ostream &png) const override
Write the frame buffer data into the provided ostream.
Definition: pngwriter.cc:114
const Pixel & pixel(unsigned x, unsigned y) const
Get a pixel from an (x, y) coordinate.
Definition: framebuffer.hh:159
const FrameBuffer & fb
Definition: imgwriter.hh:72
unsigned area() const
Total number of pixels in frame buffer.
Definition: framebuffer.hh:102
STL vector class.
Definition: stl.hh:40
png_infop pngInfoP
Pointer to PNG Info struct.
Definition: pngwriter.cc:110
static void writePng(png_structp pngPtr, png_bytep data, png_size_t length)
Write callback to use with libpng APIs.
Definition: pngwriter.cc:66
png_structp pngWriteP
Pointer to PNG Write struct.
Definition: pngwriter.cc:107
static const char * _imgExtension
Definition: pngwriter.hh:106
Bitfield< 4 > width
uint8_t length
Definition: inet.hh:334
#define warn(...)
Definition: logging.hh:212
unsigned width() const
Frame buffer width in pixels.
Definition: framebuffer.hh:98
Bitfield< 1 > x
Definition: types.hh:105
const char data[]
unsigned height() const
Frame buffer height in pixels.
Definition: framebuffer.hh:100

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13