base: Introducing utility for writing raw data in png format

Originally it was possible to use a Bitmap writer class for dumping a
framebuffer snapshot in a .bmp file. This patch enables you to choose
another format.  In particular it implements the writing of PNG Images
using libpng library.  The latter has to be already installed in your
machine, otherwise gem5 will default to the Bitmap format.  This
configurable writer has been introduced in the VNC frame dumping mechanism,
which is storing changed frame buffers from the VNC server

Change-Id: Id7e5763c82235f1ce90381c8486b85a7cce734ce
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/5181
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
diff --git a/SConstruct b/SConstruct
index 95efb83..230232d 100755
--- a/SConstruct
+++ b/SConstruct
@@ -1,6 +1,6 @@
 # -*- mode:python -*-
 
-# Copyright (c) 2013, 2015, 2016 ARM Limited
+# Copyright (c) 2013, 2015-2017 ARM Limited
 # All rights reserved.
 #
 # The license below extends only to copyright in the software and shall
@@ -1134,6 +1134,14 @@
     print "Warning: Header file <fenv.h> not found."
     print "         This host has no IEEE FP rounding mode control."
 
+# Check for <png.h> (libpng library needed if wanting to dump
+# frame buffer image in png format)
+have_png = conf.CheckHeader('png.h', '<>')
+if not have_png:
+    print "Warning: Header file <png.h> not found."
+    print "         This host has no libpng library."
+    print "         Disabling support for PNG framebuffers."
+
 # Check if we should enable KVM-based hardware virtualization. The API
 # we rely on exists since version 2.6.36 of the kernel, but somehow
 # the KVM_API_VERSION does not reflect the change. We test for one of
@@ -1278,8 +1286,11 @@
                  False),
     BoolVariable('USE_POSIX_CLOCK', 'Use POSIX Clocks', have_posix_clock),
     BoolVariable('USE_FENV', 'Use <fenv.h> IEEE mode control', have_fenv),
-    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability', False),
-    BoolVariable('USE_KVM', 'Enable hardware virtualized (KVM) CPU models', have_kvm),
+    BoolVariable('USE_PNG',  'Enable support for PNG images', have_png),
+    BoolVariable('CP_ANNOTATE', 'Enable critical path annotation capability',
+                 False),
+    BoolVariable('USE_KVM', 'Enable hardware virtualized (KVM) CPU models',
+                 have_kvm),
     BoolVariable('USE_TUNTAP',
                  'Enable using a tap device to bridge to the host network',
                  have_tuntap),
@@ -1293,7 +1304,8 @@
 # These variables get exported to #defines in config/*.hh (see src/SConscript).
 export_vars += ['USE_FENV', 'SS_COMPATIBLE_FP', 'TARGET_ISA', 'TARGET_GPU_ISA',
                 'CP_ANNOTATE', 'USE_POSIX_CLOCK', 'USE_KVM', 'USE_TUNTAP',
-                'PROTOCOL', 'HAVE_PROTOBUF', 'HAVE_PERF_ATTR_EXCLUDE_HOST']
+                'PROTOCOL', 'HAVE_PROTOBUF', 'HAVE_PERF_ATTR_EXCLUDE_HOST',
+                'USE_PNG']
 
 ###################################################
 #
@@ -1486,6 +1498,14 @@
         print "Warning: No IEEE FP rounding mode control in", variant_dir + "."
         print "         FP results may deviate slightly from other platforms."
 
+    if not have_png and env['USE_PNG']:
+        print "Warning: <png.h> not available; " \
+              "forcing USE_PNG to False in", variant_dir + "."
+        env['USE_PNG'] = False
+
+    if env['USE_PNG']:
+        env.Append(LIBS=['png'])
+
     if env['EFENCE']:
         env.Append(LIBS=['efence'])
 
