summaryrefslogtreecommitdiffstats
path: root/src/trace/imagemap.cpp
blob: 48acd8c3dd185a4fd80d0062097b0c2400c6b433 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * TODO: insert short description here
 *//*
 * Authors: see git history
 *
 * Copyright (C) 2018 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */
#include <cstdio>
#include "imagemap.h"

namespace Inkscape {
namespace Trace {

/*
 * GrayMap
 */

GrayMap::GrayMap(int width, int height)
    : MapBase(width, height)
{
}

bool GrayMap::writePPM(char const *fileName)
{
    if (!fileName) {
        return false;
    }

    auto f = std::fopen(fileName, "wb");
    if (!f) {
        return false;
    }

    std::fprintf(f, "P6 %d %d 255\n", width, height);

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            auto pix = getPixel(x, y) / 3;
            unsigned char pixb = pix & 0xff;
            std::fputc(pixb, f);
            std::fputc(pixb, f);
            std::fputc(pixb, f);
        }
    }

    std::fclose(f);

    return true;
}

/*
 * RgbMap
 */

RgbMap::RgbMap(int width, int height)
    : MapBase(width, height)
{
}

bool RgbMap::writePPM(char const *fileName)
{
    if (!fileName) {
        return false;
    }

    auto f = std::fopen(fileName, "wb");
    if (!f) {
        return false;
    }

    std::fprintf(f, "P6 %d %d 255\n", width, height);

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            auto rgb = getPixel(x, y);
            std::fputc(rgb.r, f);
            std::fputc(rgb.g, f);
            std::fputc(rgb.b, f);
        }
    }

    std::fclose(f);

    return true;
}

/*
 * IndexedMap
 */

IndexedMap::IndexedMap(int width, int height)
    : MapBase(width, height)
    , nrColors(0)
{
    clut.fill(RGB{0, 0, 0});
}

bool IndexedMap::writePPM(char const *fileName)
{
    if (!fileName) {
        return false;
    }

    auto f = std::fopen(fileName, "wb");
    if (!f) {
        return false;
    }

    std::fprintf(f, "P6 %d %d 255\n", width, height);

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            auto rgb = getPixelValue(x, y);
            std::fputc(rgb.r, f);
            std::fputc(rgb.g, f);
            std::fputc(rgb.b, f);
        }
    }

    std::fclose(f);

    return true;
}

} // namespace Trace
} // namespace Inkscape