rays 0.1.15 → 0.1.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,9 +21,9 @@ namespace Rays
21
21
  }// Rays
22
22
 
23
23
 
24
- RUCY_DECLARE_VALUE_OR_ARRAY_TO(Rays::CapType)
24
+ RUCY_DECLARE_CONVERT_TO(Rays::CapType)
25
25
 
26
- RUCY_DECLARE_VALUE_OR_ARRAY_TO(Rays::JoinType)
26
+ RUCY_DECLARE_CONVERT_TO(Rays::JoinType)
27
27
 
28
28
 
29
29
  #endif//EOH
@@ -10,6 +10,7 @@ require 'rays/color'
10
10
  require 'rays/color_space'
11
11
  require 'rays/matrix'
12
12
 
13
+ require 'rays/painter'
13
14
  require 'rays/polyline'
14
15
  require 'rays/polygon'
15
16
  require 'rays/polygon_line'
@@ -17,5 +18,4 @@ require 'rays/bitmap'
17
18
  require 'rays/image'
18
19
  require 'rays/font'
19
20
  require 'rays/shader'
20
-
21
- require 'rays/painter'
21
+ require 'rays/camera'
@@ -0,0 +1,21 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ require 'xot/block_util'
5
+ require 'rays/ext'
6
+
7
+
8
+ module Rays
9
+
10
+
11
+ class Camera
12
+
13
+ def initialize (*args, &block)
14
+ super *args
15
+ Xot::BlockUtil.instance_eval_or_block_call self, &block if block
16
+ end
17
+
18
+ end# Camera
19
+
20
+
21
+ end# Rays
@@ -1,4 +1,4 @@
1
- #include "rays/image.h"
1
+ #include "image.h"
2
2
 
3
3
 
4
4
  #include <math.h>
@@ -0,0 +1,21 @@
1
+ // -*- mode: c++ -*-
2
+ #pragma once
3
+ #ifndef __RAYS_SRC_OSX_BITMAP_H__
4
+ #define __RAYS_SRC_OSX_BITMAP_H__
5
+
6
+
7
+ #import <CoreGraphics/CGImage.h>
8
+ #include "../bitmap.h"
9
+
10
+
11
+ namespace Rays
12
+ {
13
+
14
+
15
+ void Bitmap_copy_pixels (Bitmap* bitmap, CGImageRef image);
16
+
17
+
18
+ }// Rays
19
+
20
+
21
+ #endif//EOH
@@ -1,5 +1,5 @@
1
1
  // -*- objc -*-
2
- #import "../bitmap.h"
2
+ #import "bitmap.h"
3
3
 
4
4
 
5
5
  #import <ImageIO/CGImageDestination.h>
@@ -175,6 +175,23 @@ namespace Rays
175
175
  return bmp;
176
176
  }
177
177
 
178
+ void
179
+ Bitmap_copy_pixels (Bitmap* bitmap, CGImageRef image)
180
+ {
181
+ if (!bitmap || !image)
182
+ argument_error(__FILE__, __LINE__);
183
+
184
+ CGContextRef context = bitmap->self->get_context();
185
+ if (!context)
186
+ rays_error(__FILE__, __LINE__, "getting CGContext failed.");
187
+
188
+ size_t width = CGImageGetWidth(image);
189
+ size_t height = CGImageGetHeight(image);
190
+ CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
191
+
192
+ Bitmap_set_modified(bitmap);
193
+ }
194
+
178
195
  void
179
196
  Bitmap_draw_string (
180
197
  Bitmap* bitmap, const RawFont& font, const char* str, coord x, coord y)
@@ -185,6 +202,7 @@ namespace Rays
185
202
  if (*str == '\0') return;
186
203
 
187
204
  font.draw_string(bitmap->self->get_context(), bitmap->height(), str, x, y);
205
+
188
206
  Bitmap_set_modified(bitmap);
189
207
  }
190
208
 
@@ -247,11 +265,7 @@ namespace Rays
247
265
  if (!bmp)
248
266
  rays_error(__FILE__, __LINE__, "invalid bitmap.");
249
267
 
250
- CGContextRef context = bmp.self->get_context();
251
- if (!context)
252
- rays_error(__FILE__, __LINE__, "creating CGContext failed.");
253
-
254
- CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
268
+ Bitmap_copy_pixels(&bmp, image);
255
269
  return bmp;
256
270
  }
257
271
 
@@ -329,7 +343,10 @@ namespace Rays
329
343
  Bitmap::operator bool () const
330
344
  {
331
345
  return
332
- self->width > 0 && self->height > 0 && self->color_space && self->pixels;
346
+ self->width > 0 &&
347
+ self->height > 0 &&
348
+ self->color_space &&
349
+ self->pixels;
333
350
  }
334
351
 
335
352
  bool
