rays 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/rays/bitmap.cpp +99 -32
  3. data/.doc/ext/rays/bounds.cpp +16 -12
  4. data/.doc/ext/rays/camera.cpp +1 -1
  5. data/.doc/ext/rays/color.cpp +2 -2
  6. data/.doc/ext/rays/color_space.cpp +3 -3
  7. data/.doc/ext/rays/defs.cpp +4 -4
  8. data/.doc/ext/rays/font.cpp +4 -3
  9. data/.doc/ext/rays/image.cpp +32 -18
  10. data/.doc/ext/rays/matrix.cpp +6 -6
  11. data/.doc/ext/rays/painter.cpp +1 -1
  12. data/.doc/ext/rays/point.cpp +10 -6
  13. data/.doc/ext/rays/polygon.cpp +11 -9
  14. data/.doc/ext/rays/polyline.cpp +6 -4
  15. data/.doc/ext/rays/rays.cpp +25 -15
  16. data/.doc/ext/rays/shader.cpp +3 -3
  17. data/.github/workflows/release-gem.yml +1 -1
  18. data/.github/workflows/test.yml +3 -0
  19. data/ChangeLog.md +11 -0
  20. data/Gemfile.lock +1 -1
  21. data/LICENSE +1 -1
  22. data/Rakefile +17 -3
  23. data/VERSION +1 -1
  24. data/ext/rays/bitmap.cpp +99 -32
  25. data/ext/rays/bounds.cpp +16 -12
  26. data/ext/rays/camera.cpp +1 -1
  27. data/ext/rays/color.cpp +2 -2
  28. data/ext/rays/color_space.cpp +3 -3
  29. data/ext/rays/defs.cpp +4 -4
  30. data/ext/rays/defs.h +2 -0
  31. data/ext/rays/extconf.rb +4 -2
  32. data/ext/rays/font.cpp +4 -3
  33. data/ext/rays/image.cpp +34 -18
  34. data/ext/rays/matrix.cpp +6 -6
  35. data/ext/rays/painter.cpp +1 -1
  36. data/ext/rays/point.cpp +10 -6
  37. data/ext/rays/polygon.cpp +11 -9
  38. data/ext/rays/polyline.cpp +6 -4
  39. data/ext/rays/rays.cpp +25 -15
  40. data/ext/rays/shader.cpp +3 -3
  41. data/include/rays/defs.h +7 -0
  42. data/include/rays/image.h +8 -2
  43. data/include/rays/ruby/bitmap.h +2 -2
  44. data/include/rays/ruby/bounds.h +2 -2
  45. data/include/rays/ruby/camera.h +2 -2
  46. data/include/rays/ruby/color.h +2 -2
  47. data/include/rays/ruby/color_space.h +2 -2
  48. data/include/rays/ruby/exception.h +3 -3
  49. data/include/rays/ruby/font.h +2 -2
  50. data/include/rays/ruby/image.h +2 -2
  51. data/include/rays/ruby/matrix.h +2 -2
  52. data/include/rays/ruby/painter.h +2 -2
  53. data/include/rays/ruby/point.h +2 -2
  54. data/include/rays/ruby/polygon.h +2 -2
  55. data/include/rays/ruby/polyline.h +2 -2
  56. data/include/rays/ruby/rays.h +6 -6
  57. data/include/rays/ruby/shader.h +2 -2
  58. data/lib/rays/bitmap.rb +7 -0
  59. data/lib/rays/extension.rb +4 -0
  60. data/lib/rays/image.rb +4 -0
  61. data/rays.gemspec +2 -2
  62. data/src/bounds.cpp +6 -2
  63. data/src/color.cpp +12 -4
  64. data/src/coord.h +2 -2
  65. data/src/font.cpp +1 -0
  66. data/src/image.cpp +32 -4
  67. data/src/ios/bitmap.mm +40 -33
  68. data/src/ios/font.mm +6 -1
  69. data/src/ios/rays.mm +2 -2
  70. data/src/matrix.h +1 -1
  71. data/src/opengl.h +1 -2
  72. data/src/osx/bitmap.mm +40 -33
  73. data/src/osx/font.mm +6 -1
  74. data/src/osx/rays.mm +2 -2
  75. data/src/painter.cpp +4 -1
  76. data/src/point.cpp +15 -3
  77. data/src/polygon.cpp +3 -5
  78. data/src/render_buffer.cpp +11 -3
  79. data/src/shader.cpp +3 -0
  80. data/src/shader_program.cpp +19 -9
  81. data/src/shader_source.cpp +5 -1
  82. data/src/texture.cpp +33 -11
  83. data/src/texture.h +6 -2
  84. data/src/win32/bitmap.cpp +178 -66
  85. data/src/win32/camera.cpp +119 -0
  86. data/src/win32/font.cpp +181 -40
  87. data/src/win32/gdi.h +1 -1
  88. data/src/win32/opengl.cpp +127 -0
  89. data/src/win32/rays.cpp +16 -9
  90. data/test/helper.rb +2 -0
  91. data/test/test_bitmap.rb +3 -1
  92. data/test/test_image.rb +8 -14
  93. data/test/test_painter.rb +4 -4
  94. data/test/test_painter_shape.rb +6 -5
  95. metadata +8 -7
  96. data/src/win32/font.h +0 -24
