rays 0.3.9 → 0.3.10

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.
@@ -0,0 +1,332 @@
1
+ #include "../bitmap.h"
2
+
3
+
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 <SDL.h>
10
+ #include <xot/util.h>
11
+ #include "rays/exception.h"
12
+ #include "../color_space.h"
13
+ #include "../font.h"
14
+ #include "../texture.h"
15
+ #include "../frame_buffer.h"
16
+
17
+
18
+ namespace Rays
19
+ {
20
+
21
+
22
+ struct Bitmap::Data
23
+ {
24
+
25
+ SDL_Surface* surface = NULL;
26
+
27
+ ColorSpace color_space;
28
+
29
+ bool modified;
30
+
31
+ Data ()
32
+ {
33
+ clear();
34
+ }
35
+
36
+ ~Data ()
37
+ {
38
+ clear();
39
+ }
40
+
41
+ void clear ()
42
+ {
43
+ if (surface) SDL_FreeSurface(surface);
44
+
45
+ surface = NULL;
46
+ color_space = COLORSPACE_UNKNOWN;
47
+ modified = false;
48
+ }
49
+
50
+ };// Bitmap::Data
51
+
52
+
53
+ static void
54
+ setup_bitmap (
55
+ Bitmap* bitmap,
56
+ int w, int h, const ColorSpace& cs,
57
+ const void* pixels = NULL, bool clear_pixels = true)
58
+ {
59
+ if (w <= 0)
60
+ argument_error(__FILE__, __LINE__);
61
+ if (h <= 0)
62
+ argument_error(__FILE__, __LINE__);
63
+ if (!cs)
64
+ argument_error(__FILE__, __LINE__);
65
+
66
+ Bitmap::Data* self = bitmap->self.get();
67
+ self->clear();
68
+
69
+ Uint32 r = 0, g = 0, b = 0, a = 0;
70
+ int depth = 0;
71
+ switch (cs.type())
72
+ {
73
+ case RGB_888:
74
+ case RGBA_8888:
75
+ depth = cs.bpp();
76
+ r = 0x000000FF;
77
+ g = 0x0000FF00;
78
+ b = 0x00FF0000;
79
+ if (depth == 32) a = 0xFF000000;
80
+ break;
81
+
82
+ case GRAY_8:
83
+ depth = 8;
84
+ break;
85
+
86
+ default:
87
+ not_implemented_error(__FILE__, __LINE__, "unsupported color space");
88
+ }
89
+
90
+ self->surface = SDL_CreateRGBSurface(0, w, h, depth, r, g, b, a);
91
+ if (!self->surface)
92
+ rays_error(__FILE__, __LINE__, SDL_GetError());
93
+
94
+ self->color_space = cs;
95
+ self->modified = true;
96
+
97
+ if (pixels)
98
+ {
99
+ SDL_LockSurface(self->surface);
100
+ memcpy(self->surface->pixels, pixels, self->surface->pitch * h);
101
+ SDL_UnlockSurface(self->surface);
102
+ }
103
+ else if (clear_pixels)
104
+ SDL_FillRect(self->surface, NULL, 0);
105
+ }
106
+
107
+ Bitmap
108
+ Bitmap_from (const Texture& tex)
109
+ {
110
+ if (!tex)
111
+ argument_error(__FILE__, __LINE__);
112
+
113
+ Bitmap bmp;
114
+ setup_bitmap(
115
+ &bmp, tex.width(), tex.height(), tex.color_space(), NULL, false);
116
+
117
+ GLenum format, type;
118
+ ColorSpace_get_gl_format_and_type(&format, &type, tex.color_space());
119
+
120
+ FrameBuffer fb(tex);
121
+ FrameBufferBinder binder(fb.id());
122
+
123
+ for (int y = 0; y < bmp.height(); ++y)
124
+ {
125
+ GLvoid* ptr = (GLvoid*) bmp.at<uchar>(0, y);
126
+ glReadPixels(0, y, bmp.width(), 1, format, type, ptr);
127
+ }
128
+
129
+ return bmp;
130
+ }
131
+
132
+ void
133
+ Bitmap_draw_string (
134
+ Bitmap* bitmap, const RawFont& font,
135
+ const char* str, coord x, coord y, bool smooth)
136
+ {
137
+ if (!bitmap)
138
+ argument_error(__FILE__, __LINE__);
139
+ if (!*bitmap)
140
+ argument_error(__FILE__, __LINE__);
141
+ if (!font)
142
+ argument_error(__FILE__, __LINE__);
143
+ if (!str)
144
+ argument_error(__FILE__, __LINE__);
145
+
146
+ if (*str == '\0') return;
147
+
148
+ font.draw_string(bitmap->self->surface, bitmap->height(), str, x, y);
149
+ Bitmap_set_modified(bitmap);
150
+ }
151
+
152
+ void
153
+ Bitmap_set_modified (Bitmap* bitmap, bool modified)
154
+ {
155
+ bitmap->self->modified = modified;
156
+ }
157
+
158
+ bool
159
+ Bitmap_get_modified (const Bitmap& bitmap)
160
+ {
161
+ return bitmap.self->modified;
162
+ }
163
+
164
+ static const char*
165
+ get_ext (const char* path)
166
+ {
167
+ if (!path)
168
+ return NULL;
169
+
170
+ return strrchr(path, '.');
171
+ }
172
+
173
+ void
174
+ Bitmap_save (const Bitmap& bmp, const char* path)
175
+ {
176
+ const char* extension = get_ext(path);
177
+ if (!extension)
178
+ {
179
+ argument_error(
180
+ __FILE__, __LINE__, "invalid image file extension: '%s'", path);
181
+ }
182
+
183
+ const auto& cs = bmp.color_space();
184
+ int w = bmp.width();
185
+ int h = bmp.height();
186
+ int pitch = w * cs.Bpp();
187
+
188
+ std::unique_ptr<uchar[]> pixels(new uchar[h * pitch]);
189
+ for (int y = 0; y < h; ++y)
190
+ memcpy(pixels.get() + pitch * y, bmp.at<uchar>(0, y), pitch);
191
+
192
+ String ext = extension;
193
+ ext.downcase();
194
+
195
+ int ret = 0;
196
+ if (ext == ".bmp")
197
+ ret = stbi_write_bmp(path, w, h, cs.Bpp(), pixels.get());
198
+ else if (ext == ".png")
199
+ ret = stbi_write_png(path, w, h, cs.Bpp(), pixels.get(), 0);
200
+ else if (ext == ".jpg" || ext == ".jpeg")
201
+ ret = stbi_write_jpg(path, w, h, cs.Bpp(), pixels.get(), 90);
202
+ else if (ext == ".tga")
203
+ ret = stbi_write_tga(path, w, h, cs.Bpp(), pixels.get());
204
+ else
205
+ argument_error(__FILE__, __LINE__, "unknown image file type");
206
+
207
+ if (!ret)
208
+ rays_error(__FILE__, __LINE__, "failed to save: '%s'", path);
209
+ }
210
+
211
+ Bitmap
212
+ Bitmap_load (const char* path)
213
+ {
214
+ if (!path)
215
+ argument_error(__FILE__, __LINE__);
216
+
217
+ int w = 0, h = 0, Bpp = 0;
218
+ uchar* pixels = stbi_load(path, &w, &h, &Bpp, 0);
219
+ if (!pixels)
220
+ rays_error(__FILE__, __LINE__, "failed to load: '%s'", path);
221
+
222
+ ColorSpace cs;
223
+ switch (Bpp)
224
+ {
225
+ case 1: cs = GRAY_8; break;
226
+ case 3: cs = RGB_888; break;
227
+ case 4: cs = RGBA_8888; break;
228
+ default:
229
+ rays_error(__FILE__, __LINE__, "unsupported image file: '%s'", path);
230
+ }
231
+
232
+ Bitmap bmp(w, h, cs);
233
+ if (!bmp)
234
+ rays_error(__FILE__, __LINE__, "failed to create Bitmap object");
235
+
236
+ int pitch = Bpp * w;
237
+ for (int y = 0; y < h; ++y)
238
+ memcpy(bmp.at<uchar>(0, y), pixels + pitch * y, pitch);
239
+
240
+ return bmp;
241
+ }
242
+
243
+
244
+ Bitmap::Bitmap ()
245
+ {
246
+ }
247
+
248
+ Bitmap::Bitmap (
249
+ int width, int height, const ColorSpace& color_space, const void* pixels)
250
+ {
251
+ setup_bitmap(this, width, height, color_space, pixels);
252
+ }
253
+
254
+ Bitmap::~Bitmap ()
255
+ {
256
+ }
257
+
258
+ Bitmap
259
+ Bitmap::dup () const
260
+ {
261
+ return Bitmap(width(), height(), color_space(), pixels());
262
+ }
263
+
264
+ int
265
+ Bitmap::width () const
266
+ {
267
+ if (!*this) return 0;
268
+ return self->surface->w;
269
+ }
270
+
271
+ int
272
+ Bitmap::height () const
273
+ {
274
+ if (!*this) return 0;
275
+ return self->surface->h;
276
+ }
277
+
278
+ const ColorSpace&
279
+ Bitmap::color_space () const
280
+ {
281
+ if (!*this)
282
+ {
283
+ static const ColorSpace UNKNOWN = COLORSPACE_UNKNOWN;
284
+ return UNKNOWN;
285
+ }
286
+ return self->color_space;
287
+ }
288
+
289
+ int
290
+ Bitmap::pitch () const
291
+ {
292
+ if (!*this) return 0;
293
+ return self->surface->pitch;
294
+ }
295
+
296
+ size_t
297
+ Bitmap::size () const
298
+ {
299
+ return pitch() * height();
300
+ }
301
+
302
+ void*
303
+ Bitmap::pixels ()
304
+ {
305
+ if (!*this) return NULL;
306
+ return self->surface->pixels;
307
+ }
308
+
309
+ const void*
310
+ Bitmap::pixels () const
311
+ {
312
+ return const_cast<This*>(this)->pixels();
313
+ }
314
+
315
+ Bitmap::operator bool () const
316
+ {
317
+ return
318
+ self->surface &&
319
+ self->surface->w > 0 &&
320
+ self->surface->h > 0 &&
321
+ self->surface->pixels &&
322
+ self->color_space;
323
+ }
324
+
325
+ bool
326
+ Bitmap::operator ! () const
327
+ {
328
+ return !operator bool();
329
+ }
330
+
331
+
332
+ }// Rays
@@ -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
data/src/sdl/font.cpp ADDED
@@ -0,0 +1,93 @@
1
+ #include "../font.h"
2
+
3
+
4
+ #include "rays/exception.h"
5
+
6
+
7
+ namespace Rays
8
+ {
9
+
10
+
11
+ struct RawFont::Data
12
+ {
13
+
14
+ String path;
15
+
16
+ };// RawFont::Data
17
+
18
+
19
+ const FontFamilyMap&
20
+ get_font_families ()
21
+ {
22
+ static const FontFamilyMap MAP;
23
+ return MAP;
24
+ }
25
+
26
+ RawFont
27
+ RawFont_load (const char* path, coord size)
28
+ {
29
+ return RawFont();
30
+ }
31
+
32
+
33
+ RawFont::RawFont ()
34
+ {
35
+ }
36
+
37
+ RawFont::RawFont (const char* name, coord size)
38
+ {
39
+ }
40
+
41
+ RawFont::RawFont (const This& obj, coord size)
42
+ {
43
+ }
44
+
45
+ RawFont::~RawFont ()
46
+ {
47
+ }
48
+
49
+ void
50
+ RawFont::draw_string (
51
+ void* context_, coord context_height,
52
+ const char* str, coord x, coord y) const
53
+ {
54
+ }
55
+
56
+ String
57
+ RawFont::name () const
58
+ {
59
+ return "";
60
+ }
61
+
62
+ coord
63
+ RawFont::size () const
64
+ {
65
+ return 0;
66
+ }
67
+
68
+ coord
69
+ RawFont::get_width (const char* str) const
70
+ {
71
+ if (!str || *str == '\0') return 0;
72
+ return 0;
73
+ }
74
+
75
+ coord
76
+ RawFont::get_height (coord* ascent, coord* descent, coord* leading) const
77
+ {
78
+ return 0;
79
+ }
80
+
81
+ RawFont::operator bool () const
82
+ {
83
+ return true;
84
+ }
85
+
86
+ bool
87
+ RawFont::operator ! () const
88
+ {
89
+ return !operator bool();
90
+ }
91
+
92
+
93
+ }// Rays
@@ -0,0 +1,102 @@
1
+ #include "../opengl.h"
2
+
3
+
4
+ #include <SDL.h>
5
+ #include "rays/rays.h"
6
+ #include "rays/exception.h"
7
+
8
+
9
+ namespace Rays
10
+ {
11
+
12
+
13
+ struct OffscreenContext
14
+ {
15
+
16
+ SDL_Window* window = NULL;
17
+
18
+ SDL_GLContext context = NULL;
19
+
20
+ OffscreenContext ()
21
+ {
22
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
23
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
24
+ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
25
+ SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
26
+
27
+ window = SDL_CreateWindow(
28
+ "rays/offscreen", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1, 1,
29
+ SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
30
+ if (!window)
31
+ rays_error(__FILE__, __LINE__, SDL_GetError());
32
+
33
+ context = SDL_GL_CreateContext(window);
34
+ if (!context)
35
+ rays_error(__FILE__, __LINE__, SDL_GetError());
36
+ }
37
+
38
+ ~OffscreenContext ()
39
+ {
40
+ if (context)
41
+ {
42
+ if (context == SDL_GL_GetCurrentContext())
43
+ SDL_GL_MakeCurrent(NULL, NULL);
44
+
45
+ SDL_GL_DeleteContext(context);
46
+ context = NULL;
47
+ }
48
+
49
+ if (window)
50
+ {
51
+ SDL_DestroyWindow(window);
52
+ window = NULL;
53
+ }
54
+ }
55
+
56
+ };// OffscreenContext
57
+
58
+
59
+ static OffscreenContext*
60
+ get_opengl_offscreen_context ()
61
+ {
62
+ static OffscreenContext* context = NULL;
63
+ if (!context) context = new OffscreenContext();
64
+ return context;
65
+ }
66
+
67
+
68
+ void
69
+ OpenGL_init ()
70
+ {
71
+ activate_offscreen_context();
72
+
73
+ static bool glew_initialized = false;
74
+ if (!glew_initialized)
75
+ {
76
+ glew_initialized = true;
77
+ if (glewInit() != GLEW_OK)
78
+ opengl_error(__FILE__, __LINE__, "failed to initialize GLEW.");
79
+ }
80
+ }
81
+
82
+ void
83
+ OpenGL_fin ()
84
+ {
85
+ }
86
+
87
+
88
+ Context
89
+ get_offscreen_context ()
90
+ {
91
+ return get_opengl_offscreen_context();
92
+ }
93
+
94
+ void
95
+ activate_offscreen_context ()
96
+ {
97
+ const auto* c = get_opengl_offscreen_context();
98
+ SDL_GL_MakeCurrent(c->window, c->context);
99
+ }
100
+
101
+
102
+ }// Rays
data/src/sdl/rays.cpp ADDED
@@ -0,0 +1,50 @@
1
+ #include "rays/rays.h"
2
+
3
+
4
+ #include <SDL.h>
5
+ #include "rays/exception.h"
6
+ #include "rays/debug.h"
7
+ #include "../opengl.h"
8
+
9
+
10
+ namespace Rays
11
+ {
12
+
13
+
14
+ namespace global
15
+ {
16
+
17
+ static bool initialized = false;
18
+
19
+ }
20
+
21
+
22
+ void
23
+ init ()
24
+ {
25
+ if (global::initialized)
26
+ rays_error(__FILE__, __LINE__, "already initialized");
27
+
28
+ if (SDL_Init(SDL_INIT_VIDEO) < 0)
29
+ rays_error(__FILE__, __LINE__, SDL_GetError());
30
+
31
+ OpenGL_init();
32
+
33
+ global::initialized = true;
34
+ }
35
+
36
+ void
37
+ fin ()
38
+ {
39
+ if (!global::initialized)
40
+ rays_error(__FILE__, __LINE__, "not initialized");
41
+
42
+ OpenGL_fin();
43
+
44
+ SDL_Quit();
45
+
46
+ global::initialized = false;
47
+ }
48
+
49
+
50
+ }// Rays
data/src/win32/bitmap.cpp CHANGED
@@ -136,7 +136,8 @@ namespace Rays
136
136
 
137
137
  void
138
138
  Bitmap_draw_string (
139
- Bitmap* bitmap, const RawFont& font, const char* str, coord x, coord y)
139
+ Bitmap* bitmap, const RawFont& font,
140
+ const char* str, coord x, coord y, bool smooth)
140
141
  {
141
142
  if (!bitmap)
142
143
  argument_error(__FILE__, __LINE__);
data/src/win32/opengl.cpp CHANGED
@@ -62,7 +62,6 @@ namespace Rays
62
62
 
63
63
  ~OffscreenContext ()
64
64
  {
65
-
66
65
  if (hrc && hrc == wglGetCurrentContext())
67
66
  {
68
67
  if (!wglMakeCurrent(NULL, NULL))
data/test/test_image.rb CHANGED
@@ -124,6 +124,7 @@ class TestImage < Test::Unit::TestCase
124
124
  paths.each {|path| File.delete path}
125
125
 
126
126
  assert_raise(ArgumentError) {img.save 'testimage.unknown'}
127
+ assert_raise(Errno::ENOENT) {load '/nofile.png'}
127
128
  end
128
129
 
129
130
  end# TestImage