add vicons
[goodguy/history.git] / cinelerra-5.0 / 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 owner == &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)
274 {
275 // Decompress data into a temporary vframe
276         VFrame frame;
277
278         frame.read_png(png_data);
279
280         avail_lock = 0;
281 // Initialize the bitmap
282         initialize(parent_window, 
283                 frame.get_w(), 
284                 frame.get_h(), 
285                 parent_window->get_color_model(), 
286                 0);
287
288 // Copy the vframe to the bitmap
289         read_frame(&frame, 0, 0, w, h);
290 }
291
292 BC_Bitmap::BC_Bitmap(BC_WindowBase *parent_window, VFrame *frame)
293 {
294         avail_lock = 0;
295 // Initialize the bitmap
296         initialize(parent_window, 
297                 frame->get_w(), 
298                 frame->get_h(), 
299                 parent_window->get_color_model(), 
300                 0);
301
302 // Copy the vframe to the bitmap
303         read_frame(frame, 0, 0, w, h);
304 }
305
306 BC_Bitmap::BC_Bitmap(BC_WindowBase *parent_window, 
307         int w, int h, int color_model, int use_shm)
308 {
309         avail_lock = 0;
310         initialize(parent_window, w, h, color_model, use_shm);
311 }
312
313 BC_Bitmap::~BC_Bitmap()
314 {
315         delete_data();
316         delete avail_lock;
317 }
318
319 int BC_Bitmap::initialize(BC_WindowBase *parent_window, 
320         int w, int h, int color_model, int use_shm)
321 {
322         BC_Resources *resources = parent_window->get_resources();
323         this->parent_window = parent_window;
324         this->top_level = parent_window->top_level;
325         this->xv_portid = resources->use_xvideo ? top_level->xvideo_port_id : -1;
326         if( w < 1 ) w = 1;
327         if( h < 1 ) h = 1;
328         this->w = w;
329         this->h = h;
330         this->color_model = color_model;
331         this->use_shm = !use_shm ? 0 : need_shm();
332         this->shm_reply = this->use_shm && resources->shm_reply ? 1 : 0;
333         // dont use shm for less than one page
334         this->bg_color = parent_window->bg_color;
335         if( !this->avail_lock )
336                 this->avail_lock = new Mutex("BC_Bitmap::avail_lock");
337         else
338                 this->avail_lock->reset();
339         this->buffers = 0;
340         this->last_pixmap_used = 0;
341         this->last_pixmap = 0;
342
343         this->active_bfr = 0;
344         this->buffer_count = 0;
345         this->max_buffer_count = 0;
346         this->active_buffers = 0;
347         allocate_data();
348         return 0;
349 }
350
351 int BC_Bitmap::match_params(int w, int h, int color_model, int use_shm)
352 {
353         if( use_shm ) use_shm = need_shm();
354         if(this->w /* != */ < w || this->h /* != */ < h ||
355            this->color_model != color_model || this->use_shm != use_shm) {
356                 delete_data();
357                 initialize(parent_window, w, h, color_model, use_shm);
358         }
359
360         return 0;
361 }
362
363 int BC_Bitmap::params_match(int w, int h, int color_model, int use_shm)
364 {
365         int result = 0;
366         if( use_shm ) use_shm = need_shm();
367         if(this->w == w && this->h == h && this->color_model == color_model) {
368                 if(use_shm == this->use_shm || use_shm == BC_INFINITY)
369                         result = 1;
370         }
371         return result;
372 }
373
374 BC_BitmapImage *BC_Bitmap::new_buffer(int type, int idx)
375 {
376         BC_BitmapImage *buffer = 0;
377         if( idx < 0 ) {
378                 if( type == bmXShmImage ) type = bmXImage;
379                 else if( type ==  bmXvShmImage ) type = bmXvImage;
380         }
381         switch( type ) {
382         default:
383         case bmXImage:
384                 buffer = new BC_XImage(this, idx, w, h, color_model);
385                 break;
386         case bmXvImage:
387                 buffer = new BC_XvImage(this, idx, w, h, color_model);
388                 break;
389         case bmXShmImage:
390                 buffer = new BC_XShmImage(this, idx, w, h, color_model);
391                 break;
392         case bmXvShmImage:
393                 buffer = new BC_XvShmImage(this, idx, w, h, color_model);
394                 break;
395         }
396         if( buffer->is_zombie() ) ++zombies;
397         return buffer;
398 }
399
400 void BC_Bitmap::
401 update_buffers(int count, int lock_avail)
402 {
403         int i;
404         // can deadlock in XReply (libxcb bug) without this lock
405         //top_level->lock_window("BC_Bitmap::update_buffers");
406         if( lock_avail ) avail_lock->lock("BC_Bitmap::update_buffers");
407         if( count > max_buffer_count ) count = max_buffer_count;
408         BC_BitmapImage **new_buffers = !count ? 0 : new BC_BitmapImage *[count];
409         if( buffer_count < count ) {
410                 for( i=0; i<buffer_count; ++i )
411                         new_buffers[i] = buffers[i];
412                 while( i < count ) {
413                         BC_BitmapImage *buffer = new_buffer(type, i);
414                         new_buffers[i] = buffer;
415                         avail.append(buffer);
416                         ++i;
417                 }
418         }
419         else {
420                 for( i=0; i<count; ++i )
421                         new_buffers[i] = buffers[i];
422                 while( i < buffer_count ) {
423                         BC_BitmapImage *buffer = buffers[i];
424                         if( buffer == active_bfr ) active_bfr = 0;
425                         if( buffer->is_avail() ) {
426                                 delete buffer;
427                         }
428                         else {
429                                 ++zombies;
430                                 buffer->index = -1;
431                         }
432                         ++i;
433                 }
434         }
435         delete [] buffers;
436         buffers = new_buffers;
437         buffer_count = count;
438         if( lock_avail ) avail_lock->unlock();
439         //top_level->unlock_window();
440 }
441
442 int BC_Bitmap::allocate_data()
443 {
444         int count = 1;
445         max_buffer_count = MAX_BITMAP_BUFFERS;
446         if(use_shm) { // Use shared memory.
447                 int bsz = best_buffer_size();
448                 if( bsz >= 0x800000 ) max_buffer_count = 2;
449                 else if( bsz >= 0x400000 ) max_buffer_count /= 8;
450                 else if( bsz >= 0x100000 ) max_buffer_count /= 4;
451                 else if( bsz >= 0x10000 ) max_buffer_count /= 2;
452                 type = hardware_scaling() ? bmXvShmImage : bmXShmImage;
453                 count = MIN_BITMAP_BUFFERS;
454         }
455         else // use unshared memory.
456                 type = hardware_scaling() ? bmXvImage : bmXImage;
457         update_buffers(count);
458         return 0;
459 }
460
461 int BC_Bitmap::delete_data()
462 {
463 //printf("BC_Bitmap::delete_data 1\n");
464         if( last_pixmap_used && xv_portid >= 0 )
465                 XvStopVideo(top_level->display, xv_portid, last_pixmap);
466         update_buffers(0);
467         if( xv_portid >= 0 )
468                 XvUngrabPort(top_level->display, xv_portid, CurrentTime);
469         last_pixmap_used = 0;
470         active_bfr = 0;
471         buffer_count = 0;
472         max_buffer_count = 0;
473         return 0;
474 }
475
476 int BC_Bitmap::get_default_depth()
477 {
478         return color_model == BC_TRANSPARENCY ? 1 : top_level->default_depth;
479 }
480
481 long BC_Bitmap::best_buffer_size()
482 {
483         long pixelsize = BC_CModels::calculate_pixelsize(get_color_model());
484         return pixelsize * w * h;
485 }
486
487 int BC_Bitmap::need_shm()
488 {
489 // dont use shm for less than one page
490         return best_buffer_size() < 0x1000 ? 0 :
491                 parent_window->get_resources()->use_shm > 0 ? 1 : 0;
492 }
493
494 int BC_Bitmap::set_bg_color(int color)
495 {
496         this->bg_color = color;
497         return 0;
498 }
499
500 int BC_Bitmap::invert()
501 {
502         for( int j=0; j<buffer_count; ++j ) {
503                 unsigned char **rows = buffers[j]->get_row_data();
504                 if( !rows ) continue;
505                 long bytesPerLine = buffers[j]->bytes_per_line();
506                 for( int k=0; k<h; ++k ) {
507                         unsigned char *bp = rows[k];
508                         for( int i=0; i<bytesPerLine; ++i )
509                                 *bp++ ^= 0xff;
510                 }
511         }
512         return 0;
513 }
514
515 int BC_XShmImage::get_shm_size()
516 {
517         return bytesPerLine * bitmap->h;
518 }
519
520 int BC_Bitmap::write_drawable(Drawable &pixmap, GC &gc,
521         int dest_x, int dest_y, int source_x, int source_y, 
522         int dest_w, int dest_h, int dont_wait)
523 {
524         return write_drawable(pixmap, gc,
525                 source_x, source_y, get_w() - source_x, get_h() - source_y,
526                 dest_x, dest_y, dest_w, dest_h, dont_wait);
527 }
528
529 int BC_XImage::write_drawable(Drawable &pixmap, GC &gc,
530         int source_x, int source_y, int source_w, int source_h,
531         int dest_x, int dest_y, int dest_w, int dest_h)
532 {
533         XPutImage(top_level->display, pixmap, gc,
534                 ximage, source_x, source_y,
535                 dest_x, dest_y, dest_w, dest_h);
536         return 0;
537 }
538
539 int BC_XShmImage::write_drawable(Drawable &pixmap, GC &gc,
540         int source_x, int source_y, int source_w, int source_h,
541         int dest_x, int dest_y, int dest_w, int dest_h)
542 {
543         XShmPutImage(top_level->display, pixmap, gc,
544                 ximage, source_x, source_y,
545                 dest_x, dest_y, dest_w, dest_h, bitmap->shm_reply);
546         return 0;
547 }
548
549 int BC_XvImage::write_drawable(Drawable &pixmap, GC &gc,
550         int source_x, int source_y, int source_w, int source_h,
551         int dest_x, int dest_y, int dest_w, int dest_h)
552 {
553         XvPutImage(top_level->display,
554                 bitmap->xv_portid, pixmap, gc, xv_image,
555                 source_x, source_y, source_w, source_h,
556                 dest_x, dest_y, dest_w, dest_h);
557         return 0;
558 }
559
560 int BC_XvShmImage::write_drawable(Drawable &pixmap, GC &gc,
561         int source_x, int source_y, int source_w, int source_h,
562         int dest_x, int dest_y, int dest_w, int dest_h)
563 {
564         XvShmPutImage(top_level->display,
565                 bitmap->xv_portid, pixmap, gc, xv_image,
566                 source_x, source_y, source_w, source_h,
567                 dest_x, dest_y, dest_w, dest_h, bitmap->shm_reply);
568         return 0;
569 }
570
571 int BC_Bitmap::write_drawable(Drawable &pixmap, GC &gc,
572                 int source_x, int source_y, int source_w, int source_h,
573                 int dest_x, int dest_y, int dest_w, int dest_h, 
574                 int dont_wait)
575 {
576 //printf("BC_Bitmap::write_drawable 1 %p %d\n", this, current_ringbuffer); fflush(stdout);
577         //if( dont_wait ) XSync(top_level->display, False);
578         BC_BitmapImage *bfr = cur_bfr();
579         if( !bfr->is_zombie() && is_shared() && shm_reply ) {
580 //printf("activate %p %08lx\n",bfr,bfr->get_shmseg());
581                 top_level->active_bitmaps.insert(bfr, pixmap);
582                 if( ++active_buffers > max_active_buffers )
583                         max_active_buffers = active_buffers;
584         }
585         bfr->write_drawable(pixmap, gc,
586                 source_x, source_y, source_w, source_h,
587                 dest_x, dest_y, dest_w, dest_h);
588         XFlush(top_level->display);
589         avail_lock->lock(" BC_Bitmap::write_drawable");
590         if( bfr->is_zombie() ) {
591                 delete bfr;
592                 --zombies;
593         }
594         else if( is_unshared() || !shm_reply )
595                 avail.append(bfr);
596         active_bfr = 0;
597         avail_lock->unlock();
598         last_pixmap = pixmap;
599         last_pixmap_used = 1;
600         if( !dont_wait && !shm_reply )
601                 XSync(top_level->display, False);
602         return 0;
603 }
604
605
606 int BC_XImage::read_drawable(Drawable &pixmap, int source_x, int source_y)
607 {
608         XGetSubImage(top_level->display, pixmap,
609                 source_x, source_y, bitmap->w, bitmap->h,
610                 0xffffffff, ZPixmap, ximage, 0, 0);
611         return 0;
612 }
613
614 int BC_XShmImage::read_drawable(Drawable &pixmap, int source_x, int source_y)
615 {
616         XShmGetImage(top_level->display, pixmap,
617                 ximage, source_x, source_y, 0xffffffff);
618         return 0;
619 }
620
621 int BC_Bitmap::read_drawable(Drawable &pixmap, int source_x, int source_y, VFrame *frame)
622 {
623         BC_BitmapImage *bfr = cur_bfr();
624         int result = bfr->read_drawable(pixmap, source_x, source_y);
625         if( !result && frame ) result = bfr->read_frame_rgb(frame);
626         return result;
627 }
628
629
630 // ============================ Decoding VFrames
631
632 int BC_Bitmap::read_frame(VFrame *frame, int x1, int y1, int x2, int y2)
633 {
634         return read_frame(frame, 
635                 0, 0, frame->get_w(), frame->get_h(),
636                 x1, y1, x2 - x1, y2 - y1);
637 }
638
639
640 int BC_Bitmap::read_frame(VFrame *frame, 
641         int in_x, int in_y, int in_w, int in_h,
642         int out_x, int out_y, int out_w, int out_h)
643 {
644         BC_BitmapImage *bfr = cur_bfr();
645         if( hardware_scaling() && frame->get_color_model() == color_model ) {
646 // Hardware accelerated bitmap
647                 switch(color_model) {
648                 case BC_YUV420P:
649                         memcpy(bfr->get_y_data(), frame->get_y(), w * h);
650                         memcpy(bfr->get_u_data(), frame->get_u(), w * h / 4);
651                         memcpy(bfr->get_v_data(), frame->get_v(), w * h / 4);
652                         break;
653                 case BC_YUV422P:
654                         memcpy(bfr->get_y_data(), frame->get_y(), w * h);
655                         memcpy(bfr->get_u_data(), frame->get_u(), w * h / 2);
656                         memcpy(bfr->get_v_data(), frame->get_v(), w * h / 2);
657                         break;
658                 default:
659                 case BC_YUV422:
660                 case BC_UVY422:
661                         memcpy(get_data(), frame->get_data(), w * h + w * h);
662                         break;
663                 }
664         }
665         else {
666 // Software only
667
668 //printf("BC_Bitmap::read_frame %d -> %d %d %d %d %d -> %d %d %d %d\n",
669 //  frame->get_color_model(), color_model,
670 //  in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
671 //if(color_model == 6 && frame->get_color_model() == 19)
672 //printf("BC_Bitmap::read_frame 1 %d %d %d %d\n", frame->get_w(), frame->get_h(), get_w(), get_h());
673                 BC_CModels::transfer(
674                         bfr->get_row_data(), frame->get_rows(),
675                         bfr->get_y_data(), bfr->get_u_data(), bfr->get_v_data(),
676                         frame->get_y(), frame->get_u(), frame->get_v(),
677                         in_x, in_y, in_w, in_h,
678                         out_x, out_y, out_w, out_h,
679                         frame->get_color_model(), color_model,
680                         bg_color, frame->get_w(), w);
681
682                 if( color_model == BC_TRANSPARENCY && !top_level->server_byte_order )
683                         transparency_bitswap(frame->get_data(), w, h);
684 //if(color_model == 6 && frame->get_color_model() == 19)
685 //printf("BC_Bitmap::read_frame 2\n");
686         }
687
688         return 0;
689 }
690
691 uint8_t BC_Bitmap::bitswap[256]={
692   0x00,0x80,0x40,0xc0,0x20,0xa0,0x60,0xe0,0x10,0x90,0x50,0xd0,0x30,0xb0,0x70,0xf0,
693   0x08,0x88,0x48,0xc8,0x28,0xa8,0x68,0xe8,0x18,0x98,0x58,0xd8,0x38,0xb8,0x78,0xf8,
694   0x04,0x84,0x44,0xc4,0x24,0xa4,0x64,0xe4,0x14,0x94,0x54,0xd4,0x34,0xb4,0x74,0xf4,
695   0x0c,0x8c,0x4c,0xcc,0x2c,0xac,0x6c,0xec,0x1c,0x9c,0x5c,0xdc,0x3c,0xbc,0x7c,0xfc,
696   0x02,0x82,0x42,0xc2,0x22,0xa2,0x62,0xe2,0x12,0x92,0x52,0xd2,0x32,0xb2,0x72,0xf2,
697   0x0a,0x8a,0x4a,0xca,0x2a,0xaa,0x6a,0xea,0x1a,0x9a,0x5a,0xda,0x3a,0xba,0x7a,0xfa,
698   0x06,0x86,0x46,0xc6,0x26,0xa6,0x66,0xe6,0x16,0x96,0x56,0xd6,0x36,0xb6,0x76,0xf6,
699   0x0e,0x8e,0x4e,0xce,0x2e,0xae,0x6e,0xee,0x1e,0x9e,0x5e,0xde,0x3e,0xbe,0x7e,0xfe,
700   0x01,0x81,0x41,0xc1,0x21,0xa1,0x61,0xe1,0x11,0x91,0x51,0xd1,0x31,0xb1,0x71,0xf1,
701   0x09,0x89,0x49,0xc9,0x29,0xa9,0x69,0xe9,0x19,0x99,0x59,0xd9,0x39,0xb9,0x79,0xf9,
702   0x05,0x85,0x45,0xc5,0x25,0xa5,0x65,0xe5,0x15,0x95,0x55,0xd5,0x35,0xb5,0x75,0xf5,
703   0x0d,0x8d,0x4d,0xcd,0x2d,0xad,0x6d,0xed,0x1d,0x9d,0x5d,0xdd,0x3d,0xbd,0x7d,0xfd,
704   0x03,0x83,0x43,0xc3,0x23,0xa3,0x63,0xe3,0x13,0x93,0x53,0xd3,0x33,0xb3,0x73,0xf3,
705   0x0b,0x8b,0x4b,0xcb,0x2b,0xab,0x6b,0xeb,0x1b,0x9b,0x5b,0xdb,0x3b,0xbb,0x7b,0xfb,
706   0x07,0x87,0x47,0xc7,0x27,0xa7,0x67,0xe7,0x17,0x97,0x57,0xd7,0x37,0xb7,0x77,0xf7,
707   0x0f,0x8f,0x4f,0xcf,0x2f,0xaf,0x6f,0xef,0x1f,0x9f,0x5f,0xdf,0x3f,0xbf,0x7f,0xff,
708 };
709
710 void BC_Bitmap::transparency_bitswap(uint8_t *buf, int w, int h)
711 {
712         w = ((w-1) | 7) + 1;
713         int len = w * h / 8;
714         int i;
715         for(i=0 ; i+8<=len ; i+=8){
716                 buf[i+0] = bitswap[buf[i+0]];
717                 buf[i+1] = bitswap[buf[i+1]];
718                 buf[i+2] = bitswap[buf[i+2]];
719                 buf[i+3] = bitswap[buf[i+3]];
720                 buf[i+4] = bitswap[buf[i+4]];
721                 buf[i+5] = bitswap[buf[i+5]];
722                 buf[i+6] = bitswap[buf[i+6]];
723                 buf[i+7] = bitswap[buf[i+7]];
724         }
725         for( ; i<len ; i++)
726                 buf[i+0] = bitswap[buf[i+0]];
727 }
728