rays 0.2.1 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/rays/bitmap.cpp +99 -32
  3. data/.doc/ext/rays/bounds.cpp +2 -2
  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/font.cpp +4 -3
  8. data/.doc/ext/rays/image.cpp +1 -1
  9. data/.doc/ext/rays/matrix.cpp +4 -4
  10. data/.doc/ext/rays/painter.cpp +1 -1
  11. data/.doc/ext/rays/point.cpp +2 -2
  12. data/.doc/ext/rays/polygon.cpp +3 -3
  13. data/.doc/ext/rays/polyline.cpp +3 -3
  14. data/.doc/ext/rays/rays.cpp +10 -10
  15. data/.doc/ext/rays/shader.cpp +2 -2
  16. data/.github/workflows/release-gem.yml +1 -1
  17. data/.github/workflows/test.yml +3 -0
  18. data/ChangeLog.md +5 -0
  19. data/Rakefile +17 -2
  20. data/VERSION +1 -1
  21. data/ext/rays/bitmap.cpp +99 -32
  22. data/ext/rays/bounds.cpp +2 -2
  23. data/ext/rays/camera.cpp +1 -1
  24. data/ext/rays/color.cpp +2 -2
  25. data/ext/rays/color_space.cpp +3 -3
  26. data/ext/rays/defs.h +2 -0
  27. data/ext/rays/extconf.rb +4 -2
  28. data/ext/rays/font.cpp +4 -3
  29. data/ext/rays/image.cpp +1 -1
  30. data/ext/rays/matrix.cpp +4 -4
  31. data/ext/rays/painter.cpp +1 -1
  32. data/ext/rays/point.cpp +2 -2
  33. data/ext/rays/polygon.cpp +3 -3
  34. data/ext/rays/polyline.cpp +3 -3
  35. data/ext/rays/rays.cpp +10 -10
  36. data/ext/rays/shader.cpp +2 -2
  37. data/include/rays/defs.h +7 -0
  38. data/include/rays/ruby/bitmap.h +2 -2
  39. data/include/rays/ruby/bounds.h +2 -2
  40. data/include/rays/ruby/camera.h +2 -2
  41. data/include/rays/ruby/color.h +2 -2
  42. data/include/rays/ruby/color_space.h +2 -2
  43. data/include/rays/ruby/exception.h +3 -3
  44. data/include/rays/ruby/font.h +2 -2
  45. data/include/rays/ruby/image.h +2 -2
  46. data/include/rays/ruby/matrix.h +2 -2
  47. data/include/rays/ruby/painter.h +2 -2
  48. data/include/rays/ruby/point.h +2 -2
  49. data/include/rays/ruby/polygon.h +2 -2
  50. data/include/rays/ruby/polyline.h +2 -2
  51. data/include/rays/ruby/rays.h +6 -6
  52. data/include/rays/ruby/shader.h +2 -2
  53. data/lib/rays/bitmap.rb +7 -0
  54. data/lib/rays/extension.rb +4 -0
  55. data/rays.gemspec +2 -2
  56. data/src/coord.h +2 -2
  57. data/src/font.cpp +1 -0
  58. data/src/ios/bitmap.mm +23 -30
  59. data/src/ios/font.mm +4 -1
  60. data/src/ios/rays.mm +2 -2
  61. data/src/matrix.h +1 -1
  62. data/src/opengl.h +1 -2
  63. data/src/osx/bitmap.mm +23 -30
  64. data/src/osx/font.mm +4 -1
  65. data/src/osx/rays.mm +2 -2
  66. data/src/painter.cpp +1 -0
  67. data/src/shader.cpp +3 -0
  68. data/src/win32/bitmap.cpp +167 -65
  69. data/src/win32/camera.cpp +119 -0
  70. data/src/win32/font.cpp +179 -40
  71. data/src/win32/gdi.h +1 -1
  72. data/src/win32/opengl.cpp +127 -0
  73. data/src/win32/rays.cpp +16 -9
  74. data/test/helper.rb +2 -0
  75. data/test/test_bitmap.rb +3 -1
  76. data/test/test_image.rb +8 -14
  77. data/test/test_painter.rb +4 -4
  78. metadata +7 -6
  79. 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,55 @@ 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
59
55
 
60
- static bool
61
- setup_bitmap (Bitmap* bmp, int w, int h, const ColorSpace& cs, HDC hdc = NULL)
56
+
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 || h <= 0 || !cs)
64
+ argument_error(__FILE__, __LINE__);
65
+
66
+ Bitmap::Data* self = bitmap->self.get();
65
67
 