data/src/win32/bitmap.cpp CHANGED
@@ -1,8 +1,16 @@
1
1
  #include "../bitmap.h"
2
2
 
3
3
 
4
- #include <assert.h>
5
- #include "font.h"
4
+ #define STB_IMAGE_IMPLEMENTATION
5
+ #include <stb_image.h>
6
+ #define STB_IMAGE_WRITE_IMPLEMENTATION
7
+ #include <stb_image_write.h>
8
+
9
+ #include "rays/exception.h"
10
+ #include "../color_space.h"
11
+ #include "../font.h"
12
+ #include "../texture.h"
13
+ #include "../frame_buffer.h"
6
14
  #include "gdi.h"
7
15
 
8
16
 
@@ -15,60 +23,59 @@ namespace Rays
15
23
 
16
24
  int width, height, pitch;
17
25
 
18
- ColorSpace colorspace;
26
+ ColorSpace color_space;
19
27
 
20
- void* pixels;
28
+ void* pixels = NULL;
21
29
 
22
30
  Win32::MemoryDC memdc;
23
31
 
24
32
  bool modified;
25
33
 
26
34
  Data ()
27
- : pixels(NULL), modified(true)
28
35
  {
36
+ clear();
29
37
  }
30
38
 
31
- };// Window::Data
32
-
33
-
34
- static bool
35
- init_bitmap_pixels (Bitmap* bmp)
36
- {
37
- if (!*bmp) return false;
38
-
39
- memset(bmp->data(), 0, bmp->size());
40
-
41
- if (!bmp->color_space().has_alpha())
42
- return true;
39
+ ~Data ()
40
+ {
41
+ clear();
42
+ }
43
43
 
44
- int Bpp = bmp->color_space().Bpp();
45
- for (int y = 0; y < bmp->height(); ++y)
44
+ void clear ()
46
45
  {
47
- unsigned char* p =
48
- bmp->at<unsigned char>(0, y) + bmp->color_space().alpha_pos();
49
- int w = bmp->width();
50
- while (w--)
51
- {
52
- *p = 255;
53
- p += Bpp;
54
- }
46
+ if (memdc) memdc = Win32::MemoryDC();
47
+
48
+ width = height = pitch = 0;
49
+ color_space = COLORSPACE_UNKNOWN;
50
+ pixels = NULL;
51
+ modified = false;
55
52
  }
56
53
 
57
- return true;
58
- }
54
+ };// Bitmap::Data
55
+
59
56
 
