aiie/opencv/opencv-display.cpp

271 lines
6.3 KiB
C++

#include <ctype.h> // isgraph
#include "opencv-display.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/features2d/features2d.hpp"
using namespace cv;
using namespace std;
#define WINDOWNAME "6502core"
#include "bios-font.h"
#include "display-bg.h"
#include "globals.h"
#include "applevm.h"
// RGB map of each of the lowres colors
const uint8_t loresPixelColors[16][3] = { { 0, 0, 0 }, // black
{ 195, 0, 48 }, // magenta
{ 0, 0, 130 }, // dark blue
{ 166, 52, 170 }, // purple
{ 0, 146, 0 }, // dark green
{ 105, 105, 105 }, // drak grey
{ 24, 113, 255 }, // medium blue
{ 12, 190, 235 }, // light blue
{ 150, 85, 40 }, // brown
{ 255, 24, 44 }, // orange
{ 150, 170, 170 }, // light gray
{ 255, 158, 150 }, // pink
{ 0, 255, 0 }, // green
{ 255, 255, 0 }, // yellow
{ 130, 255, 130 }, // aqua
{ 255, 255, 255 } // white
};
OpenCVDisplay::OpenCVDisplay()
{
pixels = new Mat(240*2, 320*2, CV_8UC3);
namedWindow(WINDOWNAME, CV_WINDOW_AUTOSIZE);
}
OpenCVDisplay::~OpenCVDisplay()
{
delete pixels; pixels = NULL;
}
void OpenCVDisplay::redraw()
{
// primarily for the device, where it's in and out of the
// bios. Draws the background image.
for (int y=0; y<240; y++) {
for (int x=0; x<320; x++) {
uint8_t *p = &displayBitmap[(y * 320 + x)*3];
drawPixel(x, y, p[0], p[1], p[2]);
}
}
if (g_vm) {
drawDriveDoor(0, ((AppleVM *)g_vm)->DiskName(0)[0] == '\0');
drawDriveDoor(1, ((AppleVM *)g_vm)->DiskName(1)[0] == '\0');
}
}
void OpenCVDisplay::drawDriveStatus(uint8_t which, bool isRunning)
{
// location of status indicator for left drive
uint16_t xoff = 125;
uint16_t yoff = 213;
// and right drive
if (which == 1)
xoff += 135;
for (int y=0; y<1; y++) {
for (int x=0; x<6; x++) {
drawPixel(x + xoff, y + yoff, isRunning ? 0xF800 : 0x8AA9);
}
}
}
void OpenCVDisplay::drawDriveDoor(uint8_t which, bool isOpen)
{
// location of drive door for left drive
uint16_t xoff = 55;
uint16_t yoff = 216;
// location for right drive
if (which == 1) {
xoff += 134;
}
for (int y=0; y<20; y++) {
for (int x=0; x<43; x++) {
uint8_t *p = &driveLatch[(y * 43 + x)*3];
if (isOpen) {
p = &driveLatchOpen[(y * 43 + x)*3];
}
drawPixel(x+xoff, y+yoff, p[0], p[1], p[2]);
}
}
}
void OpenCVDisplay::drawBatteryStatus(uint8_t percent)
{
uint16_t xoff = 300;
uint16_t yoff = 222;
// the area around the apple is 12 wide
// it's exactly 11 high
// the color is 210/202/159
float watermark = ((float)percent / 100.0) * 11;
for (int y=0; y<11; y++) {
uint8_t bgr = 210;
uint8_t bgg = 202;
uint8_t bgb = 159;
if (11-y > watermark) {
// black...
bgr = bgg = bgb = 0;
}
for (int x=0; x<11; x++) {
uint8_t *p = &appleBitmap[(y * 10 + (x-1))*4];
// It's RGBA; blend w/ background color
uint8_t r,g,b;
float alpha = (float)p[3] / 255.0;
r = (float)p[0] * alpha + (bgr * (1.0 - alpha));
g = (float)p[1] * alpha + (bgg * (1.0 - alpha));
b = (float)p[2] * alpha + (bgb * (1.0 - alpha));
drawPixel(x+xoff, y+yoff, r, g, b);
}
}
}
#define BASEX 36
#define BASEY 26
void OpenCVDisplay::blit()
{
uint8_t *videoBuffer = g_vm->videoBuffer; // FIXME: poking deep
for (uint8_t y=0; y<192; y++) {
for (uint16_t x=0; x<280; x++) {
uint16_t pixel = (y*320+x)/2;
uint8_t colorIdx;
if (x & 1) {
colorIdx = videoBuffer[pixel] & 0x0F;
} else {
colorIdx = videoBuffer[pixel] >> 4;
}
// OpenCV is using BGR. This pixel-doubles both axes.
for (uint8_t xoff=0; xoff<2; xoff++) {
for (uint8_t yoff=0; yoff<2; yoff++) {
pixels->at<uchar>(y*2+yoff+BASEY, (x*2+xoff+BASEX)*3 + 0) = (loresPixelColors[colorIdx][2]) & 0xFF;
pixels->at<uchar>(y*2+yoff+BASEY, (x*2+xoff+BASEX)*3 + 1) = (loresPixelColors[colorIdx][1]) & 0xFF;
pixels->at<uchar>(y*2+yoff+BASEY, (x*2+xoff+BASEX)*3 + 2) = (loresPixelColors[colorIdx][0]) & 0xFF;
}
}
}
}
if (overlayMessage[0]) {
drawString(M_SELECTDISABLED, 1, 240 - 16 - 12, overlayMessage);
}
imshow(WINDOWNAME, *pixels);
}
void OpenCVDisplay::drawPixel(uint16_t x, uint8_t y, uint16_t color)
{
uint8_t
r = (color & 0xF800) >> 8,
g = (color & 0x7E0) >> 3,
b = (color & 0x1F) << 3;
// Pixel-doubling
for (int yoff=0; yoff<2; yoff++) {
for (int xoff=0; xoff<2; xoff++) {
pixels->at<uchar>(y*2+yoff, (x*2+xoff)*3 + 0) = b;
pixels->at<uchar>(y*2+yoff, (x*2+xoff)*3 + 1) = g;
pixels->at<uchar>(y*2+yoff, (x*2+xoff)*3 + 2) = r;
}
}
}
void OpenCVDisplay::drawPixel(uint16_t x, uint8_t y, uint8_t r, uint8_t g, uint8_t b)
{
// Pixel-doubling
for (int yoff=0; yoff<2; yoff++) {
for (int xoff=0; xoff<2; xoff++) {
pixels->at<uchar>(y*2+yoff, (x*2+xoff)*3 + 0) = b;
pixels->at<uchar>(y*2+yoff, (x*2+xoff)*3 + 1) = g;
pixels->at<uchar>(y*2+yoff, (x*2+xoff)*3 + 2) = r;
}
}
}
void OpenCVDisplay::drawCharacter(uint8_t mode, uint16_t x, uint8_t y, char c)
{
int8_t xsize = 8,
ysize = 0x0C,
offset = 0x20;
uint16_t temp;
c -= offset;// font starts with a space
uint16_t offPixel, onPixel;
switch (mode) {
case M_NORMAL:
onPixel = 0xFFFF;
offPixel = 0x0010;
break;
case M_SELECTED:
onPixel = 0x0000;
offPixel = 0xFFFF;
break;
case M_DISABLED:
default:
onPixel = 0x7BEF;
offPixel = 0x0000;
break;
case M_SELECTDISABLED:
onPixel = 0x7BEF;
offPixel = 0xFFE0;
break;
}
temp=(c*ysize);
for (int8_t y_off = 0; y_off <= ysize; y_off++) {
uint8_t ch = BiosFont[temp];
for (int8_t x_off = 0; x_off <= xsize; x_off++) {
if (ch & (1 << (7-x_off))) {
drawPixel(x + x_off, y + y_off, onPixel);
} else {
drawPixel(x + x_off, y + y_off, offPixel);
}
}
temp++;
}
}
void OpenCVDisplay::drawString(uint8_t mode, uint16_t x, uint8_t y, const char *str)
{
int8_t xsize = 8; // width of a char in this font
for (int8_t i=0; i<strlen(str); i++) {
drawCharacter(mode, x, y, str[i]);
x += xsize; // fixme: any inter-char spacing?
}
}
void OpenCVDisplay::debugMsg(const char *msg)
{
printf("%s\n", msg);
}