@@ -0,0 +1,236 @@
1
+ // -*- mode: objc -*-
2
+ #import "rays/camera.h"
3
+
4
+
5
+ #import <AVFoundation/AVFoundation.h>
6
+ #include "bitmap.h"
7
+
8
+
9
+ static int video_input_queue_index = 0;
10
+
11
+
12
+ @interface VideoInput : NSObject <AVCaptureVideoDataOutputSampleBufferDelegate>
13
+ @end
14
+
15
+
16
+ @implementation VideoInput
17
+
18
+ {
19
+ AVCaptureSession* captureSession;
20
+ dispatch_queue_t captureQueue;
21
+ CGImageRef captureImage;
22
+ }
23
+
24
+ - (id) init
25
+ {
26
+ self = [super init];
27
+ if (self)
28
+ {
29
+ captureSession = nil;
30
+ captureQueue = nil;
31
+ captureImage = nil;
32
+ }
33
+ return self;
34
+ }
35
+
36
+ - (void) dealloc
37
+ {
38
+ [self stop];
39
+ [self clearImage];
40
+
41
+ if (captureQueue)
42
+ {
43
+ dispatch_release(captureQueue);
44
+ captureQueue = nil;
45
+ }
46
+
47
+ [super dealloc];
48
+ }
49
+
50
+ - (dispatch_queue_t) queue
51
+ {
52
+ if (!captureQueue)
53
+ {
54
+ auto name = Xot::stringf(
55
+ "org.xord.RaysVideoInputQueue_%d",
56
+ video_input_queue_index++);
57
+ captureQueue = dispatch_queue_create(name, DISPATCH_QUEUE_SERIAL);
58
+ }
59
+ return captureQueue;
60
+ }
61
+
62
+ - (BOOL) start
63
+ {
64
+ [self stop];
65
+
66
+ AVCaptureSession* session = [[[AVCaptureSession alloc] init] autorelease];
67
+ session.sessionPreset = AVCaptureSessionPresetHigh;
68
+
69
+ AVCaptureDevice* device =
70
+ [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
71
+ if (!device) return NO;
72
+
73
+ //device.activeVideoMinFrameDuration = CMTimeMake(1, 30);
74
+
75
+ NSError* error = nil;
76
+ AVCaptureDeviceInput* input = [[[AVCaptureDeviceInput alloc]
77
+ initWithDevice: device error: &error]
78
+ autorelease];
79
+ if (!input || error || ![session canAddInput: input])
80
+ return NO;
81
+
82
+ AVCaptureVideoDataOutput* output =
83
+ [[[AVCaptureVideoDataOutput alloc] init] autorelease];
84
+ output.videoSettings = @{
85
+ (NSString*) kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)
86
+ };
87
+ output.alwaysDiscardsLateVideoFrames = YES;
88
+ [output setSampleBufferDelegate: self queue: self.queue];
89
+ if (![session canAddOutput: output])
90
+ return NO;
91
+
92
+ [session addInput: input];
93
+ [session addOutput: output];
94
+ [session startRunning];
95
+
96
+ captureSession = [session retain];
97
+ return YES;
98
+ }
99
+
100
+ - (void) captureOutput: (AVCaptureOutput*) output
101
+ didOutputSampleBuffer: (CMSampleBufferRef) sampleBuffer
102
+ fromConnection: (AVCaptureConnection*) connection
103
+ {
104
+ CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
105
+ if (!pixelBuffer) return;
106
+
107
+ CIImage* ciImage = [CIImage imageWithCVPixelBuffer: pixelBuffer];
108
+ if (!ciImage) return;
109
+
110
+ CIContext* context = [CIContext contextWithOptions: nil];
111
+ size_t width = CVPixelBufferGetWidth(pixelBuffer);
112
+ size_t height = CVPixelBufferGetHeight(pixelBuffer);
113
+ CGRect rect = CGRectMake(0, 0, width, height);
114
+ CGImageRef cgImage = [context createCGImage: ciImage fromRect: rect];
115
+
116
+ dispatch_async(dispatch_get_main_queue(), ^{
117
+ [self clearImage];
118
+ captureImage = cgImage;
119
+ });
120
+ }
121
+
122
+ - (void) stop
123
+ {
124
+ if (!captureSession) return;
125
+
126
+ [captureSession stopRunning];
127
+ [captureSession release];
128
+ captureSession = nil;
129
+ }
130
+
131
+ - (BOOL) isActive
132
+ {
133
+ return captureSession != nil;
134
+ }
135
+
136
+ - (void) clearImage
137
+ {
138
+ if (!captureImage) return;
139
+
140
+ CGImageRelease(captureImage);
141
+ captureImage = nil;
142
+ }
143
+
144
+ - (CGImageRef) getImage
145
+ {
146
+ return captureImage;
147
+ }
148
+
149
+ @end// VideoInput
150
+
151
+
152
+ namespace Rays
153
+ {
154
+
155
+
156
+ struct Camera::Data
157
+ {
158
+
159
+ mutable Image image;
160
+
161
+ VideoInput* video_input = nil;
162
+
163
+ void update_image_from_video_input () const
164
+ {
165
+ if (!video_input) return;
166
+
167
+ CGImageRef cgImage = [video_input getImage];
168
+ if (!cgImage) return;
169
+
170
+ if (!image)
171
+ {
172
+ Bitmap bmp(
173
+ (int) CGImageGetWidth(cgImage),
174
+ (int) CGImageGetHeight(cgImage));
175
+ image = Image(bmp);
176
+ }
177
+
178
+ Bitmap_copy_pixels(&image.bitmap(), cgImage);
179
+
180
+ [video_input clearImage];
181
+ }
182
+
183
+ };// Camera::Data
184
+
185
+
186
+ Camera::Camera ()
187
+ {
188
+ }
189
+
190
+ Camera::~Camera ()
191
+ {
192
+ stop();
193
+ if (self->video_input) [self->video_input release];
194
+ }
195
+
196
+ bool
197
+ Camera::start ()
198
+ {
199
+ if (!self->video_input) self->video_input = [[VideoInput alloc] init];
200
+ return [self->video_input start];
201
+ }
202
+
203
+ void
204
+ Camera::stop ()
205
+ {
206
+ if (!self->video_input) return;
207
+
208
+ [self->video_input stop];
209
+ }
210
+
211
+ bool
212
+ Camera::is_active () const
213
+ {
214
+ return self->video_input && [self->video_input isActive];
215
+ }
216
+
217
+ const Image*
218
+ Camera::image () const
219
+ {
220
+ self->update_image_from_video_input();
221
+ return self->image ? &self->image : NULL;
222
+ }
223
+
224
+ Camera::operator bool () const
225
+ {
226
+ return true;
227
+ }
228
+
229
+ bool
230
+ Camera::operator ! () const
231
+ {
232
+ return !operator bool();
233
+ }
234
+
235
+
236
+ }// Rays
@@ -0,0 +1,21 @@
1
+ // -*- mode: c++ -*-
2
+ #pragma once
3
+ #ifndef __RAYS_SRC_OSX_BITMAP_H__
4
+ #define __RAYS_SRC_OSX_BITMAP_H__
5
+
6
+
7
+ #import <CoreGraphics/CGImage.h>
8
+ #include "../bitmap.h"
9
+
10
+
11
+ namespace Rays
12
+ {
13
+
14
+
15
+ void Bitmap_copy_pixels (Bitmap* bitmap, CGImageRef image);
16
+
17
+
18
+ }// Rays
19
+
20
+
21
+ #endif//EOH
@@ -1,5 +1,5 @@
1
1
  // -*- objc -*-
