19 #include "conversions.h"
22 #include <dc1394/dc1394.h>
44 dc->size[0] =
static_cast<uint32_t
>(yimg.
width());
45 dc->size[1] =
static_cast<uint32_t
>(yimg.
height());
49 dc->color_filter =
static_cast<dc1394color_filter_t
>(filter);
50 dc->yuv_byte_order = 0;
52 dc->stride =
static_cast<uint32_t
>(yimg.
getRowSize());
53 dc->video_mode = DC1394_VIDEO_MODE_640x480_RGB8;
55 dc->padding_bytes = 0;
56 dc->total_bytes = dc->image_bytes;
58 dc->frames_behind = 0;
61 dc->allocated_image_bytes = dc->image_bytes;
62 #ifdef YARP_LITTLE_ENDIAN
63 dc->little_endian = DC1394_TRUE;
65 dc->little_endian = DC1394_FALSE;
67 dc->data_in_padding = DC1394_FALSE;
100 bool ok = in.read(reader);
108 need_reset = (0!=memcmp(&header_in_cmp,&header_in,
sizeof(header_in)));
112 int m = DC1394_BAYER_METHOD_BILINEAR;
115 if (config.
check(
"size")) {
120 if (config.
check(
"method")) {
122 bayer_method_set =
true;
124 m = DC1394_BAYER_METHOD_AHD;
125 }
else if (method==
"bilinear") {
126 m = DC1394_BAYER_METHOD_BILINEAR;
127 }
else if (method==
"downsample") {
128 m = DC1394_BAYER_METHOD_DOWNSAMPLE;
130 }
else if (method==
"edgesense") {
131 m = DC1394_BAYER_METHOD_EDGESENSE;
132 }
else if (method==
"hqlinear") {
133 m = DC1394_BAYER_METHOD_HQLINEAR;
134 }
else if (method==
"nearest") {
135 m = DC1394_BAYER_METHOD_NEAREST;
136 }
else if (method==
"simple") {
137 m = DC1394_BAYER_METHOD_SIMPLE;
138 }
else if (method==
"vng") {
139 m = DC1394_BAYER_METHOD_VNG;
141 yCWarning(BAYERCARRIER,
"bayer method %s not recognized, try: ahd bilinear downsample edgesense hqlinear nearest simple vng", method.c_str());
148 setFormat(config.
check(
"order",
Value(
"grbg")).asString().c_str());
149 header_in.setFromImage(in);
150 yCTrace(BAYERCARRIER,
"Need reset.");
155 local->setSize(
sizeof(header)+image_data_len);
165 if (src.width()%8==0) {
166 dc1394video_frame_t dc_src;
167 dc1394video_frame_t dc_dest;
170 dc1394_debayer_frames(&dc_src,&dc_dest,DC1394_BAYER_METHOD_DOWNSAMPLE);
174 if (bayer_method_set) {
175 yCWarning(BAYERCARRIER,
"Not using dc1394 debayer methods (image width not a multiple of 8)");
180 int h = src.height();
181 int wo = dest.width();
182 int ho = dest.height();
184 int roffx = roff?goff:goff1;
186 int boffx = boff?goff:goff1;
187 for (
int yo=0; yo<ho; yo++) {
188 for (
int xo=0; xo<wo; xo++) {
192 if (x+1>=w-1 || y+1>=h-1) {
196 po.
r = src.
pixel(x+roffx,y+roff);
197 po.
b = src.
pixel(x+boffx,y+boff);
207 if (src.width()%8==0) {
208 dc1394video_frame_t dc_src;
209 dc1394video_frame_t dc_dest;
212 dc1394_debayer_frames(&dc_src,&dc_dest,
213 static_cast<dc1394bayer_method_t
>(bayer_method));
217 if (bayer_method_set) {
218 yCWarning(BAYERCARRIER,
"Not using dc1394 debayer methods (image width not a multiple of 8)");
220 int w = dest.width();
221 int h = dest.height();
223 int roffx = roff?goff:goff1;
225 int boffx = boff?goff:goff1;
226 for (
int y=0; y<h; y++) {
227 for (
int x=0; x<w; x++) {
236 if (x>0) { g += src.
pixel(x-1,y); ct++; }
237 if (x<w-1) { g += src.
pixel(x+1,y); ct++; }
238 if (y>0) { g += src.
pixel(x,y-1); ct++; }
239 if (y<h-1) { g += src.
pixel(x,y+1); ct++; }
240 if (ct>0) { g /= ct; }
241 po.
g =
static_cast<int>(g);
245 if (y%2==boff && x%2==boffx) {
247 }
else if (y%2==boff) {
250 if (x>0) { b += src.
pixel(x-1,y); ct++; }
251 if (x<w-1) { b += src.
pixel(x+1,y); ct++; }
252 if (ct>0) { b /= ct; }
253 po.
b =
static_cast<int>(b);
254 }
else if (x%2==boffx) {
257 if (y>0) { b += src.
pixel(x,y-1); ct++; }
258 if (y<h-1) { b += src.
pixel(x,y+1); ct++; }
259 if (ct>0) { b /= ct; }
260 po.
b =
static_cast<int>(b);
264 if (x>0&&y>0) { b += src.
pixel(x-1,y-1); ct++; }
265 if (x>0&&y<h-1) { b += src.
pixel(x-1,y+1); ct++; }
266 if (x<w-1&&y>0) { b += src.
pixel(x+1,y-1); ct++; }
267 if (x<w-1&&y<h-1) { b += src.
pixel(x+1,y+1); ct++; }
268 if (ct>0) { b /= ct; }
269 po.
b =
static_cast<int>(b);
273 if (y%2==roff && x%2==roffx) {
275 }
else if (y%2==roff) {
278 if (x>0) { r += src.
pixel(x-1,y); ct++; }
279 if (x<w-1) { r += src.
pixel(x+1,y); ct++; }
280 if (ct>0) { r /= ct; }
281 po.
r =
static_cast<int>(r);
282 }
else if (x%2==roffx) {
285 if (y>0) { r += src.
pixel(x,y-1); ct++; }
286 if (y<h-1) { r += src.
pixel(x,y+1); ct++; }
287 if (ct>0) { r /= ct; }
288 po.
r =
static_cast<int>(r);
292 if (x>0&&y>0) { r += src.
pixel(x-1,y-1); ct++; }
293 if (x>0&&y<h-1) { r += src.
pixel(x-1,y+1); ct++; }
294 if (x<w-1&&y>0) { r += src.
pixel(x+1,y-1); ct++; }
295 if (x<w-1&&y<h-1) { r += src.
pixel(x+1,y+1); ct++; }
296 if (ct>0) { r /= ct; }
297 po.
r =
static_cast<int>(r);
305 return const_cast<BayerCarrier*
>(
this)->processBuffered();
310 yCTrace(BAYERCARRIER,
"Copy-based conversion.");
312 out.resize(in.width()/2,in.height()/2);
318 header.setFromImage(out);
319 image_data_len = out.getRawImageSize();
327 memcpy(bytes.
get(),out.getRawImage(),bytes.
length());
330 yCTrace(BAYERCARRIER,
"Copyless conversion");
332 wrap.setQuantum(out.getQuantum());
333 wrap.setExternal(bytes.
get(),out.width(),out.height());
335 debayerHalf(in,wrap);
337 debayerFull(in,wrap);
345 if (consumed<
sizeof(header)) {
347 if (len>
sizeof(header)-consumed) {
348 len =
sizeof(header)-consumed;
350 memcpy(b.
get(),(
reinterpret_cast<char*
>(&header))+consumed,len);
355 if (b.
length()==image_data_len) {
358 consumed += image_data_len;
359 return image_data_len;
363 if (consumed<
sizeof(header)+out.getRawImageSize()) {
365 if (len>
sizeof(header)+out.getRawImageSize()-consumed) {
366 len =
sizeof(header)+out.getRawImageSize()-consumed;
368 memcpy(b.
get(),out.getRawImage()+consumed-
sizeof(header),len);
376 bool BayerCarrier::setFormat(
const char *fmt) {
377 dcformat = DC1394_COLOR_FILTER_GRBG;
382 goff = (f[0]==
'g'||f[0]==
'G')?0:1;
383 roff = (f[0]==
'r'||f[0]==
'R'||f[1]==
'r'||f[1]==
'R')?0:1;
384 if (goff==0&&roff==0) {
385 dcformat = DC1394_COLOR_FILTER_GRBG;
386 }
else if (goff==0&&roff==1) {
387 dcformat = DC1394_COLOR_FILTER_GBRG;
388 }
else if (goff==1&&roff==0) {
389 dcformat = DC1394_COLOR_FILTER_RGGB;
390 }
else if (goff==1&&roff==1) {
391 dcformat = DC1394_COLOR_FILTER_BGGR;