60
- static bool
61
- setup_bitmap (Bitmap* bmp, int w, int h, const ColorSpace& cs, HDC hdc = NULL)
57
+ static void
58
+ setup_bitmap (
59
+ Bitmap* bitmap,
60
+ int w, int h, const ColorSpace& cs,
61
+ const void* pixels = NULL, bool clear_pixels = true, HDC hdc = NULL)
62
62
  {
63
- if (w <= 0 || h <= 0 || !cs || !bmp || *bmp)
64
- return false;
63
+ if (w <= 0)
64
+ argument_error(__FILE__, __LINE__);
65
+ if (h <= 0)
66
+ argument_error(__FILE__, __LINE__);
67
+ if (!cs)
68
+ argument_error(__FILE__, __LINE__);
69
+
70
+ Bitmap::Data* self = bitmap->self.get();
65
71
 
66
- Bitmap::Data* self = bmp->self.get();
72
+ self->clear();
67
73
 
68
- self->width = w;
69
- self->height = h;
70
- self->colorspace = cs;
71
- self->pitch = self->width * self->colorspace.Bpp();
74
+ self->width = w;
75
+ self->height = h;
76
+ self->pitch = w * cs.Bpp();
77
+ self->color_space = cs;
78
+ self->modified = true;
72
79
 
73
80
  int padding = 4 - self->pitch % 4;
74
81
  if (padding < 4) self->pitch += padding;
@@ -81,32 +88,49 @@ namespace Rays
81
88
  header.biWidth = self->width;
82
89
  header.biHeight = -self->height;
83
90
  header.biPlanes = 1;
84
- header.biBitCount = self->colorspace.bpp();
91
+ header.biBitCount = self->color_space.bpp();
85
92
  header.biCompression = BI_RGB;
86
93
 
87
94
  Win32::DC dc = hdc ? Win32::DC(hdc) : Win32::screen_dc();
88
95
 
89
96
  HBITMAP hbmp = CreateDIBSection(
90
97
  dc.handle(), &bmpinfo, DIB_RGB_COLORS, (void**) &self->pixels, NULL, 0);
91
- if (!hbmp) return false;
98
+ if (!hbmp)
99
+ rays_error(__FILE__, __LINE__);
92
100
 
93
101
  self->memdc = Win32::MemoryDC(dc.handle(), Win32::Bitmap(hbmp, true));
94
- if (!self->memdc) return false;
95
-
96
- return init_bitmap_pixels(bmp);
97
- }
98
-
99
- static void
100
- setup_bitmap (Bitmap* this_, const Texture& tex)
101
- {
102
- not_implemented_error(__FILE__, __LINE__);
102
+ if (!self->memdc)
103
+ rays_error(__FILE__, __LINE__);
104
+
105
+ size_t size = self->pitch * self->height;
106
+ if (pixels)
107
+ memcpy(self->pixels, pixels, size);
108
+ else if (clear_pixels)
109
+ memset(self->pixels, 0, size);
103
110
  }
104
111
 
105
112
  Bitmap
106
- Bitmap_from (const Texture& texture)
113
+ Bitmap_from (const Texture& tex)
107
114
  {
115
+ if (!tex)
116
+ argument_error(__FILE__, __LINE__);
117
+
108
118
  Bitmap bmp;
109
- setup_bitmap(&bmp, texture);
119
+ setup_bitmap(
120
+ &bmp, tex.width(), tex.height(), tex.color_space(), NULL, false);
121
+
122
+ GLenum format, type;
123
+ ColorSpace_get_gl_format_and_type(&format, &type, tex.color_space());
124
+
125
+ FrameBuffer fb(tex);
126
+ FrameBufferBinder binder(fb.id());
127
+
128
+ for (int y = 0; y < bmp.height(); ++y)
129
+ {
130
+ GLvoid* ptr = (GLvoid*) bmp.at<uchar>(0, y);
131
+ glReadPixels(0, y, bmp.width(), 1, format, type, ptr);
132
+ }
133
+
110
134
  return bmp;
111
135
  }
112
136
 
@@ -114,7 +138,13 @@ namespace Rays
114
138
  Bitmap_draw_string (
115
139
  Bitmap* bitmap, const RawFont& font, const char* str, coord x, coord y)
116
140
  {
117
- if (!bitmap || !*bitmap || !font || !str)
141
+ if (!bitmap)
142
+ argument_error(__FILE__, __LINE__);
143
+ if (!*bitmap)
144
+ argument_error(__FILE__, __LINE__);
145
+ if (!font)
146
+ argument_error(__FILE__, __LINE__);
147
+ if (!str)
118
148
  argument_error(__FILE__, __LINE__);
119
149
 
120
150
  if (*str == '\0') return;
@@ -126,8 +156,6 @@ namespace Rays
126
156
  void
127
157
  Bitmap_set_modified (Bitmap* bitmap, bool modified)
128
158
  {
129
- assert(bitmap);
130
-
131
159
  bitmap->self->modified = modified;
132
160
  }
133
161
 
@@ -137,16 +165,83 @@ namespace Rays
137
165
  return bitmap.self->modified;
138
166
  }
139
167
 