66
- Bitmap::Data* self = bmp->self.get();
68
+ self->clear();
67
69
 
68
- self->width = w;
69
- self->height = h;
70
- self->colorspace = cs;
71
- self->pitch = self->width * self->colorspace.Bpp();
70
+ self->width = w;
71
+ self->height = h;
72
+ self->pitch = w * cs.Bpp();
73
+ self->color_space = cs;
74
+ self->modified = true;
72
75
 
73
76
  int padding = 4 - self->pitch % 4;
74
77
  if (padding < 4) self->pitch += padding;
@@ -81,32 +84,49 @@ namespace Rays
81
84
  header.biWidth = self->width;
82
85
  header.biHeight = -self->height;
83
86
  header.biPlanes = 1;
84
- header.biBitCount = self->colorspace.bpp();
87
+ header.biBitCount = self->color_space.bpp();
85
88
  header.biCompression = BI_RGB;
86
89
 
87
90
  Win32::DC dc = hdc ? Win32::DC(hdc) : Win32::screen_dc();
88
91
 
89
92
  HBITMAP hbmp = CreateDIBSection(
90
93
  dc.handle(), &bmpinfo, DIB_RGB_COLORS, (void**) &self->pixels, NULL, 0);
91
- if (!hbmp) return false;
94
+ if (!hbmp)
95
+ rays_error(__FILE__, __LINE__);
92
96
 
93
97
  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__);
98
+ if (!self->memdc)
99
+ rays_error(__FILE__, __LINE__);
100
+
101
+ size_t size = self->pitch * self->height;
102
+ if (pixels)
103
+ memcpy(self->pixels, pixels, size);
104
+ else if (clear_pixels)
105
+ memset(self->pixels, 0, size);
103
106
  }
104
107
 
105
108
  Bitmap
106
- Bitmap_from (const Texture& texture)
109
+ Bitmap_from (const Texture& tex)
107
110
  {
111
+ if (!tex)
112
+ argument_error(__FILE__, __LINE__);
113
+
108
114
  Bitmap bmp;
109
- setup_bitmap(&bmp, texture);
115
+ setup_bitmap(
116
+ &bmp, tex.width(), tex.height(), tex.color_space(), NULL, false);
117
+
118
+ GLenum format, type;
119
+ ColorSpace_get_gl_format_and_type(&format, &type, tex.color_space());
120
+
121
+ FrameBuffer fb(tex);
122
+ FrameBufferBinder binder(fb.id());
123
+
124
+ for (int y = 0; y < bmp.height(); ++y)
125
+ {
126
+ GLvoid* ptr = (GLvoid*) bmp.at<uchar>(0, y);
127
+ glReadPixels(0, y, bmp.width(), 1, format, type, ptr);
128
+ }
129
+
110
130
  return bmp;
111
131
  }
112
132
 
@@ -126,8 +146,6 @@ namespace Rays
126
146
  void
127
147
  Bitmap_set_modified (Bitmap* bitmap, bool modified)
128
148
  {
129
- assert(bitmap);
130
-
131
149
  bitmap->self->modified = modified;
132
150
  }
133
151
 
@@ -137,16 +155,83 @@ namespace Rays
137
155
  return bitmap.self->modified;
138
156
  }
139
157
 
140
- bool
141
- Bitmap_save (const Bitmap& bitmap, const char* path)
158
+ static const char*
159
+ get_ext (const char* path)
142
160
  {
143
- return false;
161
+ if (!path)
162
+ return NULL;
163
+
164
+ return strrchr(path, '.');
144
165
  }
145
166
 