diff --git a/src/base/Graphics.py b/src/base/Graphics.py
new file mode 100644
index 0000000..c42882e
--- /dev/null
+++ b/src/base/Graphics.py
@@ -0,0 +1,43 @@
+# Copyright (c) 2017 ARM Limited
+# All rights reserved
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder.  You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met: redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer;
+# redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution;
+# neither the name of the copyright holders nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# Authors: Giacomo Travaglini
+
+from m5.SimObject import SimObject
+from m5.params import *
+
+# Image Formats:
+# Auto option will let gem5 to choose the image format it prefers.
+class ImageFormat(Enum): vals = ['Auto', 'Bitmap', 'Png']
diff --git a/src/base/SConscript b/src/base/SConscript
index 1ca0e35..7aa93f5 100644
--- a/src/base/SConscript
+++ b/src/base/SConscript
@@ -33,15 +33,19 @@
 if env['CP_ANNOTATE']:
     SimObject('CPA.py')
     Source('cp_annotate.cc')
+SimObject('Graphics.py')
 Source('atomicio.cc')
 Source('bitfield.cc')
 Source('bigint.cc')
-Source('bitmap.cc')
+Source('imgwriter.cc')
+Source('bmpwriter.cc')
 Source('callback.cc')
 Source('cprintf.cc')
 Source('debug.cc')
 if env['USE_FENV']:
     Source('fenv.c')
+if env['USE_PNG']:
+    Source('pngwriter.cc')
 Source('framebuffer.cc')
 Source('hostinfo.cc')
 Source('inet.cc')
diff --git a/src/base/bitmap.cc b/src/base/bmpwriter.cc
similarity index 92%
rename from src/base/bitmap.cc
rename to src/base/bmpwriter.cc
index 0052503..f650074 100644
--- a/src/base/bitmap.cc
+++ b/src/base/bmpwriter.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015 ARM Limited
+ * Copyright (c) 2010, 2015, 2017 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -40,24 +40,22 @@
  *          Andreas Sandberg
  */
 
-#include "base/bitmap.hh"
+#include "base/bmpwriter.hh"
 
 #include <cassert>
 
 #include "base/misc.hh"
 
+const char* BmpWriter::_imgExtension = "bmp";
+
 // bitmap class ctor
-Bitmap::Bitmap(const FrameBuffer *_fb)
-    : fb(*_fb)
+BmpWriter::BmpWriter(const FrameBuffer *_fb)
+    : ImgWriter(_fb)
 {
 }
 
-Bitmap::~Bitmap()
-{
-}
-
-const Bitmap::CompleteV1Header
-Bitmap::getCompleteHeader() const
+const BmpWriter::CompleteV1Header
+BmpWriter::getCompleteHeader() const
 {
     const uint32_t pixel_array_size(sizeof(PixelType) * fb.area());
     const uint32_t file_size(sizeof(CompleteV1Header) + pixel_array_size);
@@ -90,7 +88,7 @@
 }
 
 void
-Bitmap::write(std::ostream &bmp) const
+BmpWriter::write(std::ostream &bmp) const
 {
     const CompleteV1Header header(getCompleteHeader());
 
@@ -112,3 +110,4 @@
     bmp.flush();
 }
 
+
diff --git a/src/base/bitmap.hh b/src/base/bmpwriter.hh
similarity index 89%
rename from src/base/bitmap.hh
rename to src/base/bmpwriter.hh
index 0797a26..f78b994 100644
--- a/src/base/bitmap.hh
+++ b/src/base/bmpwriter.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015 ARM Limited
+ * Copyright (c) 2010, 2015, 2017 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -46,31 +46,38 @@
 
 #include "base/compiler.hh"
 #include "base/framebuffer.hh"
+#include "base/imgwriter.hh"
 
 /**
  * @file Declaration of a class that writes a frame buffer to a bitmap
  */
 
-
 // write frame buffer into a bitmap picture
