/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            yuv_draw.cc
 *
 *  Thu Sep 22 12:35:28 CEST 2005
 *  Copyright  2005 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of MIaV.
 *
 *  MIaV is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  MIaV is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MIaV; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "config.h"
#ifdef USE_GUI
#include "yuv_draw.h"

// for miav_app
#include "miav.h"

//#include "font.h"
#include <string.h>

#define TEXT_MARGIN 10

YUVDraw::YUVDraw()
{
  overlay = NULL;

  // One line of text!
  top_pixmap = new QPixmap(720 - TEXT_MARGIN, 20);
  bottom_pixmap = new QPixmap(720 - TEXT_MARGIN, 20);

  for(int x = 0; x < 720 - TEXT_MARGIN; x++) {
    for(int y = 0; y < 20; y++) {
      top_grey[x][y] = 255;
    }
  }

  for(int x = 0; x < 720 - TEXT_MARGIN; x++) {
    for(int y = 0; y < 20; y++) {
      bottom_grey[x][y] = 255;
    }
  }

}

YUVDraw::~YUVDraw()
{
  delete top_pixmap;
  delete bottom_pixmap;
}

void YUVDraw::setOverlay(SDL_Overlay* o)
{
  overlay = o;
}

void YUVDraw::addPixel(int x, int y, int val)
{
  if(overlay->w < x) return; // Out of range
  if(overlay->h < y) return; // Out of range

  Uint8 **pixels = overlay->pixels;
  Uint16 *pitches = overlay->pitches;

  Uint8* pixel = &pixels[0][(2 * x) + (y * pitches[0])];

  if(val > 0) *pixel = (255<*pixel+val?255:*pixel+val);
  else *pixel = (0>*pixel+val?0:*pixel+val);
}


void YUVDraw::setTopText(char* text)
{
  miav_app->lock();
  top_pixmap->fill();

	QPainter painter;
	painter.begin(top_pixmap);
	painter.setFont( QFont( "Arial", 12, QFont::Bold ) );
	painter.setPen( Qt::black );
	painter.drawText(0, 15, text);
	painter.end();

  QImage image = top_pixmap->convertToImage();

  for(int x = 0; x < 720 - TEXT_MARGIN; x++) {
    for(int y = 0; y < 20; y++) {
      top_grey[x][y] = qGray(image.pixel(x, y));
    }
  }
  miav_app->unlock();
}

void YUVDraw::setBottomText(char* text)
{
  miav_app->lock();
  bottom_pixmap->fill();

	QPainter painter;
	painter.begin(bottom_pixmap);
	painter.setFont( QFont( "Arial", 12, QFont::Bold ) );
	painter.setPen( Qt::black );
	painter.drawText(0, 15, text);
	painter.end();

  QImage image = bottom_pixmap->convertToImage();

  for(int x = 0; x < 720 - TEXT_MARGIN; x++) {
    for(int y = 0; y < 20; y++) {
      bottom_grey[x][y] = qGray(image.pixel(x, y));
    }
  }
  miav_app->unlock();
}

void YUVDraw::draw()
{
  for(int x = 0; x < 720 - TEXT_MARGIN; x++) {
    for(int y = 0; y < 20; y++) {
      if(top_grey[x][y] != 255) addPixel(x + TEXT_MARGIN, y + TEXT_MARGIN, 255 - top_grey[x][y]);
    }
  }

  for(int x = 0; x < 720 - TEXT_MARGIN; x++) {
    for(int y = 0; y < 20; y++) {
      if(bottom_grey[x][y] != 255) addPixel(x + TEXT_MARGIN, (556 - TEXT_MARGIN)+ y, 255 - bottom_grey[x][y]);
    }
  }
}






























/*
void YUVDraw::setText(int xoffset, int yoffset, char* text, int val)
{
  for(unsigned int i = 0; i < strlen(text); i++) {
    for(int x = 0; x < FONT_WIDTH; x++) {
      for(int y = 0; y < FONT_HEIGHT; y++) {
        unsigned char col = palette[letter[(int)text[i]][y][x]];
        if(col) col += val;
        addPixel(i * FONT_WIDTH + x + xoffset, y + yoffset, col);
      }
    }
  }
}
*/

/*
typedef struct{
  Uint32 format;
  int w, h;
  int planes;
  Uint16 *pitches;
  Uint8 **pixels;
  Uint32 hw_overlay:1;
} SDL_Overlay;
*/

#endif/*USE_GUI*/