146
- bool
147
- Bitmap_load (Bitmap* bitmap, const char* path)
167
+ void
168
+ Bitmap_save (const Bitmap& bmp, const char* path)
148
169
  {
149
- return false;
170
+ const char* ext = get_ext(path);
171
+ if (!ext)
172
+ {
173
+ argument_error(
174
+ __FILE__, __LINE__, "invalid image file extension: '%s'", path);
175
+ }
176
+
177
+ const auto& cs = bmp.color_space();
178
+ size_t w = bmp.width();
179
+ size_t h = bmp.height();
180
+ size_t pitch = w * cs.Bpp();
181
+
182
+ std::unique_ptr<uchar[]> pixels(new uchar[h * pitch]);
183
+ for (size_t y = 0; y < h; ++y)
184
+ memcpy(pixels.get() + pitch * y, bmp.at<uchar>(0, y), pitch);
185
+
186
+ int ret = 0;
187
+ if (stricmp(ext, ".bmp") == 0)
188
+ ret = stbi_write_bmp(path, w, h, cs.Bpp(), pixels.get());
189
+ else
190
+ if (stricmp(ext, ".png") == 0)
191
+ ret = stbi_write_png(path, w, h, cs.Bpp(), pixels.get(), 0);
192
+ else
193
+ if (stricmp(ext, ".jpg") == 0 || stricmp(ext, ".jpeg") == 0)
194
+ ret = stbi_write_jpg(path, w, h, cs.Bpp(), pixels.get(), 90);
195
+ else
196
+ if (stricmp(ext, ".tga") == 0)
197
+ ret = stbi_write_tga(path, w, h, cs.Bpp(), pixels.get());
198
+ else
199
+ argument_error(__FILE__, __LINE__, "unknown image file type");
200
+
201
+ if (!ret)
202
+ rays_error(__FILE__, __LINE__, "failed to save: '%s'", path);
203
+ }
204
+
205
+ Bitmap
206
+ Bitmap_load (const char* path)
207
+ {
208
+ if (!path)
209
+ argument_error(__FILE__, __LINE__);
210
+
211
+ int w = 0, h = 0, Bpp = 0;
212
+ uchar* pixels = stbi_load(path, &w, &h, &Bpp, 0);
213
+ if (!pixels)
214
+ rays_error(__FILE__, __LINE__, "failed to load: '%s'", path);
215
+
216
+ ColorSpace cs;
217
+ switch (Bpp)
218
+ {
219
+ case 1: cs = GRAY_8; break;
220
+ case 3: cs = RGB_888; break;
221
+ case 4: cs = RGBA_8888; break;
222
+ default:
223
+ rays_error(__FILE__, __LINE__, "unsupported image file: '%s'", path);
224
+ }
225
+
226
+ Bitmap bmp(w, h, cs);
227
+ if (!bmp)
228
+ rays_error(__FILE__, __LINE__, "failed to create Bitmap object");
229
+
230
+ int pitch = Bpp * w;
231
+ for (int y = 0; y < h; ++y)
232
+ memcpy(bmp.at<uchar>(0, y), pixels + pitch * y, pitch);
233
+
234
+ return bmp;
150
235
  }
151
236
 
152
237
 
@@ -154,31 +239,45 @@ namespace Rays
154
239
  {
155
240
  }
156
241
 
157
- Bitmap::Bitmap (int width, int height, const ColorSpace& cs)
242
+ Bitmap::Bitmap (
243
+ int width, int height, const ColorSpace& color_space, const void* pixels)
158
244
  {
159
- setup_bitmap(this, width, height, cs);
245
+ setup_bitmap(this, width, height, color_space, pixels);
160
246
  }
161
247
 
162
248
  Bitmap::~Bitmap ()
163
249
  {
164
250
  }
165
251
 
252
+ Bitmap
253
+ Bitmap::dup () const
254
+ {
255
+ return Bitmap(width(), height(), color_space(), pixels());
256
+ }
257
+
166
258
  int
167
259
  Bitmap::width () const
168
260
  {
261
+ if (!*this) return 0;
169
262
  return self->width;
170
263
  }
171
264
 
172
265
  int
173
266
  Bitmap::height () const
174
267
  {
268
+ if (!*this) return 0;
175
269
  return self->height;
176
270
  }
177
271
 
178
272
  const ColorSpace&
179
273
  Bitmap::color_space () const
180
274
  {
181
- return self->colorspace;
275
+ if (!*this)
276
+ {
277
+ static const ColorSpace UNKNOWN = COLORSPACE_UNKNOWN;
278
+ return UNKNOWN;
279
+ }
280
+ return self->color_space;
182
281
  }
183
282
 
184
283
  int
@@ -196,6 +295,7 @@ namespace Rays
196
295
  void*
197
296
  Bitmap::pixels ()
198
297
  {
298
+ if (!*this) return NULL;
199
299
  return self->pixels;
200
300
  }
201
301
 
@@ -208,9 +308,11 @@ namespace Rays
208
308
  Bitmap::operator bool () const
209
309
  {
210
310
  return
211
- self &&
212
- self->width > 0 && self->height > 0 && self->pitch > 0 &&
213
- self->colorspace && self->pixels && self->memdc;
311
+ self->width > 0 &&
312
+ self->height > 0 &&
313
+ self->pitch > 0 &&
314
+ self->color_space &&
315
+ self->pixels;
214
316
  }
215
317
 
216
318
  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