File: pngfile.cpp

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (70 lines) | stat: -rw-r--r-- 2,155 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "pngfile.h"

#include <stdexcept>

PngFile::PngFile(const std::string& filename, size_t width, size_t height)
    : _filename(filename), _width(width), _height(height), _pixelSize(4) {}

PngFile::~PngFile() {}

void PngFile::BeginWrite() {
  _fp = fopen(_filename.c_str(), "wb");
  if (!_fp) throw std::runtime_error("Can not open file");

  _png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp) nullptr,
                                     nullptr, nullptr);

  if (!_png_ptr) {
    fclose(_fp);
    throw std::runtime_error("Can not create png write structure");
  }

  _info_ptr = png_create_info_struct(_png_ptr);
  if (!_info_ptr) {
    png_destroy_write_struct(&_png_ptr, (png_infopp) nullptr);
    fclose(_fp);
    throw std::runtime_error("Can not write info structure to file");
  }

  if (setjmp(png_jmpbuf(_png_ptr))) {
    png_destroy_write_struct(&_png_ptr, &_info_ptr);
    fclose(_fp);
    throw std::runtime_error(
        "Unknown error occured during writing of png file");
  }

  png_init_io(_png_ptr, _fp);

  png_set_IHDR(_png_ptr, _info_ptr, _width, _height, 8,
               PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
               PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

  _row_pointers = (png_bytep*)png_malloc(_png_ptr, _height * sizeof(png_bytep));

  for (size_t i = 0; i < _height; i++)
    _row_pointers[i] = (png_bytep)png_malloc(_png_ptr, _width * _pixelSize);
}

void PngFile::Close() {
  png_set_rows(_png_ptr, _info_ptr, _row_pointers);
  png_write_png(_png_ptr, _info_ptr, PNG_TRANSFORM_IDENTITY, nullptr);
  png_write_end(_png_ptr, _info_ptr);

  for (unsigned i = 0; i < _height; i++) png_free(_png_ptr, _row_pointers[i]);
  png_free(_png_ptr, _row_pointers);

  png_destroy_write_struct(&_png_ptr, &_info_ptr);
  fclose(_fp);
}

void PngFile::Clear(int colorR, int colorG, int colorB, int colorA) {
  for (size_t y = 0; y < _height; y++) {
    int xa = 0;
    for (unsigned x = 0; x < _width; x++) {
      _row_pointers[y][xa++] = colorR;
      _row_pointers[y][xa++] = colorG;
      _row_pointers[y][xa++] = colorB;
      _row_pointers[y][xa++] = colorA;
    }
  }
}