140
- bool
141
- Bitmap_save (const Bitmap& bitmap, const char* path)
168
+ static const char*
169
+ get_ext (const char* path)
142
170
  {
143
- return false;
171
+ if (!path)
172
+ return NULL;
173
+
174
+ return strrchr(path, '.');
144
175
  }
145
176
 
146
- bool
147
- Bitmap_load (Bitmap* bitmap, const char* path)
177
+ void
178
+ Bitmap_save (const Bitmap& bmp, const char* path)
148
179
  {
149
- return false;
180
+ const char* ext = get_ext(path);
181
+ if (!ext)
182
+ {
183
+ argument_error(
184
+ __FILE__, __LINE__, "invalid image file extension: '%s'", path);
185
+ }
186
+
187
+ const auto& cs = bmp.color_space();
188
+ size_t w = bmp.width();
189
+ size_t h = bmp.height();
190
+ size_t pitch = w * cs.Bpp();
191
+
192
+ std::unique_ptr<uchar[]> pixels(new uchar[h * pitch]);
193
+ for (size_t y = 0; y < h; ++y)
194
+ memcpy(pixels.get() + pitch * y, bmp.at<uchar>(0, y), pitch);
195
+
196
+ int ret = 0;
197
+ if (stricmp(ext, ".bmp") == 0)
198
+ ret = stbi_write_bmp(path, w, h, cs.Bpp(), pixels.get());
199
+ else
200
+ if (stricmp(ext, ".png") == 0)
201
+ ret = stbi_write_png(path, w, h, cs.Bpp(), pixels.get(), 0);
202
+ else
203
+ if (stricmp(ext, ".jpg") == 0 || stricmp(ext, ".jpeg") == 0)
204
+ ret = stbi_write_jpg(path, w, h, cs.Bpp(), pixels.get(), 90);
205
+ else
206
+ if (stricmp(ext, ".tga") == 0)
207
+ ret = stbi_write_tga(path, w, h, cs.Bpp(), pixels.get());
208
+ else
209
+ argument_error(__FILE__, __LINE__, "unknown image file type");
210
+
211
+ if (!ret)
212
+ rays_error(__FILE__, __LINE__, "failed to save: '%s'", path);
213
+ }
214
+
215
+ Bitmap
216
+ Bitmap_load (const char* path)
217
+ {
218
+ if (!path)
219
+ argument_error(__FILE__, __LINE__);
220
+
221
+ int w = 0, h = 0, Bpp = 0;
222
+ uchar* pixels = stbi_load(path, &w, &h, &Bpp, 0);
223
+ if (!pixels)
224
+ rays_error(__FILE__, __LINE__, "failed to load: '%s'", path);
225
+
226
+ ColorSpace cs;
227
+ switch (Bpp)
228
+ {
229
+ case 1: cs = GRAY_8; break;
230
+ case 3: cs = RGB_888; break;
231
+ case 4: cs = RGBA_8888; break;
232
+ default:
233
+ rays_error(__FILE__, __LINE__, "unsupported image file: '%s'", path);
234
+ }
235
+
236
+ Bitmap bmp(w, h, cs);
237
+ if (!bmp)
238
+ rays_error(__FILE__, __LINE__, "failed to create Bitmap object");
239
+
240
+ int pitch = Bpp * w;
241
+ for (int y = 0; y < h; ++y)
242
+ memcpy(bmp.at<uchar>(0, y), pixels + pitch * y, pitch);
243
+
244
+ return bmp;
150
245
  }
151
246
 
152
247
 
@@ -154,31 +249,45 @@ namespace Rays
154
249
  {
155
250
  }
156
251
 
157
- Bitmap::Bitmap (int width, int height, const ColorSpace& cs)
252
+ Bitmap::Bitmap (
253
+ int width, int height, const ColorSpace& color_space, const void* pixels)
158
254
  {
159
- setup_bitmap(this, width, height, cs);
255
+ setup_bitmap(this, width, height, color_space, pixels);
160
256
  }
161
257
 
162
258
  Bitmap::~Bitmap ()
163
259
  {
164
260
  }
165
261
 
262
+ Bitmap
263
+ Bitmap::dup () const
264
+ {
265
+ return Bitmap(width(), height(), color_space(), pixels());
266
+ }
267
+
166
268
  int
167
269
  Bitmap::width () const
168
270
  {
271
+ if (!*this) return 0;
169
272
  return self->width;
170
273
  }
171
274
 
172
275
  int
