summaryrefslogtreecommitdiff
path: root/libraries/daguerre/include/daguerre.hpp
blob: 94688261cab572821306879a9b3396d03589f04c (plain) (blame)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#pragma once

#include <algorithm>
#include <stdint.h>
#include <cstring>
#include <cstdio>

namespace daguerre {

  typedef uint32_t hilbert_color;

  struct rgb24 {
    uint8_t r;
    uint8_t g;
    uint8_t b;
  };

  template <class color_t>
  static inline void default_overlay(color_t &dest, const color_t &src) {
    dest = src;
  }

  static inline void default_overlay(hilbert_color &dest, const rgb24 &src) {
    dest = __euler_encode_color(src.r, src.g, src.b);
  }

  static inline void default_overlay(rgb24 &dest, const bool &src) {
    dest.r = src ? 255 : 0;
    dest.g = src ? 255 : 0;
    dest.b = src ? 255 : 0;
  }

  template <class color_t>
  class image {

  public:
    bool delete_buffer_on_destruct;
    color_t *buffer;
    unsigned width;
    unsigned height;
    unsigned pitch;//in sizeof(color_t)

    image()
    : delete_buffer_on_destruct(false), buffer(0), width(0), height(0),
      pitch(0) {}

    image(unsigned width, unsigned height)
    : delete_buffer_on_destruct(true), buffer(new color_t[width * height]),
      width(width), height(height), pitch(width) {}

    image(
      color_t *buffer, unsigned width, unsigned height, unsigned pitch,
      bool delete_buffer_on_destruct)
    : delete_buffer_on_destruct(delete_buffer_on_destruct), buffer(buffer),
      width(width), height(height), pitch(pitch) {}

    ~image() {
      if (delete_buffer_on_destruct && buffer)
        delete[] buffer;
    }

    template <class other_color_t,
      void overlay(color_t &, const other_color_t &) = default_overlay>
    image(const image<other_color_t> &other)
    : delete_buffer_on_destruct(true),
      buffer(new color_t[other.width * other.height]), width(other.width),
      height(other.height), pitch(other.width) {
      overlay_from<other_color_t, overlay>(other, 0, 0, 0, 0, width, height);
    }

    image(image<color_t> &&other)
    : delete_buffer_on_destruct(other.delete_buffer_on_destruct),
      buffer(other.buffer), width(other.width), height(other.height),
      pitch(other.pitch) {
      other.buffer = 0;
    }

    template <class other_color_t,
      void overlay(color_t &, const other_color_t &) = default_overlay>
    image<color_t> &operator =(const image<other_color_t> &other) {
      if (delete_buffer_on_destruct && buffer)
        delete[] buffer;
      delete_buffer_on_destruct = true;
      width = other.width;
      height = other.height;
      pitch = width;
      buffer = new color_t[width * height];
      overlay_from<other_color_t, overlay>(other, 0, 0, 0, 0, width, height);
      return *this;
    }

    image<color_t> &operator =(image<color_t> &&other) {
      if (delete_buffer_on_destruct && buffer)
        delete[] buffer;
      delete_buffer_on_destruct = other.delete_buffer_on_destruct;
      buffer = other.buffer;
      width = other.width;
      height = other.height;
      pitch = other.pitch;
      other.buffer = 0;
      return *this;
    }

    color_t get(unsigned x, unsigned y) const {
      return buffer[y * pitch + x];
    }

    void set(unsigned x, unsigned y, const color_t &c) {
      buffer[y * pitch + x] = c;
    }

    void copy_from(
      const image<color_t> &other, unsigned to_x, unsigned to_y,
      unsigned from_x, unsigned from_y, unsigned width, unsigned height) {

      color_t *to_start = buffer + pitch * to_y + to_x;
      const color_t *from_start = other.buffer + other.pitch * from_y + from_x;

      for (unsigned y = 0; y < height; ++y)
        std::memcpy(
          to_start + pitch * y, from_start + other.pitch * y,
          width * sizeof(color_t));

    }

    template <class other_color_t,
      void overlay(color_t &, const other_color_t &) = default_overlay>
    void overlay_from(
      const image<other_color_t> &other, unsigned to_x, unsigned to_y,
      unsigned from_x, unsigned from_y, unsigned width, unsigned height) {

      color_t *to_start = buffer + pitch * to_y + to_x;
      const other_color_t *from_start =
        other.buffer + other.pitch * from_y + from_x;

      for (unsigned y = 0; y < height; ++y)
        for (unsigned x = 0; x < width; ++x)
          overlay(to_start[pitch * y + x], from_start[other.pitch * y + x]);

    }

  };

  template <class color_t>
  void swap(image<color_t> &a, image<color_t> &b) {
    std::swap(a.delete_buffer_on_destruct, b.delete_buffer_on_destruct);
    std::swap(a.buffer, b.buffer);
    std::swap(a.width, b.width);
    std::swap(a.height, b.height);
    std::swap(a.pitch, b.pitch);
  }

  image<hilbert_color> get_hilbert_framebuffer();

  bool try_load_ppm(std::FILE *input, image<rgb24> &into);

  static inline bool try_load_ppm(const char *path, image<rgb24> &into) {
    std::FILE *f = std::fopen(path, "r");
    if (!f)
      return false;
    bool success = try_load_ppm(f, into);
    std::fclose(f);
    return success;
  }

  //TODO: unicode
  template <class color_t>
  class fixed_bitmap_font {

  public:
    unsigned width;
    unsigned height;
    image<color_t> glyphs[128];

    template <class target_color_t,
      void overlay(target_color_t &dest, const color_t &src) = default_overlay>
    void overlay_text(
      image<target_color_t> &target, unsigned x,
      unsigned y, const char *text) const {

      while (1) {
        uint8_t ch = (uint8_t)*text;
        if (ch == 0)
          return;
        if (ch < 128) {
          target.template overlay_from<color_t, overlay>(
            glyphs[ch], x, y, 0, 0, width, height);
          x += width;
        }
        ++text;
      }

    }

  };

  bool try_load_psf(std::FILE *input, fixed_bitmap_font<bool> &into);

  static inline bool try_load_psf(
    const char *path, fixed_bitmap_font<bool> &into) {
    std::FILE *f = std::fopen(path, "r");
    if (!f)
      return false;
    bool success = try_load_psf(f, into);
    std::fclose(f);
    return success;
  }

}