-class  Bitmap
+class  BmpWriter : public ImgWriter
 {
   public:
     /**
      * Create a bitmap that takes data in a given mode & size and
      * outputs to an ostream.
      */
-    Bitmap(const FrameBuffer *fb);
+    BmpWriter(const FrameBuffer *fb);
 
-    ~Bitmap();
+    ~BmpWriter() {};
+
+    /*
+     * Return Image format as a string
+     *
+     * @return img extension (e.g. bmp for Bitmap)
+     */
+    const char* getImgExtension() const override
+    { return _imgExtension; }
 
     /**
      * Write the frame buffer data into the provided ostream
      *
      * @param bmp stream to write to
      */
-    void write(std::ostream &bmp) const;
-
+    void write(std::ostream &bmp) const override;
 
   private:
     struct FileHeader {
@@ -117,10 +124,11 @@
 
     typedef BmpPixel32 PixelType;
 
-    const CompleteV1Header getCompleteHeader() const;
+    static const char* _imgExtension;
 
-    const FrameBuffer &fb;
+    const CompleteV1Header getCompleteHeader() const;
 };
 
+
 #endif // __BASE_BITMAP_HH__
 
diff --git a/src/base/imgwriter.cc b/src/base/imgwriter.cc
new file mode 100644
index 0000000..ba9b58c
--- /dev/null
+++ b/src/base/imgwriter.cc
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2017 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Giacomo Travaglini
+ */
+
+#include "base/imgwriter.hh"
+#include "base/bmpwriter.hh"
+#include "base/misc.hh"
+
+#include "config/use_png.hh"
+
+#if USE_PNG
+#include "base/pngwriter.hh"
+#endif
+
+std::unique_ptr<ImgWriter>
+createImgWriter(Enums::ImageFormat type, const FrameBuffer *fb)
+{
+    switch (type) {
+      case Enums::Auto:
+        // The Auto option allows gem5 to choose automatically the
+        // writer type, and it will choose for the best fit in
+        // performance.
+        // gem5 will try PNG first, and it will fallback to BMP if not
+        // available.
+
+        /* FALLTHROUGH */
+#if USE_PNG
+      case Enums::Png:
+        return std::unique_ptr<PngWriter>(new PngWriter(fb));
+#endif
+      case Enums::Bitmap:
+        return std::unique_ptr<BmpWriter>(new BmpWriter(fb));
+      default:
+        warn("Invalid Image Type specified, defaulting to Bitmap\n");
+        return std::unique_ptr<BmpWriter>(new BmpWriter(fb));
+    }
+}
diff --git a/src/base/imgwriter.hh b/src/base/imgwriter.hh
new file mode 100644
index 0000000..8bfb920
--- /dev/null
+++ b/src/base/imgwriter.hh
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2017 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Giacomo Travaglini
+ */
+#ifndef __BASE_IMGWRITER_HH__
+#define __BASE_IMGWRITER_HH__
+
+#include <ostream>
+
+#include "base/compiler.hh"
+#include "base/framebuffer.hh"
+
+#include "enums/ImageFormat.hh"
+
+// write frame buffer to an image
+class ImgWriter
+{
+  public:
+    ImgWriter(const FrameBuffer *_fb)
+      : fb(*_fb)
+    {}
+
+    virtual ~ImgWriter() {};
+    /**
+     * Write the frame buffer data into the provided ostream
+     *
+     * @param out output stream to write to
+     */
+    virtual void write(std::ostream &out) const = 0;
+    /*
+     * Return Image format as a string
+     *
+     * @return img extension (e.g. bmp for Bitmap)
+     */
+    virtual const char* getImgExtension() const = 0;
+
+  protected:
+    const FrameBuffer &fb;
+};
+
+/**
+ * Factory Function which allocates a ImgWriter object and returns
+ * a smart pointer to it. The dynamic type of the object being pointed
+ * depends upon the enum type passed as a first parameter.
+ * If the enum contains an invalid value, the function will produce a warning
+ * and will default to Bitamp.
+ *
+ * @param type Image writer type (e.g. Bitamp, Png)
+ * @param fb Pointer to a FrameBuffer object
+ *           This contains the raw data which will be stored as an image
+ *           when calling the appropriate object method
+ * @return smart pointer to the allocated Image Writer
+ */
+std::unique_ptr<ImgWriter>
+createImgWriter(Enums::ImageFormat type, const FrameBuffer *fb);
+
+#endif //__BASE_IMGWRITER_HH__
diff --git a/src/base/pngwriter.cc b/src/base/pngwriter.cc
new file mode 100644
index 0000000..3b49d47
--- /dev/null
+++ b/src/base/pngwriter.cc
@@ -0,0 +1,175 @@
+/*
+ * Copyright (c) 2017 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Giacomo Travaglini
+ */
+
+/**
+ * @file Definition of a class that writes a frame buffer to a png
+ */
+
+#include "base/pngwriter.hh"
+
+extern "C"
+{
+#include <png.h>
+}
+
+#include <cstdio>
+#include <cstdlib>
+
+#include "base/misc.hh"
+
+const char* PngWriter::_imgExtension = "png";
+
+/**
+ * Write callback to use with libpng APIs
+ *
+ * @param pngPtr  pointer to the png_struct structure
+ * @param data    pointer to the data being written
+ * @param length  number of bytes being written
+ */
+static void
+writePng(png_structp pngPtr, png_bytep data, png_size_t length)
+{
+    // Here we get our IO pointer back from the write struct
+    // and we cast it into a ostream* type.
+    std::ostream* strmPtr = reinterpret_cast<std::ostream*>(
+        png_get_io_ptr(pngPtr)
+    );
+
+    // Write length bytes to data
+    strmPtr->write(reinterpret_cast<const char *>(data), length);
+}
+
+struct PngWriter::PngStructHandle {
+  private:
+    // Make PngStructHandle uncopyable
+    PngStructHandle(const PngStructHandle&) = delete;
+    PngStructHandle& operator=(const PngStructHandle&) = delete;
+  public:
+
+    PngStructHandle() :
+        pngWriteP(NULL), pngInfoP(NULL)
+    {
+        // Creating write structure
+        pngWriteP = png_create_write_struct(
+            PNG_LIBPNG_VER_STRING, NULL, NULL, NULL
+        );
+
+        if (pngWriteP) {
+            // Creating info structure
+            pngInfoP = png_create_info_struct(pngWriteP);
+        }
+    }
+
+    ~PngStructHandle()
+    {
+        if (pngWriteP) {
+            png_destroy_write_struct(&pngWriteP, &pngInfoP);
+        }
+    }
+
+    /** Pointer to PNG Write struct */
+    png_structp pngWriteP;
+
+    /** Pointer to PNG Info struct */
+    png_infop pngInfoP;
+};
+
+void
+PngWriter::write(std::ostream &png) const
+{
+
+    // Height of the frame buffer
+    unsigned height = fb.height();
+    unsigned width  = fb.width();
+
+    // Do not write if frame buffer is empty
+    if (!fb.area()) {
+        png.flush();
+        return;
+    }
+
+    // Initialize Png structures
+    PngStructHandle handle;
+
+    // Png info/write pointers.
+    png_structp pngPtr  = handle.pngWriteP;
+    png_infop   infoPtr = handle.pngInfoP;
+
+    if (!pngPtr) {
+        warn("Frame buffer dump aborted: Unable to create"
+             "Png Write Struct\n");
+        return;
+    }
+
+    if (!infoPtr) {
+        warn("Frame buffer dump aborted: Unable to create"
+             "Png Info Struct\n");
+        return;
+    }
+
+    // We cannot use default libpng write function since it requires
+    // a file pointer (FILE*), whereas we want to use the ostream.
+    // The following function replaces the write function with a custom
+    // one provided by us (writePng)
+    png_set_write_fn(pngPtr, (png_voidp)&png, writePng, NULL);
+
+    png_set_IHDR(pngPtr, infoPtr, width, height, 8,
+                 PNG_COLOR_TYPE_RGB,
+                 PNG_INTERLACE_NONE,
+                 PNG_COMPRESSION_TYPE_DEFAULT,
+                 PNG_FILTER_TYPE_DEFAULT);
+
+    png_write_info(pngPtr, infoPtr);
+
+    // libpng requires an array of pointers to the frame buffer's rows.
+    std::vector<PixelType> rowPacked(width);
+    for (unsigned y=0; y < height; ++y) {
+        for (unsigned x=0; x < height; ++x) {
+            rowPacked[x] = fb.pixel(x, y);
+        }
+
+        png_write_row(pngPtr,
+            reinterpret_cast<png_bytep>(rowPacked.data())
+        );
+    }
+
+    // End of write
+    png_write_end(pngPtr, NULL);
+}
+
diff --git a/src/base/bitmap.hh b/src/base/pngwriter.hh
similarity index 64%
copy from src/base/bitmap.hh
copy to src/base/pngwriter.hh
index 0797a26..984f2f6 100644
--- a/src/base/bitmap.hh
+++ b/src/base/pngwriter.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015 ARM Limited
+ * Copyright (c) 2017 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -34,93 +34,76 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * Authors: William Wang
- *          Ali Saidi
- *          Chris Emmons
- *          Andreas Sandberg
+ * Authors: Giacomo Travaglini
  */
-#ifndef __BASE_BITMAP_HH__
-#define __BASE_BITMAP_HH__
 
-#include <ostream>
+/**
+ * @file Declaration of a class that writes a frame buffer to a png
+ */
+
+#ifndef __BASE_PNG_HH__
+#define __BASE_PNG_HH__
 
 #include "base/compiler.hh"
 #include "base/framebuffer.hh"
+#include "base/imgwriter.hh"
 
-/**
- * @file Declaration of a class that writes a frame buffer to a bitmap
- */
-
-
-// write frame buffer into a bitmap picture
-class  Bitmap
+/** Image writer implementing support for PNG */
+class PngWriter : public ImgWriter
 {
   public:
     /**
-     * Create a bitmap that takes data in a given mode & size and
+     * Create a png that takes data in a given mode & size and
      * outputs to an ostream.
      */
-    Bitmap(const FrameBuffer *fb);
+    PngWriter(const FrameBuffer *_fb)
+      : ImgWriter(_fb)
+    {}
 
-    ~Bitmap();
+    ~PngWriter() {};
+
+    /**
+     * Return Image format as a string
+     *
+     * @return img extension (e.g. .png for Png)
+     */
+    const char* getImgExtension() const override
+    { return _imgExtension; }
 
     /**
      * Write the frame buffer data into the provided ostream
      *
-     * @param bmp stream to write to
+     * @param png stream to write to
      */
-    void write(std::ostream &bmp) const;
-
-
+    void write(std::ostream &png) const override;
   private:
-    struct FileHeader {
-        unsigned char magic_number[2];
-        uint32_t size;
-        uint16_t reserved1;
-        uint16_t reserved2;
-        uint32_t offset;
-    } M5_ATTR_PACKED;
-
-    struct InfoHeaderV1 { /* Aka DIB header */
-        uint32_t Size;
-        uint32_t Width;
-        uint32_t Height;
-        uint16_t Planes;
-        uint16_t BitCount;
-        uint32_t Compression;
-        uint32_t SizeImage;
-        uint32_t XPelsPerMeter;
-        uint32_t YPelsPerMeter;
-        uint32_t ClrUsed;
-        uint32_t ClrImportant;
-    } M5_ATTR_PACKED;
-
-    struct CompleteV1Header {
-        FileHeader file;
-        InfoHeaderV1 info;
-    } M5_ATTR_PACKED;
-
-    struct BmpPixel32 {
-        BmpPixel32 &operator=(const Pixel &rhs) {
+    /** Png Pixel type: not containing padding */
+    struct PngPixel24 {
+        PngPixel24 &operator=(const Pixel &rhs) {
             red = rhs.red;
             green = rhs.green;
             blue = rhs.blue;
-            padding = 0;
 
             return *this;
         }
-        uint8_t blue;
-        uint8_t green;
         uint8_t red;
-        uint8_t padding;
+        uint8_t green;
+        uint8_t blue;
     } M5_ATTR_PACKED;
 
-    typedef BmpPixel32 PixelType;
+    /**
+     * Handle to resources used by libpng:
+     *   - png_struct: Structure holding write informations
+     *   - png_info  : Structure holding image informations
+     *
+     * The class is automatically taking care of struct
+     * allocation/deallocation
+     */
+    struct PngStructHandle;
 
-    const CompleteV1Header getCompleteHeader() const;
+    typedef PngPixel24 PixelType;
 
-    const FrameBuffer &fb;
+    static const char* _imgExtension;
 };
 
-#endif // __BASE_BITMAP_HH__
-
+#endif // __BASE_PNG_HH__
diff --git a/src/base/vnc/Vnc.py b/src/base/vnc/Vnc.py
index a7faefb..0aed0dc 100644
--- a/src/base/vnc/Vnc.py
+++ b/src/base/vnc/Vnc.py
@@ -37,11 +37,16 @@
 
 from m5.SimObject import SimObject
 from m5.params import *
+from Graphics import *
+
 
 class VncInput(SimObject):
     type = 'VncInput'
     cxx_header = "base/vnc/vncinput.hh"
     frame_capture = Param.Bool(False, "capture changed frames to files")
+    img_format = Param.ImageFormat(
+        "Bitmap", "Format of the dumped Framebuffer"
+    )
 
 class VncServer(VncInput):
     type = 'VncServer'
diff --git a/src/base/vnc/vncinput.cc b/src/base/vnc/vncinput.cc
index 541b771..b9d1d25 100644
--- a/src/base/vnc/vncinput.cc
+++ b/src/base/vnc/vncinput.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015 ARM Limited
+ * Copyright (c) 2010, 2015, 2017 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -48,6 +48,7 @@
 
 #include "base/misc.hh"
 #include "base/output.hh"
+
 #include "base/trace.hh"
 #include "debug/VNC.hh"
 
@@ -58,7 +59,8 @@
       fb(&FrameBuffer::dummy),
       _videoWidth(fb->width()), _videoHeight(fb->height()),
       captureEnabled(p->frame_capture),
-      captureCurrentFrame(0), captureLastHash(0)
+      captureCurrentFrame(0), captureLastHash(0),
+      imgFormat(p->img_format)
 {
     if (captureEnabled) {
         // remove existing frame output directory if it exists, then create a
@@ -78,9 +80,11 @@
 
     fb = rfb;
 
-    // create bitmap of the frame with new attributes
-    if (captureEnabled)
-        captureBitmap.reset(new Bitmap(rfb));
+    // Create the Image Writer object in charge of dumping
+    // the frame buffer raw data into a file in a specific format.
+    if (captureEnabled) {
+        captureImage = createImgWriter(imgFormat, rfb);
+    }
 
     // Setting a new frame buffer means that we need to send an update
     // to the client. Mark the internal buffers as dirty to do so.
@@ -110,7 +114,7 @@
 void
 VncInput::captureFrameBuffer()
 {
-    assert(captureBitmap);
+    assert(captureImage);
 
     // skip identical frames
     uint64_t new_hash = fb->getHash();
@@ -120,13 +124,14 @@
 
     // get the filename for the current frame
     char frameFilenameBuffer[64];
-    snprintf(frameFilenameBuffer, 64, "fb.%06d.%lld.bmp.gz",
-            captureCurrentFrame, static_cast<long long int>(curTick()));
+    snprintf(frameFilenameBuffer, 64, "fb.%06d.%lld.%s.gz",
+            captureCurrentFrame, static_cast<long long int>(curTick()),
+            captureImage->getImgExtension());
     const string frameFilename(frameFilenameBuffer);
 
     // create the compressed framebuffer file
     OutputStream *fb_out(captureOutputDirectory->create(frameFilename, true));
-    captureBitmap->write(*fb_out->stream());
+    captureImage->write(*fb_out->stream());
     captureOutputDirectory->close(fb_out);
 
     ++captureCurrentFrame;
diff --git a/src/base/vnc/vncinput.hh b/src/base/vnc/vncinput.hh
index 15ddc5c..38f4245 100644
--- a/src/base/vnc/vncinput.hh
+++ b/src/base/vnc/vncinput.hh
@@ -48,7 +48,7 @@
 #include <iostream>
 #include <memory>
 
-#include "base/bitmap.hh"
+#include "base/imgwriter.hh"
 #include "params/VncInput.hh"
 #include "sim/sim_object.hh"
 
@@ -226,8 +226,11 @@
     /** Computed hash of the last captured frame */
     uint64_t captureLastHash;
 
-    /** Cached bitmap object for writing out frame buffers to file */
-    std::unique_ptr<Bitmap> captureBitmap;
+    /** Cached ImgWriter object for writing out frame buffers to file */
+    std::unique_ptr<ImgWriter> captureImage;
+
+    /** image format */
+    Enums::ImageFormat imgFormat;
 
     /** Captures the current frame buffer to a file */
     void captureFrameBuffer();
diff --git a/src/base/vnc/vncserver.cc b/src/base/vnc/vncserver.cc
index 9cf38dc..00ff560 100644
--- a/src/base/vnc/vncserver.cc
+++ b/src/base/vnc/vncserver.cc
@@ -64,7 +64,6 @@
 #include <cstdio>
 
 #include "base/atomicio.hh"
-#include "base/bitmap.hh"
 #include "base/misc.hh"
 #include "base/output.hh"
 #include "base/socket.hh"
diff --git a/src/base/vnc/vncserver.hh b/src/base/vnc/vncserver.hh
index 99f4b5f..f64ccd7 100644
--- a/src/base/vnc/vncserver.hh
+++ b/src/base/vnc/vncserver.hh
@@ -48,7 +48,6 @@
 #include <iostream>
 
 #include "base/vnc/vncinput.hh"
-#include "base/bitmap.hh"
 #include "base/circlebuf.hh"
 #include "base/pollevent.hh"
 #include "base/socket.hh"
diff --git a/src/dev/arm/hdlcd.hh b/src/dev/arm/hdlcd.hh
index ecfe1ea..dd52e20 100644
--- a/src/dev/arm/hdlcd.hh
+++ b/src/dev/arm/hdlcd.hh
@@ -79,7 +79,7 @@
 #include <fstream>
 #include <memory>
 
-#include "base/bitmap.hh"
+#include "base/bmpwriter.hh"
 #include "base/framebuffer.hh"
 #include "base/output.hh"
 #include "dev/arm/amba_device.hh"
@@ -350,7 +350,7 @@
     EventFunctionWrapper virtRefreshEvent;
 
     /** Helper to write out bitmaps */
-    Bitmap bmp;
+    BmpWriter bmp;
 
     /** Picture of what the current frame buffer looks like */
     OutputStream *pic;
diff --git a/src/dev/arm/pl111.hh b/src/dev/arm/pl111.hh
index a9189e9..3040ea7 100644
--- a/src/dev/arm/pl111.hh
+++ b/src/dev/arm/pl111.hh
@@ -49,7 +49,7 @@
 #include <fstream>
 #include <memory>
 
-#include "base/bitmap.hh"
+#include "base/bmpwriter.hh"
 #include "base/framebuffer.hh"
 #include "base/output.hh"
 #include "dev/arm/amba_device.hh"
@@ -266,7 +266,7 @@
     VncInput *vnc;
 
     /** Helper to write out bitmaps */
-    Bitmap bmp;
+    BmpWriter bmp;
 
     /** Picture of what the current frame buffer looks like */
     OutputStream *pic;