173
276
  Bitmap::height () const
174
277
  {
278
+ if (!*this) return 0;
175
279
  return self->height;
176
280
  }
177
281
 
178
282
  const ColorSpace&
179
283
  Bitmap::color_space () const
180
284
  {
181
- return self->colorspace;
285
+ if (!*this)
286
+ {
287
+ static const ColorSpace UNKNOWN = COLORSPACE_UNKNOWN;
288
+ return UNKNOWN;
289
+ }
290
+ return self->color_space;
182
291
  }
183
292
 
184
293
  int
@@ -196,6 +305,7 @@ namespace Rays
196
305
  void*
197
306
  Bitmap::pixels ()
198
307
  {
308
+ if (!*this) return NULL;
199
309
  return self->pixels;
200
310
  }
201
311
 
@@ -208,9 +318,11 @@ namespace Rays
208
318
  Bitmap::operator bool () const
209
319
  {
210
320
  return
211
- self &&
212
- self->width > 0 && self->height > 0 && self->pitch > 0 &&
213
- self->colorspace && self->pixels && self->memdc;
321
+ self->width > 0 &&
322
+ self->height > 0 &&
323
+ self->pitch > 0 &&
324
+ self->color_space &&
325
+ self->pixels;
214
326
  }
215
327
 
216
328
  bool
@@ -0,0 +1,119 @@
1
+ #include "rays/camera.h"
2
+
3
+
4
+ #include "rays/exception.h"
5
+
6
+
7
+ namespace Rays
8
+ {
9
+
10
+
11
+ struct Camera::Data
12
+ {
13
+ };// Camera::Data
14
+
15
+
16
+ std::vector<String>
17
+ get_camera_device_names ()
18
+ {
19
+ not_implemented_error(__FILE__, __LINE__);
20
+ }
21
+
22
+
23
+ Camera::Camera (
24
+ const char* device_name,
25
+ int min_width, int min_height, bool resize, bool crop)
26
+ {
27
+ not_implemented_error(__FILE__, __LINE__);
28
+ }
29
+
30
+ Camera::~Camera ()
31
+ {
32
+ not_implemented_error(__FILE__, __LINE__);
33
+ }
34
+
35
+ bool
36
+ Camera::start ()
37
+ {
38
+ not_implemented_error(__FILE__, __LINE__);
39
+ }
40
+
41
+ void
42
+ Camera::stop ()
43
+ {
44
+ not_implemented_error(__FILE__, __LINE__);
45
+ }
46
+
47
+ bool
48
+ Camera::is_active () const
49
+ {
50
+ not_implemented_error(__FILE__, __LINE__);
51
+ }
52
+
53
+ void
54
+ Camera::set_min_width (int width)
55
+ {
56
+ not_implemented_error(__FILE__, __LINE__);
57
+ }
58
+
59
+ int
60
+ Camera::min_width () const
61
+ {
62
+ not_implemented_error(__FILE__, __LINE__);
63
+ }
64
+
65
+ void
66
+ Camera::set_min_height (int height)
67
+ {
68
+ not_implemented_error(__FILE__, __LINE__);
69
+ }
70
+
71
+ int
72
+ Camera::min_height () const
73
+ {
74
+ not_implemented_error(__FILE__, __LINE__);
75
+ }
76
+
77
+ void
78
+ Camera::set_resize (bool resize)
79
+ {
80
+ not_implemented_error(__FILE__, __LINE__);
81
+ }
82
+
83
+ bool
84
+ Camera::is_resize () const
85
+ {
86
+ not_implemented_error(__FILE__, __LINE__);
87
+ }
88
+
89
+ void
90
+ Camera::set_crop (bool crop)
91
+ {
92
+ not_implemented_error(__FILE__, __LINE__);
93
+ }
94
+
95
+ bool
96
+ Camera::is_crop () const
97
+ {
98
+ not_implemented_error(__FILE__, __LINE__);
99
+ }
100
+
101
+ const Image*
102
+ Camera::image () const
103
+ {
104
+ not_implemented_error(__FILE__, __LINE__);
105
+ }
106
+
107
+ Camera::operator bool () const
108
+ {
109
+ return false;
110
+ }
111
+
112
+ bool
113
+ Camera::operator ! () const
114
+ {
115
+ return !operator bool();
116
+ }
117
+
118
+
119
+ }// Rays