remove whitespace at eol
[goodguy/history.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         if( lock_avail ) avail_lock->unlock();
435         //top_level->unlock_window();
436 }
437
438 int BC_Bitmap::allocate_data()
439 {
440         int count = 1;
441         max_buffer_count = MAX_BITMAP_BUFFERS;
442         if(use_shm) { // Use shared memory.
443                 int bsz = best_buffer_size();
444                 if( bsz >= 0x800000 ) max_buffer_count = 2;
445                 else if( bsz >= 0x400000 ) max_buffer_count /= 8;
446                 else if( bsz >= 0x100000 ) max_buffer_count /= 4;
447                 else if( bsz >= 0x10000 ) max_buffer_count /= 2;
448                 type = hardware_scaling() ? bmXvShmImage : bmXShmImage;
449                 count = MIN_BITMAP_BUFFERS;
450         }
451         else // use unshared memory.
452                 type = hardware_scaling() ? bmXvImage : bmXImage;
453         update_buffers(count);
454         return 0;
455 }
456
457 int BC_Bitmap::delete_data()
458 {
459 //printf("BC_Bitmap::delete_data 1\n");
460         update_buffers(0);
461         active_bfr = 0;
462         buffer_count = 0;
463         max_buffer_count = 0;
464         return 0;
465 }
466
467 int BC_Bitmap::get_default_depth()
468 {
469         return color_model == BC_TRANSPARENCY ? 1 : top_level->default_depth;
470 }
471
472 long BC_Bitmap::best_buffer_size()
473 {
474         long pixelsize = BC_CModels::calculate_pixelsize(get_color_model());
475         return pixelsize * w * h;
476 }
477
478 int BC_Bitmap::need_shm()
479 {
480 // dont use shm for less than one page
481         return best_buffer_size() < 0x1000 ? 0 :
482                 parent_window->get_resources()->use_shm > 0 ? 1 : 0;
483 }
484
485 int BC_Bitmap::set_bg_color(int color)
486 {
487         this->bg_color = color;
488         return 0;
489 }
490
491 int BC_Bitmap::invert()
492 {
493         for( int j=0; j<buffer_count; ++j ) {
494                 unsigned char **rows = buffers[j]->get_row_data();
495                 if( !rows ) continue;
496                 long bytesPerLine = buffers[j]->bytes_per_line();
497                 for( int k=0; k<h; ++k ) {
498                         unsigned char *bp = rows[k];
499                         for( int i=0; i<bytesPerLine; ++i )
500                                 *bp++ ^= 0xff;
501                 }
502         }
503         return 0;
504 }
505
506 int BC_XShmImage::get_shm_size()
507 {
508         return bytesPerLine * bitmap->h;
509 }
510
511 int BC_Bitmap::write_drawable(Drawable &pixmap, GC &gc,
512         int dest_x, int dest_y, int source_x, int source_y,
513         int dest_w, int dest_h, int dont_wait)
514 {
515         return write_drawable(pixmap, gc,
516                 source_x, source_y, get_w() - source_x, get_h() - source_y,
517                 dest_x, dest_y, dest_w, dest_h, dont_wait);
518 }
519
520 int BC_XImage::write_drawable(Drawable &pixmap, GC &gc,
521         int source_x, int source_y, int source_w, int source_h,
522         int dest_x, int dest_y, int dest_w, int dest_h)
523 {
524         XPutImage(top_level->display, pixmap, gc,
525                 ximage, source_x, source_y,
526                 dest_x, dest_y, dest_w, dest_h);
527         return 0;
528 }
529
530 int BC_XShmImage::write_drawable(Drawable &pixmap, GC &gc,
531         int source_x, int source_y, int source_w, int source_h,
532         int dest_x, int dest_y, int dest_w, int dest_h)
533 {
534         XShmPutImage(top_level->display, pixmap, gc,
535                 ximage, source_x, source_y,
536                 dest_x, dest_y, dest_w, dest_h, bitmap->shm_reply);
537         return 0;
538 }
539
540 int BC_XvImage::write_drawable(Drawable &pixmap, GC &gc,
541         int source_x, int source_y, int source_w, int source_h,
542         int dest_x, int dest_y, int dest_w, int dest_h)
543 {
544         XvPutImage(top_level->display,
545                 bitmap->xv_portid, pixmap, gc, xv_image,
546                 source_x, source_y, source_w, source_h,
547                 dest_x, dest_y, dest_w, dest_h);
548         return 0;
549 }
550
551 int BC_XvShmImage::write_drawable(Drawable &pixmap, GC &gc,
552         int source_x, int source_y, int source_w, int source_h,
553         int dest_x, int dest_y, int dest_w, int dest_h)
554 {
555         XvShmPutImage(top_level->display,
556                 bitmap->xv_portid, pixmap, gc, xv_image,
557                 source_x, source_y, source_w, source_h,
558                 dest_x, dest_y, dest_w, dest_h, bitmap->shm_reply);
559         return 0;
560 }
561
562 int BC_Bitmap::write_drawable(Drawable &pixmap, GC &gc,
563                 int source_x, int source_y, int source_w, int source_h,
564                 int dest_x, int dest_y, int dest_w, int dest_h,
565                 int dont_wait)
566 {
567 //printf("BC_Bitmap::write_drawable 1 %p %d\n", this, current_ringbuffer); fflush(stdout);
568         //if( dont_wait ) XSync(top_level->display, False);
569         BC_BitmapImage *bfr = cur_bfr();
570         if( !bfr->is_zombie() && is_shared() && shm_reply ) {
571 //printf("activate %p %08lx\n",bfr,bfr->get_shmseg());
572                 top_level->active_bitmaps.insert(bfr, pixmap);
573                 if( ++active_buffers > max_active_buffers )
574                         max_active_buffers = active_buffers;
575         }
576         bfr->write_drawable(pixmap, gc,
577                 source_x, source_y, source_w, source_h,
578                 dest_x, dest_y, dest_w, dest_h);
579         XFlush(top_level->display);
580         avail_lock->lock(" BC_Bitmap::write_drawable");
581         if( bfr->is_zombie() ) {
582                 delete bfr;
583                 --zombies;
584         }
585         else if( is_unshared() || !shm_reply )
586                 avail.append(bfr);
587         active_bfr = 0;
588         avail_lock->unlock();
589         if( !dont_wait && !shm_reply )
590                 XSync(top_level->display, False);
591         return 0;
592 }
593
594
595 int BC_XImage::read_drawable(Drawable &pixmap, int source_x, int source_y)
596 {
597         XGetSubImage(top_level->display, pixmap,
598                 source_x, source_y, bitmap->w, bitmap->h,
599                 0xffffffff, ZPixmap, ximage, 0, 0);
600         return 0;
601 }
602
603 int BC_XShmImage::read_drawable(Drawable &pixmap, int source_x, int source_y)
604 {
605         XShmGetImage(top_level->display, pixmap,
606                 ximage, source_x, source_y, 0xffffffff);
607         return 0;
608 }
609
610 int BC_Bitmap::read_drawable(Drawable &pixmap, int source_x, int source_y, VFrame *frame)
611 {
612         BC_BitmapImage *bfr = cur_bfr();
613         int result = bfr->read_drawable(pixmap, source_x, source_y);
614         if( !result && frame ) result = bfr->read_frame_rgb(frame);
615         return result;
616 }
617
618
619 // ============================ Decoding VFrames
620
621 int BC_Bitmap::read_frame(VFrame *frame, int x1, int y1, int x2, int y2)
622 {
623         return read_frame(frame,
624                 0, 0, frame->get_w(), frame->get_h(),
625                 x1, y1, x2 - x1, y2 - y1);
626 }
627
628
629 int BC_Bitmap::read_frame(VFrame *frame,
630         int in_x, int in_y, int in_w, int in_h,
631         int out_x, int out_y, int out_w, int out_h)
632 {
633         BC_BitmapImage *bfr = cur_bfr();
634         if( hardware_scaling() && frame->get_color_model() == color_model ) {
635 // Hardware accelerated bitmap
636                 switch(color_model) {
637                 case BC_YUV420P:
638                         memcpy(bfr->get_y_data(), frame->get_y(), w * h);
639                         memcpy(bfr->get_u_data(), frame->get_u(), w * h / 4);
640                         memcpy(bfr->get_v_data(), frame->get_v(), w * h / 4);
641                         break;
642                 case BC_YUV422P:
643                         memcpy(bfr->get_y_data(), frame->get_y(), w * h);
644                         memcpy(bfr->get_u_data(), frame->get_u(), w * h / 2);
645                         memcpy(bfr->get_v_data(), frame->get_v(), w * h / 2);
646                         break;
647                 default:
648                 case BC_YUV422:
649                 case BC_UVY422:
650                         memcpy(get_data(), frame->get_data(), w * h + w * h);
651                         break;
652                 }
653         }
654         else {
655 // Software only
656
657 //printf("BC_Bitmap::read_frame %d -> %d %d %d %d %d -> %d %d %d %d\n",
658 //  frame->get_color_model(), color_model,
659 //  in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
660 //if(color_model == 6 && frame->get_color_model() == 19)
661 //printf("BC_Bitmap::read_frame 1 %d %d %d %d\n", frame->get_w(), frame->get_h(), get_w(), get_h());
662                 BC_CModels::transfer(
663                         bfr->get_row_data(), frame->get_rows(),
664                         bfr->get_y_data(), bfr->get_u_data(), bfr->get_v_data(),
665                         frame->get_y(), frame->get_u(), frame->get_v(),
666                         in_x, in_y, in_w, in_h,
667                         out_x, out_y, out_w, out_h,
668                         frame->get_color_model(), color_model,
669                         bg_color, frame->get_w(), w);
670
671                 if( color_model == BC_TRANSPARENCY && !top_level->server_byte_order )
672                         transparency_bitswap(frame->get_data(), w, h);
673 //if(color_model == 6 && frame->get_color_model() == 19)
674 //printf("BC_Bitmap::read_frame 2\n");
675         }
676
677         return 0;
678 }
679
680 uint8_t BC_Bitmap::bitswap[256]={
681   0x00,0x80,0x40,0xc0,0x20,0xa0,0x60,0xe0,0x10,0x90,0x50,0xd0,0x30,0xb0,0x70,0xf0,
682   0x08,0x88,0x48,0xc8,0x28,0xa8,0x68,0xe8,0x18,0x98,0x58,0xd8,0x38,0xb8,0x78,0xf8,
683   0x04,0x84,0x44,0xc4,0x24,0xa4,0x64,0xe4,0x14,0x94,0x54,0xd4,0x34,0xb4,0x74,0xf4,
684   0x0c,0x8c,0x4c,0xcc,0x2c,0xac,0x6c,0xec,0x1c,0x9c,0x5c,0xdc,0x3c,0xbc,0x7c,0xfc,
685   0x02,0x82,0x42,0xc2,0x22,0xa2,0x62,0xe2,0x12,0x92,0x52,0xd2,0x32,0xb2,0x72,0xf2,
686   0x0a,0x8a,0x4a,0xca,0x2a,0xaa,0x6a,0xea,0x1a,0x9a,0x5a,0xda,0x3a,0xba,0x7a,0xfa,
687   0x06,0x86,0x46,0xc6,0x26,0xa6,0x66,0xe6,0x16,0x96,0x56,0xd6,0x36,0xb6,0x76,0xf6,
688   0x0e,0x8e,0x4e,0xce,0x2e,0xae,0x6e,0xee,0x1e,0x9e,0x5e,0xde,0x3e,0xbe,0x7e,0xfe,
689   0x01,0x81,0x41,0xc1,0x21,0xa1,0x61,0xe1,0x11,0x91,0x51,0xd1,0x31,0xb1,0x71,0xf1,
690   0x09,0x89,0x49,0xc9,0x29,0xa9,0x69,0xe9,0x19,0x99,0x59,0xd9,0x39,0xb9,0x79,0xf9,
691   0x05,0x85,0x45,0xc5,0x25,0xa5,0x65,0xe5,0x15,0x95,0x55,0xd5,0x35,0xb5,0x75,0xf5,
692   0x0d,0x8d,0x4d,0xcd,0x2d,0xad,0x6d,0xed,0x1d,0x9d,0x5d,0xdd,0x3d,0xbd,0x7d,0xfd,
693   0x03,0x83,0x43,0xc3,0x23,0xa3,0x63,0xe3,0x13,0x93,0x53,0xd3,0x33,0xb3,0x73,0xf3,
694   0x0b,0x8b,0x4b,0xcb,0x2b,0xab,0x6b,0xeb,0x1b,0x9b,0x5b,0xdb,0x3b,0xbb,0x7b,0xfb,
695   0x07,0x87,0x47,0xc7,0x27,0xa7,0x67,0xe7,0x17,0x97,0x57,0xd7,0x37,0xb7,0x77,0xf7,
696   0x0f,0x8f,0x4f,0xcf,0x2f,0xaf,0x6f,0xef,0x1f,0x9f,0x5f,0xdf,0x3f,0xbf,0x7f,0xff,
697 };
698
699 void BC_Bitmap::transparency_bitswap(uint8_t *buf, int w, int h)
700 {
701         w = ((w-1) | 7) + 1;
702         int len = w * h / 8;
703         int i;
704         for(i=0 ; i+8<=len ; i+=8){
705                 buf[i+0] = bitswap[buf[i+0]];
706                 buf[i+1] = bitswap[buf[i+1]];
707                 buf[i+2] = bitswap[buf[i+2]];
708                 buf[i+3] = bitswap[buf[i+3]];
709                 buf[i+4] = bitswap[buf[i+4]];
710                 buf[i+5] = bitswap[buf[i+5]];
711                 buf[i+6] = bitswap[buf[i+6]];
712                 buf[i+7] = bitswap[buf[i+7]];
713         }
714         for( ; i<len ; i++)
715                 buf[i+0] = bitswap[buf[i+0]];
716 }
717