e430c071058df5b1789f2b9de103631ae8b1754f
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcbitmap.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcbitmap.h"
23 #include "bcipc.h"
24 #include "bcresources.h"
25 #include "bcsignals.h"
26 #include "bcwindow.h"
27 #include "bccmodels.h"
28 #include "vframe.h"
29
30 #include <string.h>
31 #include <unistd.h>
32 #include <X11/extensions/Xvlib.h>
33
34 int BC_Bitmap::max_active_buffers = 0;
35 int BC_Bitmap::zombies = 0;
36
37
38 BC_BitmapImage::BC_BitmapImage(BC_Bitmap *bitmap, int index)
39 {
40         this->index = index;
41         this->bitmap = bitmap;
42         this->top_level = bitmap->top_level;
43         this->drawable = 0;
44         this->data = 0;
45         this->row_data = 0;
46         this->dataSize = 0;
47         this->bytesPerLine = 0;
48         this->bitsPerPixel = 0;
49 }
50
51 BC_BitmapImage::~BC_BitmapImage()
52 {
53         delete [] data;
54         delete [] row_data;
55 }
56
57 bool BC_BitmapImage::is_avail()
58 {
59         return list == &bitmap->avail;
60 }
61
62 BC_BitmapImage *BC_Bitmap::cur_bfr()
63 {
64         if( !active_bfr ) {
65                 avail_lock->lock("BC_Bitmap::cur_bfr 1");
66                 if( (!use_shm || top_level->is_running()) &&
67                      (active_bfr=avail.first) != 0 )
68                         avail.remove_pointer(active_bfr);
69                 else {
70                         update_buffers(buffer_count+1, 0);
71                         if( (active_bfr=avail.first) != 0 )
72                                 avail.remove_pointer(active_bfr);
73                         else
74                                 active_bfr = new_buffer(type, -1);
75                 }
76                 avail_lock->unlock();
77         }
78         return active_bfr;
79 }
80
81 int BC_BitmapImage::
82 read_frame_rgb(VFrame *frame)
83 {
84         int w = bitmap->w, h = bitmap->h;
85         if( frame->get_w() != w || frame->get_h() != h ||
86             frame->get_color_model() != BC_RGB888 )
87                 frame->reallocate(0, 0, 0, 0, 0, w, h, BC_RGB888, -1);
88         uint8_t *dp = frame->get_data();
89         int rmask = ximage->red_mask;
90         int gmask = ximage->green_mask;
91         int bmask = ximage->blue_mask;
92 //due to a bug in the xserver, the mask is being cleared by XShm.C:402
93 //  when a faulty data in a XShmGetImage reply returns a zero visual
94 //workaround, use the original visual for the mask data
95         if ( !rmask ) rmask = bitmap->top_level->vis->red_mask;
96         if ( !gmask ) gmask = bitmap->top_level->vis->green_mask;
97         if ( !bmask ) bmask = bitmap->top_level->vis->blue_mask;
98         double fr = 255.0 / rmask;
99         double fg = 255.0 / gmask;
100         double fb = 255.0 / bmask;
101         int nbyte = (ximage->bits_per_pixel+7)/8;
102         int b0 = ximage->byte_order == MSBFirst ? 0 : nbyte-1;
103         int bi = ximage->byte_order == MSBFirst ? 1 : -1;
104         uint8_t *data = (uint8_t *)ximage->data;
105
106         for( int y=0;  y<h; ++y ) {
107                 uint8_t *cp = data + y*ximage->bytes_per_line + b0;
108                 for( int x=0; x<w; ++x, cp+=nbyte ) {
109                         uint8_t *bp = cp;  uint32_t n = *bp;
110                         for( int i=nbyte; --i>0; n=(n<<8)|*bp ) bp += bi;
111                         *dp++ = (int)((n&rmask)*fr + 0.5);
112                         *dp++ = (int)((n&gmask)*fg + 0.5);
113                         *dp++ = (int)((n&bmask)*fb + 0.5);
114                 }
115         }
116         return 0;
117 }
118
119 void BC_Bitmap::reque(BC_BitmapImage *bfr)
120 {
121         avail_lock->lock("BC_Bitmap::reque");
122         --active_buffers;
123         avail.append(bfr);
124         avail_lock->unlock();
125 }
126
127 BC_XvShmImage::BC_XvShmImage(BC_Bitmap *bitmap, int index,
128         int w, int h, int color_model)
129  : BC_BitmapImage(bitmap, index)
130 {
131         Display *display = top_level->get_display();
132         int id = BC_CModels::bc_to_x(color_model);
133 // Create the XvImage
134         xv_image = XvShmCreateImage(display, bitmap->xv_portid, id,
135                         0, w, h, &shm_info);
136         dataSize = xv_image->data_size;
137 // Create the shared memory
138         shm_info.shmid = shmget(IPC_PRIVATE,
139                 dataSize + 8, IPC_CREAT | 0777);
140         if(shm_info.shmid < 0)
141                 perror("BC_XvShmImage::BC_XvShmImage shmget");
142         data = (unsigned char *)shmat(shm_info.shmid, NULL, 0);
143 // This causes it to automatically delete when the program exits.
144         shmctl(shm_info.shmid, IPC_RMID, 0);
145 // setting ximage->data stops BadValue
146         xv_image->data = shm_info.shmaddr = (char*)data;
147         shm_info.readOnly = 0;
148 // Get the real parameters
149         w = xv_image->width;
150         h = xv_image->height;
151         if(!XShmAttach(top_level->display, &shm_info))
152                 perror("BC_XvShmImage::BC_XvShmImage XShmAttach");
153         if( color_model == BC_YUV422 || color_model == BC_UVY422 ) {
154                 bytesPerLine = w*2;
155                 bitsPerPixel = 12;
156                 row_data = new unsigned char*[h];
157                 for( int i=0; i<h; ++i )
158                         row_data[i] = &data[i*bytesPerLine];
159         }
160 }
161
162 BC_XvShmImage::~BC_XvShmImage()
163 {
164         data = 0;
165         XFree(xv_image);
166         XShmDetach(top_level->display, &shm_info);
167         shmdt(shm_info.shmaddr);
168 }
169
170
171 BC_XShmImage::BC_XShmImage(BC_Bitmap *bitmap, int index,
172         int w, int h, int color_model)
173  : BC_BitmapImage(bitmap, index)
174 {
175         Display *display = top_level->display;
176         Visual *visual = top_level->vis;
177         int default_depth = bitmap->get_default_depth();
178         ximage = XShmCreateImage(display, visual,
179                 default_depth, default_depth == 1 ? XYBitmap : ZPixmap,
180                 (char*)NULL, &shm_info, w, h);
181 // Create shared memory
182         bytesPerLine = ximage->bytes_per_line;
183         bitsPerPixel = ximage->bits_per_pixel;
184         dataSize = h * bytesPerLine;
185         shm_info.shmid = shmget(IPC_PRIVATE,
186                 dataSize + 8, IPC_CREAT | 0777);
187         if(shm_info.shmid < 0)
188                 perror("BC_XShmImage::BC_XShmImage shmget");
189         data = (unsigned char *)shmat(shm_info.shmid, NULL, 0);
190 // This causes it to automatically delete when the program exits.
191         shmctl(shm_info.shmid, IPC_RMID, 0);
192         ximage->data = shm_info.shmaddr = (char*)data;
193         shm_info.readOnly = 0;
194 // Get the real parameters
195         if(!XShmAttach(top_level->display, &shm_info))
196                 perror("BC_XShmImage::BC_XShmImage XShmAttach");
197         row_data = new unsigned char*[h];
198         for( int i=0; i<h; ++i )
199                 row_data[i] = &data[i*bytesPerLine];
200 }
201
202 BC_XShmImage::~BC_XShmImage()
203 {
204         data = 0;
205         ximage->data = 0;
206         XDestroyImage(ximage);
207         XShmDetach(top_level->display, &shm_info);
208         XFlush(top_level->display);
209         shmdt(shm_info.shmaddr);
210 }
211
212
213
214 BC_XvImage::BC_XvImage(BC_Bitmap *bitmap, int index,
215         int w, int h, int color_model)
216  : BC_BitmapImage(bitmap, index)
217 {
218         Display *display = top_level->display;
219         int id = BC_CModels::bc_to_x(color_model);
220         xv_image = XvCreateImage(display, bitmap->xv_portid, id, 0, w, h);
221         dataSize = xv_image->data_size;
222         long data_sz = dataSize + 8;
223         data = new unsigned char[data_sz];
224         memset(data, 0, data_sz);
225         xv_image->data = (char *) data;
226         w = xv_image->width;
227         h = xv_image->height;
228         if( color_model == BC_YUV422 || color_model == BC_UVY422 ) {
229                 int bytesPerLine = w*2;
230                 row_data = new unsigned char*[h];
231                 for( int i=0; i<h; ++i )
232                         row_data[i] = &data[i*bytesPerLine];
233         }
234 }
235
236 BC_XvImage::~BC_XvImage()
237 {
238         XFree(xv_image);
239 }
240
241
242 BC_XImage::BC_XImage(BC_Bitmap *bitmap, int index,
243         int w, int h, int color_model)
244  : BC_BitmapImage(bitmap, index)
245 {
246         Display *display = top_level->display;
247         Visual *visual = top_level->vis;
248         int default_depth = bitmap->get_default_depth();
249         ximage = XCreateImage(display, visual, default_depth,
250                 default_depth == 1 ? XYBitmap : ZPixmap,
251                 0, (char*)data, w, h, 8, 0);
252         bytesPerLine = ximage->bytes_per_line;
253         bitsPerPixel = ximage->bits_per_pixel;
254         dataSize = h * bytesPerLine;
255         long data_sz = dataSize + 8;
256         data = new unsigned char[data_sz];
257         memset(data, 0, data_sz);
258         ximage->data = (char*) data;
259         row_data = new unsigned char*[h];
260         for( int i=0; i<h; ++i )
261                 row_data[i] = &data[i*bytesPerLine];
262 }
263
264 BC_XImage::~BC_XImage()
265 {
266         delete [] data;
267         data = 0;
268         ximage->data = 0;
269         XDestroyImage(ximage);
270 }
271
272
273 BC_Bitmap::BC_Bitmap(BC_WindowBase *parent_window, unsigned char *png_data, double scale)
274 {
275 // Decompress data into a temporary vframe
276         VFramePng frame(png_data, scale);
277
278         avail_lock = 0;
279 // Initialize the bitmap
280         initialize(parent_window,
281                 frame.get_w(),
282                 frame.get_h(),
283                 parent_window->get_color_model(),
284                 0);
285
286 // Copy the vframe to the bitmap
287         read_frame(&frame, 0, 0, w, h);
288 }
289
290 BC_Bitmap::BC_Bitmap(BC_WindowBase *parent_window, VFrame *frame)
291 {
292         avail_lock = 0;
293 // Initialize the bitmap
294         initialize(parent_window,
295                 frame->get_w(),
296                 frame->get_h(),
297                 parent_window->get_color_model(),
298                 0);
299
300 // Copy the vframe to the bitmap
301         read_frame(frame, 0, 0, w, h);
302 }
303
304 BC_Bitmap::BC_Bitmap(BC_WindowBase *parent_window,
305         int w, int h, int color_model, int use_shm)
306 {
307         avail_lock = 0;
308         initialize(parent_window, w, h, color_model, use_shm);
309 }
310
311 BC_Bitmap::~BC_Bitmap()
312 {
313         delete_data();
314         delete avail_lock;
315 }
316
317 int BC_Bitmap::initialize(BC_WindowBase *parent_window,
318         int w, int h, int color_model, int use_shm)
319 {
320         BC_Resources *resources = parent_window->get_resources();
321         this->parent_window = parent_window;
322         this->top_level = parent_window->top_level;
323         this->xv_portid = resources->use_xvideo ? top_level->xvideo_port_id : -1;
324         if( w < 1 ) w = 1;
325         if( h < 1 ) h = 1;
326         this->w = w;
327         this->h = h;
328         this->color_model = color_model;
329         this->use_shm = !use_shm ? 0 : need_shm();
330         this->shm_reply = this->use_shm && resources->shm_reply ? 1 : 0;
331         // dont use shm for less than one page
332         this->bg_color = parent_window->bg_color;
333         if( !this->avail_lock )
334                 this->avail_lock = new Mutex("BC_Bitmap::avail_lock");
335         else
336                 this->avail_lock->reset();
337         this->buffers = 0;
338
339         this->active_bfr = 0;
340         this->buffer_count = 0;
341         this->max_buffer_count = 0;
342         this->active_buffers = 0;
343         allocate_data();
344         return 0;
345 }
346
347 int BC_Bitmap::match_params(int w, int h, int color_model, int use_shm)
348 {
349         if( use_shm ) use_shm = need_shm();
350         if(this->w /* != */ < w || this->h /* != */ < h ||
351            this->color_model != color_model || this->use_shm != use_shm) {
352                 delete_data();
353                 initialize(parent_window, w, h, color_model, use_shm);
354         }
355
356         return 0;
357 }
358
359 int BC_Bitmap::params_match(int w, int h, int color_model, int use_shm)
360 {
361         int result = 0;
362         if( use_shm ) use_shm = need_shm();
363         if(this->w == w && this->h == h && this->color_model == color_model) {
364                 if(use_shm == this->use_shm || use_shm == BC_INFINITY)
365                         result = 1;
366         }
367         return result;
368 }
369
370 BC_BitmapImage *BC_Bitmap::new_buffer(int type, int idx)
371 {
372         BC_BitmapImage *buffer = 0;
373         if( idx < 0 ) {
374                 if( type == bmXShmImage ) type = bmXImage;
375                 else if( type ==  bmXvShmImage ) type = bmXvImage;
376         }
377         switch( type ) {
378         default:
379         case bmXImage:
380                 buffer = new BC_XImage(this, idx, w, h, color_model);
381                 break;
382         case bmXvImage:
383                 buffer = new BC_XvImage(this, idx, w, h, color_model);
384                 break;
385         case bmXShmImage:
386                 buffer = new BC_XShmImage(this, idx, w, h, color_model);
387                 break;
388         case bmXvShmImage:
389                 buffer = new BC_XvShmImage(this, idx, w, h, color_model);
390                 break;
391         }
392         if( buffer->is_zombie() ) ++zombies;
393         return buffer;
394 }
395
396 void BC_Bitmap::
397 update_buffers(int count, int lock_avail)
398 {
399         int i;
400         // can deadlock in XReply (libxcb bug) without this lock
401         //top_level->lock_window("BC_Bitmap::update_buffers");
402         if( lock_avail ) avail_lock->lock("BC_Bitmap::update_buffers");
403         if( count > max_buffer_count ) count = max_buffer_count;
404         BC_BitmapImage **new_buffers = !count ? 0 : new BC_BitmapImage *[count];
405         if( buffer_count < count ) {
406                 for( i=0; i<buffer_count; ++i )
407                         new_buffers[i] = buffers[i];
408                 while( i < count ) {
409                         BC_BitmapImage *buffer = new_buffer(type, i);
410                         new_buffers[i] = buffer;
411                         avail.append(buffer);
412                         ++i;
413                 }
414         }
415         else {
416                 for( i=0; i<count; ++i )
417                         new_buffers[i] = buffers[i];
418                 while( i < buffer_count ) {
419                         BC_BitmapImage *buffer = buffers[i];
420                         if( buffer == active_bfr ) active_bfr = 0;
421                         if( buffer->is_avail() ) {
422                                 delete buffer;
423                         }
424                         else {
425                                 ++zombies;
426                                 buffer->index = -1;
427                         }
428                         ++i;
429                 }
430         }
431         delete [] buffers;
432         buffers = new_buffers;
433         buffer_count = count;
434         XFlush(top_level->display);
435         if( lock_avail ) avail_lock->unlock();
436         //top_level->unlock_window();
437 }
438
439 int BC_Bitmap::allocate_data()
440 {
441         int count = 1;
442         max_buffer_count = MAX_BITMAP_BUFFERS;
443         if(use_shm) { // Use shared memory.
444                 int bsz = best_buffer_size();
445                 if( bsz >= 0x800000 ) max_buffer_count = 2;
446                 else if( bsz >= 0x400000 ) max_buffer_count /= 8;
447                 else if( bsz >= 0x100000 ) max_buffer_count /= 4;
448                 else if( bsz >= 0x10000 ) max_buffer_count /= 2;
449                 type = hardware_scaling() ? bmXvShmImage : bmXShmImage;
450                 count = MIN_BITMAP_BUFFERS;
451         }
452         else // use unshared memory.
453                 type = hardware_scaling() ? bmXvImage : bmXImage;
454         update_buffers(count);
455         return 0;
456 }
457
458 int BC_Bitmap::delete_data()
459 {
460 //printf("BC_Bitmap::delete_data 1\n");
461         update_buffers(0);
462         active_bfr = 0;
463         buffer_count = 0;
464         max_buffer_count = 0;
465         return 0;
466 }
467
468 int BC_Bitmap::get_default_depth()
469 {
470         return color_model == BC_TRANSPARENCY ? 1 : top_level->default_depth;
471 }
472
473 long BC_Bitmap::best_buffer_size()
474 {
475         long pixelsize = BC_CModels::calculate_pixelsize(get_color_model());
476         return pixelsize * w * h;
477 }
478
479 int BC_Bitmap::need_shm()
480 {
481 // dont use shm for less than one page
482         return best_buffer_size() < 0x1000 ? 0 :
483                 parent_window->get_resources()->use_shm > 0 ? 1 : 0;
484 }
485
486 int BC_Bitmap::set_bg_color(int color)
487 {
488         this->bg_color = color;
489         return 0;
490 }
491
492 int BC_Bitmap::invert()
493 {
494         for( int j=0; j<buffer_count; ++j ) {
495                 unsigned char **rows = buffers[j]->get_row_data();
496                 if( !rows ) continue;
497                 long bytesPerLine = buffers[j]->bytes_per_line();
498                 for( int k=0; k<h; ++k ) {
499                         unsigned char *bp = rows[k];
500                         for( int i=0; i<bytesPerLine; ++i )
501                                 *bp++ ^= 0xff;
502                 }
503         }
504         return 0;
505 }
506
507 int BC_XShmImage::get_shm_size()
508 {
509         return bytesPerLine * bitmap->h;
510 }
511
512 int BC_Bitmap::write_drawable(Drawable &pixmap, GC &gc,
513         int dest_x, int dest_y, int source_x, int source_y,
514         int dest_w, int dest_h, int dont_wait)
515 {
516         return write_drawable(pixmap, gc,
517                 source_x, source_y, get_w() - source_x, get_h() - source_y,
518                 dest_x, dest_y, dest_w, dest_h, dont_wait);
519 }
520
521 int BC_XImage::write_drawable(Drawable &pixmap, GC &gc,
522         int source_x, int source_y, int source_w, int source_h,
523         int dest_x, int dest_y, int dest_w, int dest_h)
524 {
525         XPutImage(top_level->display, pixmap, gc,
526                 ximage, source_x, source_y,
527                 dest_x, dest_y, dest_w, dest_h);
528         return 0;
529 }
530
531 int BC_XShmImage::write_drawable(Drawable &pixmap, GC &gc,
532         int source_x, int source_y, int source_w, int source_h,
533         int dest_x, int dest_y, int dest_w, int dest_h)
534 {
535         XShmPutImage(top_level->display, pixmap, gc,
536                 ximage, source_x, source_y,
537                 dest_x, dest_y, dest_w, dest_h, bitmap->shm_reply);
538         return 0;
539 }
540
541 int BC_XvImage::write_drawable(Drawable &pixmap, GC &gc,
542         int source_x, int source_y, int source_w, int source_h,
543         int dest_x, int dest_y, int dest_w, int dest_h)
544 {
545         XvPutImage(top_level->display,
546                 bitmap->xv_portid, pixmap, gc, xv_image,
547                 source_x, source_y, source_w, source_h,
548                 dest_x, dest_y, dest_w, dest_h);
549         return 0;
550 }
551
552 int BC_XvShmImage::write_drawable(Drawable &pixmap, GC &gc,
553         int source_x, int source_y, int source_w, int source_h,
554         int dest_x, int dest_y, int dest_w, int dest_h)
555 {
556         XvShmPutImage(top_level->display,
557                 bitmap->xv_portid, pixmap, gc, xv_image,
558                 source_x, source_y, source_w, source_h,
559                 dest_x, dest_y, dest_w, dest_h, bitmap->shm_reply);
560         return 0;
561 }
562
563 int BC_Bitmap::write_drawable(Drawable &pixmap, GC &gc,
564                 int source_x, int source_y, int source_w, int source_h,
565                 int dest_x, int dest_y, int dest_w, int dest_h,
566                 int dont_wait)
567 {
568 //printf("BC_Bitmap::write_drawable 1 %p %d\n", this, current_ringbuffer); fflush(stdout);
569         //if( dont_wait ) XSync(top_level->display, False);
570         BC_BitmapImage *bfr = cur_bfr();
571         if( !bfr->is_zombie() && is_shared() && shm_reply ) {
572 //printf("activate %p %08lx\n",bfr,bfr->get_shmseg());
573                 top_level->active_bitmaps.insert(bfr, pixmap);
574                 if( ++active_buffers > max_active_buffers )
575                         max_active_buffers = active_buffers;
576         }
577         bfr->write_drawable(pixmap, gc,
578                 source_x, source_y, source_w, source_h,
579                 dest_x, dest_y, dest_w, dest_h);
580         XFlush(top_level->display);
581         avail_lock->lock(" BC_Bitmap::write_drawable");
582         if( bfr->is_zombie() ) {
583                 delete bfr;
584                 --zombies;
585         }
586         else if( is_unshared() || !shm_reply )
587                 avail.append(bfr);
588         active_bfr = 0;
589         avail_lock->unlock();
590         if( !dont_wait && !shm_reply )
591                 XSync(top_level->display, False);
592         return 0;
593 }
594
595
596 int BC_XImage::read_drawable(Drawable &pixmap, int source_x, int source_y)
597 {
598         XGetSubImage(top_level->display, pixmap,
599                 source_x, source_y, bitmap->w, bitmap->h,
600                 0xffffffff, ZPixmap, ximage, 0, 0);
601         return 0;
602 }
603
604 int BC_XShmImage::read_drawable(Drawable &pixmap, int source_x, int source_y)
605 {
606         XShmGetImage(top_level->display, pixmap,
607                 ximage, source_x, source_y, 0xffffffff);
608         return 0;
609 }
610
611 int BC_Bitmap::read_drawable(Drawable &pixmap, int source_x, int source_y, VFrame *frame)
612 {
613         BC_BitmapImage *bfr = cur_bfr();
614         int result = bfr->read_drawable(pixmap, source_x, source_y);
615         if( !result && frame ) result = bfr->read_frame_rgb(frame);
616         return result;
617 }
618
619
620 // ============================ Decoding VFrames
621
622 int BC_Bitmap::read_frame(VFrame *frame, int x1, int y1, int x2, int y2)
623 {
624         return read_frame(frame,
625                 0, 0, frame->get_w(), frame->get_h(),
626                 x1, y1, x2 - x1, y2 - y1);
627 }
628
629
630 int BC_Bitmap::read_frame(VFrame *frame,
631         int in_x, int in_y, int in_w, int in_h,
632         int out_x, int out_y, int out_w, int out_h)
633 {
634         BC_BitmapImage *bfr = cur_bfr();
635         if( hardware_scaling() && frame->get_color_model() == color_model ) {
636 // Hardware accelerated bitmap
637                 switch(color_model) {
638                 case BC_YUV420P:
639                         memcpy(bfr->get_y_data(), frame->get_y(), w * h);
640                         memcpy(bfr->get_u_data(), frame->get_u(), w * h / 4);
641                         memcpy(bfr->get_v_data(), frame->get_v(), w * h / 4);
642                         break;
643                 case BC_YUV422P:
644                         memcpy(bfr->get_y_data(), frame->get_y(), w * h);
645                         memcpy(bfr->get_u_data(), frame->get_u(), w * h / 2);
646                         memcpy(bfr->get_v_data(), frame->get_v(), w * h / 2);
647                         break;
648                 default:
649                 case BC_YUV422:
650                 case BC_UVY422:
651                         memcpy(get_data(), frame->get_data(), w * h + w * h);
652                         break;
653                 }
654         }
655         else {
656 // Software only
657
658 //printf("BC_Bitmap::read_frame %d -> %d %d %d %d %d -> %d %d %d %d\n",
659 //  frame->get_color_model(), color_model,
660 //  in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
661 //if(color_model == 6 && frame->get_color_model() == 19)
662 //printf("BC_Bitmap::read_frame 1 %d %d %d %d\n", frame->get_w(), frame->get_h(), get_w(), get_h());
663                 BC_CModels::transfer(
664                         bfr->get_row_data(), frame->get_rows(),
665                         bfr->get_y_data(), bfr->get_u_data(), bfr->get_v_data(),
666                         frame->get_y(), frame->get_u(), frame->get_v(),
667                         in_x, in_y, in_w, in_h,
668                         out_x, out_y, out_w, out_h,
669                         frame->get_color_model(), color_model,
670                         bg_color, frame->get_w(), w);
671
672                 if( color_model == BC_TRANSPARENCY && !top_level->server_byte_order )
673                         transparency_bitswap(frame->get_data(), w, h);
674 //if(color_model == 6 && frame->get_color_model() == 19)
675 //printf("BC_Bitmap::read_frame 2\n");
676         }
677
678         return 0;
679 }
680
681 uint8_t BC_Bitmap::bitswap[256]={
682   0x00,0x80,0x40,0xc0,0x20,0xa0,0x60,0xe0,0x10,0x90,0x50,0xd0,0x30,0xb0,0x70,0xf0,
683   0x08,0x88,0x48,0xc8,0x28,0xa8,0x68,0xe8,0x18,0x98,0x58,0xd8,0x38,0xb8,0x78,0xf8,
684   0x04,0x84,0x44,0xc4,0x24,0xa4,0x64,0xe4,0x14,0x94,0x54,0xd4,0x34,0xb4,0x74,0xf4,
685   0x0c,0x8c,0x4c,0xcc,0x2c,0xac,0x6c,0xec,0x1c,0x9c,0x5c,0xdc,0x3c,0xbc,0x7c,0xfc,
686   0x02,0x82,0x42,0xc2,0x22,0xa2,0x62,0xe2,0x12,0x92,0x52,0xd2,0x32,0xb2,0x72,0xf2,
687   0x0a,0x8a,0x4a,0xca,0x2a,0xaa,0x6a,0xea,0x1a,0x9a,0x5a,0xda,0x3a,0xba,0x7a,0xfa,
688   0x06,0x86,0x46,0xc6,0x26,0xa6,0x66,0xe6,0x16,0x96,0x56,0xd6,0x36,0xb6,0x76,0xf6,
689   0x0e,0x8e,0x4e,0xce,0x2e,0xae,0x6e,0xee,0x1e,0x9e,0x5e,0xde,0x3e,0xbe,0x7e,0xfe,
690   0x01,0x81,0x41,0xc1,0x21,0xa1,0x61,0xe1,0x11,0x91,0x51,0xd1,0x31,0xb1,0x71,0xf1,
691   0x09,0x89,0x49,0xc9,0x29,0xa9,0x69,0xe9,0x19,0x99,0x59,0xd9,0x39,0xb9,0x79,0xf9,
692   0x05,0x85,0x45,0xc5,0x25,0xa5,0x65,0xe5,0x15,0x95,0x55,0xd5,0x35,0xb5,0x75,0xf5,
693   0x0d,0x8d,0x4d,0xcd,0x2d,0xad,0x6d,0xed,0x1d,0x9d,0x5d,0xdd,0x3d,0xbd,0x7d,0xfd,
694   0x03,0x83,0x43,0xc3,0x23,0xa3,0x63,0xe3,0x13,0x93,0x53,0xd3,0x33,0xb3,0x73,0xf3,
695   0x0b,0x8b,0x4b,0xcb,0x2b,0xab,0x6b,0xeb,0x1b,0x9b,0x5b,0xdb,0x3b,0xbb,0x7b,0xfb,
696   0x07,0x87,0x47,0xc7,0x27,0xa7,0x67,0xe7,0x17,0x97,0x57,0xd7,0x37,0xb7,0x77,0xf7,
697   0x0f,0x8f,0x4f,0xcf,0x2f,0xaf,0x6f,0xef,0x1f,0x9f,0x5f,0xdf,0x3f,0xbf,0x7f,0xff,
698 };
699
700 void BC_Bitmap::transparency_bitswap(uint8_t *buf, int w, int h)
701 {
702         w = ((w-1) | 7) + 1;
703         int len = w * h / 8;
704         int i;
705         for(i=0 ; i+8<=len ; i+=8){
706                 buf[i+0] = bitswap[buf[i+0]];
707                 buf[i+1] = bitswap[buf[i+1]];
708                 buf[i+2] = bitswap[buf[i+2]];
709                 buf[i+3] = bitswap[buf[i+3]];
710                 buf[i+4] = bitswap[buf[i+4]];
711                 buf[i+5] = bitswap[buf[i+5]];
712                 buf[i+6] = bitswap[buf[i+6]];
713                 buf[i+7] = bitswap[buf[i+7]];
714         }
715         for( ; i<len ; i++)
716                 buf[i+0] = bitswap[buf[i+0]];
717 }
718