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