summaryrefslogtreecommitdiffstats
path: root/tests/pngxx.cpp
blob: 4a993707352d5bb4f239686b6068e67de2c5b1a6 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*************************************************************************
 * Copyright (C) 2008 Tavian Barnes <tavianator@gmail.com>               *
 *                                                                       *
 * This file is part of The Dimension Test Suite.                        *
 *                                                                       *
 * Dimension is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU Lesser General Public License as published *
 * by the Free Software Foundation; either version 3 of the License, or  *
 * (at your option) any later version.                                   *
 *                                                                       *
 * Dimension is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General Public      *
 * License along with this program.  If not, see                         *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

#include "testsxx.hpp"
#include <fstream>

int
main()
{
  Dimension::resilience(Dimension::SEVERITY_LOW);

  const unsigned int width = 333, height = 300;

  {
    std::ofstream ofstr("dimensionxx1.png", std::ios::binary);
    Dimension::PNG_Canvas ocanvas(3*width, height, ofstr);

    Dimension::CIE_xyY xyY;
    Dimension::CIE_Lab Lab;
    Dimension::CIE_Luv Luv;
    Dimension::sRGB RGB;
    Dimension::Color color;

    for (unsigned int x = 0; x < width; ++x) {
      for (unsigned int y = 0; y < height; ++y) {
        /* CIE xyY colorspace */
        xyY = Dimension::CIE_xyY(static_cast<double>(x)/(width - 1),
                                 static_cast<double>(y)/(height - 1),
                                 0.5);
        color = xyY;
        RGB = color;

        if (RGB.R() > 1.0 || RGB.G() > 1.0 || RGB.B() > 1.0
            || RGB.R() < 0.0 || RGB.G() < 0.0 || RGB.B() < 0.0) {
          /* Out of sRGB gamut */
          color.trans(0.5);
        }

        ocanvas.pixel(x, y, color);

        /* CIE Lab colorspace */

        Lab = Dimension::CIE_Lab(75.0,
                                 200.0*(static_cast<double>(x)/
                                        (width - 1) - 0.5),
                                 200.0*(static_cast<double>(y)/
                                        (height - 1) - 0.5));
        color = Lab;
        RGB = color;

        if (RGB.R() > 1.0 || RGB.G() > 1.0 || RGB.B() > 1.0
            || RGB.R() < 0.0 || RGB.G() < 0.0 || RGB.B() < 0.0) {
          /* Out of sRGB gamut */
          color.trans(0.5);
        }

        ocanvas.pixel(x + width, y, color);

        /* CIE Luv colorspace */

        Luv = Dimension::CIE_Luv(75.0,
                                 200.0*(static_cast<double>(x)/
                                        (width - 1) - 0.5),
                                 200.0*(static_cast<double>(y)/
                                        (height - 1) - 0.5));
        color = Luv;
        RGB = color;

        if (RGB.R() > 1.0 || RGB.G() > 1.0 || RGB.B() > 1.0
            || RGB.R() < 0.0 || RGB.G() < 0.0 || RGB.B() < 0.0) {
          /* Out of sRGB gamut */
          color.trans(0.5);
        }

        ocanvas.pixel(x + 2*width, y, color);
      }
    }

    Dimension::Progress progress = ocanvas.write_async();
    std::cout << "Writing PNG file: " << progress << std::endl;
  }

  std::ifstream ifstr("dimensionxx1.png", std::ios::binary);
  std::ofstream ofstr("dimensionxx2.png", std::ios::binary);

  Dimension::Progress iprogress
    = Dimension::PNG_Canvas::read_async(ifstr);
  std::cout << "Reading PNG file: " << iprogress << std::endl;

  Dimension::PNG_Canvas iocanvas(iprogress, ofstr);

  Dimension::Progress oprogress = iocanvas.write_async();
  std::cout << "Writing PNG file: " << oprogress << std::endl;

  return 0;
}