summaryrefslogtreecommitdiffstats
path: root/tests/png.c
blob: f8c81b8c2aae10663be4da1c495217b62a2d2d25 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*************************************************************************
 * 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 "../libdimension/dimension.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

int main() {
  dmnsn_canvas *canvas;
  dmnsn_color color;
  dmnsn_CIE_xyY xyY;
  dmnsn_CIE_Lab Lab;
  dmnsn_CIE_Luv Luv;
  dmnsn_sRGB sRGB;
  FILE *ofile, *ifile;
  unsigned int i, j;
  const unsigned int x = 333, y = 300;

  dmnsn_set_resilience(DMNSN_SEVERITY_LOW);

  canvas = dmnsn_new_canvas(3*x, y);
  if (!canvas) {
    fprintf(stderr, "--- Allocation of canvas failed! ---\n");
    return EXIT_FAILURE;
  }

  ofile = fopen("dimension1.png", "wb");
  if (!ofile) {
    fprintf(stderr, "--- Opening 'dimension1.png' for writing failed! ---\n");
    return EXIT_FAILURE;
  }

  for (i = 0; i < x; ++i) {
    for (j = 0; j < y; ++j) {
      /*
       * CIE xyY colorspace
       */
      xyY.Y = 0.5;
      xyY.x = ((double)i)/(x - 1);
      xyY.y = ((double)j)/(y - 1);

      color = dmnsn_color_from_xyY(xyY);
      sRGB  = dmnsn_sRGB_from_color(color);

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

      dmnsn_set_pixel(canvas, i, j, color);

      /*
       * CIE Lab colorspace
       */
      Lab.L = 75.0;
      Lab.a = 200.0*(((double)i)/(x - 1) - 0.5);
      Lab.b = 200.0*(((double)j)/(y - 1) - 0.5);

      color = dmnsn_color_from_Lab(Lab, dmnsn_whitepoint);
      sRGB  = dmnsn_sRGB_from_color(color);

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

      dmnsn_set_pixel(canvas, i + x, j, color);

      /*
       * CIE Luv colorspace
       */
      Luv.L = 75.0;
      Luv.u = 200.0*(((double)i)/(x - 1) - 0.5);
      Luv.v = 200.0*(((double)j)/(y - 1) - 0.5);

      color = dmnsn_color_from_Luv(Luv, dmnsn_whitepoint);
      sRGB  = dmnsn_sRGB_from_color(color);

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

      dmnsn_set_pixel(canvas, i + 2*x, j, color);
    }
  }

  if (dmnsn_png_write_canvas(canvas, ofile)) {
    fprintf(stderr, "--- Writing canvas failed! ---\n");
    return EXIT_FAILURE;
  }

  dmnsn_delete_canvas(canvas);

  fclose(ofile);
  ifile = fopen("dimension1.png", "rb");
  if (!ifile) {
    fprintf(stderr, "--- Opening 'dimension1.png' for reading failed! ---\n");
    return EXIT_FAILURE;
  }

  if (!(canvas = dmnsn_png_read_canvas(ifile))) {
    fprintf(stderr, "--- Reading canvas failed! ---\n");
    return EXIT_FAILURE;
  }

  fclose(ifile);
  ofile = fopen("dimension2.png", "wb");
  if (!ofile) {
    fprintf(stderr, "--- Opening 'dimension2.png' for writing failed! ---\n");
    return EXIT_FAILURE;
  }

  if (dmnsn_png_write_canvas(canvas, ofile)) {
    fprintf(stderr, "--- Writing canvas failed! ---\n");
    return EXIT_FAILURE;
  }

  dmnsn_delete_canvas(canvas);
  return EXIT_SUCCESS;
}