2
- #import "../bitmap.h"
2
+ #import "bitmap.h"
3
3
 
4
4
 
5
5
  #import <Cocoa/Cocoa.h>
@@ -174,6 +174,23 @@ namespace Rays
174
174
  return bmp;
175
175
  }
176
176
 
177
+ void
178
+ Bitmap_copy_pixels (Bitmap* bitmap, CGImageRef image)
179
+ {
180
+ if (!bitmap || !image)
181
+ argument_error(__FILE__, __LINE__);
182
+
183
+ CGContextRef context = bitmap->self->get_context();
184
+ if (!context)
185
+ rays_error(__FILE__, __LINE__, "getting CGContext failed.");
186
+
187
+ size_t width = CGImageGetWidth(image);
188
+ size_t height = CGImageGetHeight(image);
189
+ CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
190
+
191
+ Bitmap_set_modified(bitmap);
192
+ }
193
+
177
194
  void
178
195
  Bitmap_draw_string (
179
196
  Bitmap* bitmap, const RawFont& font, const char* str, coord x, coord y)
@@ -184,6 +201,7 @@ namespace Rays
184
201
  if (*str == '\0') return;
185
202
 
186
203
  font.draw_string(bitmap->self->get_context(), bitmap->height(), str, x, y);
204
+
187
205
  Bitmap_set_modified(bitmap);
188
206
  }
189
207
 
@@ -247,11 +265,7 @@ namespace Rays
247
265
  if (!bmp)
248
266
  rays_error(__FILE__, __LINE__, "invalid bitmap.");
249
267
 
250
- CGContextRef context = bmp.self->get_context();
251
- if (!context)
252
- rays_error(__FILE__, __LINE__, "creating CGContext failed.");
253
-
254
- CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
268
+ Bitmap_copy_pixels(&bmp, image);
255
269
  return bmp;
256
270
  }
257
271