2 Copyright 2004-2005 Chris Tallon
3 Portions copyright 2004 Jon Gettler
5 This file is part of VOMP.
7 VOMP is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 VOMP is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with VOMP; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 Surface* Surface::screen = NULL;
26 osd_font_t* Surface::font = &font_helvB18;
28 Surface::Surface(int id)
30 if (id == SCREEN) screen = this;
32 fdOsd = Osd::getInstance()->getFD();
33 memset(&surface, 0, sizeof(osd_surface_t));
34 memset(&surface.sfc, 0, sizeof(stbgfx_sfc_t));
35 memset(&surface.map, 0, sizeof(stbgfx_map_t));
40 for (int i = 0; i < 3; i++)
42 if (surface.base[i]) munmap(surface.base[i], surface.map.map[i].size);
47 a = ioctl(fdOsd, GFX_FB_SFC_UNMAP, surface.sfc.handle);
48 if (a) Log::getInstance()->log("Surface", Log::ERR, "Surface unmap failed");
50 a = ioctl(fdOsd, GFX_FB_SFC_FREE, surface.sfc.handle);
51 if (a) Log::getInstance()->log("Surface", Log::ERR, "Surface destroy failed");
55 Surface* Surface::getScreen()
60 int Surface::create(UINT width, UINT height)
63 int displayNumber = 0; // mvpmc iterates 0->15 till it gets one that works?
65 surface.sfc.width = width;
66 surface.sfc.height = height;
67 surface.sfc.flags = 0x3f1533;
69 surface.sfc.unknown = -1;
71 // r = ioctl(fdOsd, GFX_FB_SET_OSD, &displayNumber);
74 r = ioctl(fdOsd, GFX_FB_OSD_SURFACE, &displayNumber);
77 r = ioctl(fdOsd, GFX_FB_SFC_ALLOC, &surface.sfc);
80 surface.map.map[0].unknown = surface.sfc.handle;
81 r = ioctl(fdOsd, GFX_FB_MAP, &surface.map);
84 surface.base[0] = (unsigned char *)mmap(NULL, surface.map.map[0].size, PROT_READ|PROT_WRITE, MAP_SHARED, fdOsd, surface.map.map[0].addr);
85 if (surface.base[0] == MAP_FAILED) return 0;
87 surface.base[1] = (unsigned char *)mmap(NULL, surface.map.map[1].size, PROT_READ|PROT_WRITE, MAP_SHARED, fdOsd, surface.map.map[1].addr);
88 if (surface.base[1] == MAP_FAILED) return 0;
90 surface.base[2] = (unsigned char *)mmap(NULL, surface.map.map[2].size, PROT_READ|PROT_WRITE, MAP_SHARED, fdOsd, surface.map.map[2].addr);
91 if (surface.base[2] == MAP_FAILED) return 0;
93 // surface.display.num = displayNumber;
94 // r = ioctl(fdOsd, GFX_FB_MOVE_DISPLAY, &surface.display);
97 // r = ioctl(fdOsd, GFX_FB_SET_DISPLAY, &surface.display);
103 void Surface::display()
105 unsigned long fb_descriptor[2];
107 fb_descriptor[0] = surface.sfc.handle;
108 fb_descriptor[1] = 1;
110 ioctl(fdOsd, GFX_FB_ATTACH, fb_descriptor);
113 unsigned long Surface::getSurfaceHandle()
115 return surface.sfc.handle;
118 // ----------------------------------------------------------------------------
120 // Now for the drawing functions
123 * RGB to YUV conversion tables
125 int Surface::conv_YB[256];
126 int Surface::conv_YG[256];
127 int Surface::conv_YR[256];
128 int Surface::conv_UB[256];
129 int Surface::conv_UG[256];
130 int Surface::conv_UR[256];
131 int Surface::conv_VB[256];
132 int Surface::conv_VG[256];
133 int Surface::conv_VR[256];
135 int Surface::conv_BY[256];
136 int Surface::conv_GY[256];
137 int Surface::conv_RY[256];
138 int Surface::conv_BU[256];
139 int Surface::conv_GU[256];
140 int Surface::conv_RU[256];
141 int Surface::conv_BV[256];
142 int Surface::conv_GV[256];
143 int Surface::conv_RV[256];
146 * gfx_init() - initialize the RGB to YUV conversion tables
148 void Surface::initConversionTables(void)
152 for (i=0; i<256; i++) {
153 conv_YB[i] = (int)(0.299 * (double)i);
156 for (i=0; i<256; i++) {
157 conv_YG[i] = (int)(0.587 * (double)i);
160 for (i=0; i<256; i++) {
161 conv_YR[i] = (int)(0.114 * (double)i);
165 for (i=0; i<256; i++) {
166 conv_UB[i] = (int)(0.5 * (double)i);
167 conv_BU[i] = (int)(1.732 * (i - 128));
169 for (i=0; i<256; i++) {
170 conv_UG[i] = (int)(-0.33126 * (double)i);
171 conv_GU[i] = (int)(-0.338 * (i - 128));
173 for (i=0; i<256; i++) {
174 conv_UR[i] = (int)(-0.16874 * (double)i);
178 for (i=0; i<256; i++) {
179 conv_VB[i] = (int)(-0.08131 * (double)i);
182 for (i=0; i<256; i++) {
183 conv_VG[i] = (int)(-0.41869 * (double)i);
184 conv_GV[i] = (int)(-0.698 * (i - 128));
186 for (i=0; i<256; i++) {
187 conv_VR[i] = (int)(0.5 * (double)i);
188 conv_RV[i] = (int)(1.370 * ((double)i - 128));
193 * rgb2yuv() - convert an RGB pixel to YUV
196 void Surface::rgb2yuv(unsigned char r, unsigned char g, unsigned char b, unsigned char *y, unsigned char *u, unsigned char *v)
200 Y = conv_YB[b] + conv_YG[g] + conv_YR[r];
201 U = conv_UB[b] + conv_UG[g] + conv_UR[r] + 128;
202 V = conv_VB[b] + conv_VG[g] + conv_VR[r] + 128;
222 int Surface::fillblt(int x, int y, int width, int height, unsigned int c)
226 fblt.handle = surface.sfc.handle;
230 fblt.height = height;
233 return ioctl(fdOsd, GFX_FB_OSD_FILLBLT, &fblt);
236 void Surface::drawPixel(int x, int y, unsigned int c)
239 unsigned char r, g, b, a, Y, U, V;
240 unsigned int line, remainder;
242 if ((x >= (int)surface.sfc.width) || (y >= (int)surface.sfc.height))
245 c2rgba(c, &r, &g, &b, &a);
247 rgb2yuv(r, g, b, &Y, &U, &V);
249 remainder = (surface.sfc.width % 4);
251 line = surface.sfc.width;
253 line = surface.sfc.width + (4 - remainder);
255 offset = (y * line) + x;
257 *(surface.base[0] + offset) = Y;
258 *(surface.base[1] + (offset & 0xfffffffe)) = U;
259 *(surface.base[1] + (offset & 0xfffffffe) + 1) = V;
260 *(surface.base[2] + offset) = a;
264 void Surface::drawHorzLine(int x1, int x2, int y, unsigned int c)
266 fillblt(x1, y, x2-x1, 1, c);
269 void Surface::drawVertLine(int x, int y1, int y2, unsigned int c)
271 fillblt(x, y1, 1, y2-y1, c);
275 /* surface update to screen needs:
276 source x distance into this surface
277 source y distance into this surface
280 destination x on screen
281 destination y on screen
283 int Surface::updateToScreen(int sx, int sy, int w, int h, int dx, int dy) // FIXME new, replace others with this FIXME
285 return blt(fdOsd, surface.sfc.handle, sx, sy, w, h, screen->getSurfaceHandle(), dx, dy);
288 int Surface::blt(int fd, unsigned long shandle, int sx, int sy, int width, int height, unsigned long dhandle, int dx, int dy)
291 memset(&fblt, 0, sizeof(fblt));
293 fblt.dst_handle = dhandle;
297 fblt.src_handle = shandle;
302 fblt.height = height;
308 return ioctl(fd, GFX_FB_OSD_BITBLT, &fblt);
311 int Surface::drawText(char* text, int x, int y, int r, int g, int b)
317 fg = rgba(r, g, b, 255);
326 unsigned char c = text[i];
327 unsigned long *character = &font->content[font->offset[c]];
328 int w = font->width[c];
335 if ((character[Y] >> (32 - X)) & 0x1)
337 drawPixel(x+X+cx, y+Y, fg);
347 int Surface::drawTextRJ(char* text, int x, int y, int r, int g, int b)
354 for (i = 0; i < n; i++)
356 w += font->width[text[i]];
362 else return drawText(text, x, y, r, g, b);
365 int Surface::drawTextCentre(char* text, int x, int y, int r, int g, int b)
372 for (i = 0; i < n; i++)
374 w += font->width[text[i]];
380 else return drawText(text, x, y, r, g, b);
383 int Surface::getCharWidth(char c)
385 return font->width[c];
388 int Surface::getFontHeight()
390 return font->spacing;
393 void Surface::screenShot(char* fileName)
395 Log* logger = Log::getInstance();
397 FILE* outfile = fopen(fileName, "w");
400 logger->log("Surface", Log::ERR, "Can't open JPEG");
403 logger->log("Surface", Log::DEBUG, "File opened %u %u", surface.sfc.height, surface.sfc.width);
405 struct jpeg_compress_struct cinfo;
406 struct jpeg_error_mgr jerr;
407 cinfo.err = jpeg_std_error(&jerr);
408 jpeg_create_compress(&cinfo);
409 jpeg_stdio_dest(&cinfo, outfile);
410 cinfo.image_width = surface.sfc.width;
411 cinfo.image_height = surface.sfc.height;
412 cinfo.input_components = 3;
413 cinfo.in_color_space = JCS_RGB;
414 jpeg_set_defaults(&cinfo);
415 jpeg_start_compress(&cinfo, TRUE);
418 unsigned char row[surface.sfc.width * 3];
419 unsigned char* prow = (unsigned char*)&row;
420 unsigned char r, g, b;
422 while (cinfo.next_scanline < cinfo.image_height)
424 for(unsigned int i = 0; i < surface.sfc.width; i++)
426 readPixel(i, cinfo.next_scanline, &r, &g, &b);
428 row[(i * 3) + 1] = g;
429 row[(i * 3) + 2] = b;
431 jpeg_write_scanlines(&cinfo, (JSAMPLE **)&prow, 1);
434 jpeg_finish_compress(&cinfo);
435 jpeg_destroy_compress(&cinfo);
437 logger->log("Surface", Log::DEBUG, "Jpeg saved");
440 void Surface::readPixel(int x, int y, unsigned char* r, unsigned char* g, unsigned char* b)
443 unsigned char a, Y, U, V;
444 unsigned int line, remainder;
446 if (((unsigned int)x >= surface.sfc.width) || ((unsigned int)y >= surface.sfc.height)) return;
448 remainder = (surface.sfc.width % 4);
450 line = surface.sfc.width;
452 line = surface.sfc.width + (4 - remainder);
454 offset = (y * line) + x;
456 Y = *(surface.base[0] + offset);
457 U = *(surface.base[1] + (offset & 0xfffffffe));
458 V = *(surface.base[1] + (offset & 0xfffffffe) + 1);
459 a = *(surface.base[2] + offset);
461 yuv2rgb(Y, U, V, r, g, b);
464 void Surface::yuv2rgb(int y, int u, int v, unsigned char* pr, unsigned char* pg, unsigned char* pb)
466 // from http://www.fourcc.org/index.php?http%3A//www.fourcc.org/fccyvrgb.php
468 // unsigned int pixel32;
469 // unsigned char *pixel = (unsigned char *)&pixel32;
474 One formula I found: (not the right one)
476 R = 1.164(Y - 16) + 1.596(Cr - 128)
477 G = 1.164(Y - 16) - 0.813(Cr - 128) - 0.391(Cb - 128)
478 B = 1.164(Y - 16) + 2.018(Cb - 128)
481 r = (1.164 * (y - 16))
482 + (2.018 * (v - 128));
483 g = (1.164 * (y - 16))
484 - (0.813 * (u - 128))
485 - (0.391 * (v - 128));
486 b = (1.164 * (y - 16))
487 + (1.596 * (u - 128));
490 Another formula I found: (seems to work)
492 R = Y + 1.370705 (V-128)
493 G = Y - 0.698001 (V-128) - 0.337633 (U-128)
494 B = Y + 1.732446 (U-128)
497 r = (int)( y + (1.370705 * (v-128)) );
498 g = (int)( y - (0.698001 * (v-128)) - (0.337633 * (u-128)) );
499 b = (int)( y + (1.732446 * (u-128)) );
501 // Even with proper conversion, some values still need clipping.
502 if (r > 255) r = 255;
503 if (g > 255) g = 255;
504 if (b > 255) b = 255;
509 // Values only go from 0-220.. Why?
510 // pixel[0] = r * 220 / 256;
511 // pixel[1] = g * 220 / 256;
512 // pixel[2] = b * 220 / 256;
515 *pr = (unsigned char) (r * 220 / 256);
516 *pg = (unsigned char) (g * 220 / 256);
517 *pb = (unsigned char) (b * 220 / 256);
520 //printf("yuv2rgb(%i, %i, %i) -> %i, %i, %i (0x%x)\n",
522 pixel[0], pixel[1], pixel[2],