Entwicklung - Tipps und Tricks

Aus VDR Wiki
Version vom 27. März 2006, 11:08 Uhr von Decembersoul (Diskussion | Beiträge)

(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

Hier sind ein paar Tips und Tricks die einem beim entwickeln von Plugins helfen können:

Bilder

Es gibt zwei Möglichkeiten Bilder im Plugin zu verwenden.

  1. Das XPM Format
  2. Images rändern

Das XPM Format

XPM ist ein sehr interessantes Bildformat für C Entwickler. Das Format kann man mit vielen Bildbearbeitungsprogrammen bearbeiten und unter XPM speichern. Auf der Platte wird das ganze dann aber als C Array gespeichert.

Das folgende Bild einmal als Image und einmal als "Text"

Entwicklung-tipsundtricks-00.png

/* XPM */
static char * vdr_xpm[] = {
"100 50 2 1",
" 	c #000000",
".	c #FFFFFF",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"..................     .............     ...           .............           .....................",
"..................     .............    ....              .........              ...................",
"..................     ............     ....               ........               ..................",
"...................    ...........     ....                 .......               ..................",
"...................     ..........     ....     .....        ......     .....      .................",
"...................     .........     .....     .......      ......    .......     .................",
"...................     .........     .....     ........      ....     .......     .................",
"...................     ........     ......    ..........     ....     .......     .................",
"....................    ........    .......    ..........     ....     .......     .................",
"....................    .......     ......     ..........     ....     .......    ..................",
"....................     ......    .......     ..........     ....    .......     ..................",
"....................     .....     .......     ..........     ....    .....      ...................",
"....................     .....    ........    ...........     ...                ...................",
".....................    ....    .........    ...........     ...               ....................",
".....................    ....    ........     ...........    ....             ......................",
".....................    ...    .........     ..........     ....           ........................",
".....................     .     .........     ..........    .....    .     .........................",
"......................    .    ..........     .........     ....     ..     ........................",
"......................         ..........    .........     .....     ..      .......................",
"......................        ..........     ........      .....     ...     .......................",
"......................       ...........     .....        ......     ...      ......................",
"......................       ...........                 .......    .....     ......................", 
".......................     ............                ........    .....      .....................",
".......................     ............              .........     ......     .....................",
".......................    .............           ............     ......      ....................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................",
"....................................................................................................", 
"...................................................................................................."};

Im VDR kann man dieses sehr leicht verwenden.

Im VDR gibt es die Klasse cBitmap. Einem der Konstruktoren kann man dieses "XMP"Array übergeben. Schon kann man es verwenden. Somit lassen sich sehr leicht und auch sehr effektiv Zeichnungen oder Bilder erstellen.

Leider kennt das Format keine transparenz, daher würde ich raten die erste Farbe so zu wählen das man sie später als Transparent setzen kann. Dieses kann man einfach machen indem man die erste Farbe bei dem erstellten cBitmap mit Transparenz ersetzt.

cBitmap vdr(vdr_xpm);
vdr.SetColor(0, clrTransparent);

Ein Nachteil dieses Formates ist es das es etwas schwer ist die Bilder erst zur Laufzeit einzubinden. Meist werden sie als include in die Header eingebunden.


Image rändern

Auf diese weise kann man recht gut fast jedes Bildformat zur Laufzeit einbinden. Dabei wird das Image (in diesem Beispiel als png format) auf ein cBitmap gerändert. Sicher gibt es hier mehrere Möglichkeiten. Eine recht einfache Möglichkeit ist aber das verwenden von der Magick++ lib.

brougs78 aus dem VDR Portal hat eine schöne cOSDImageBitmap Klasse geschrieben die von einigen Leuten weiterentwicklet wurde. Falls Euch verbesserungen einfallen, könnt Ihr sie hier gerne reinschreiben.


#ifndef _OSDIMAGE_BITMAP_H_
#define _OSDIMAGE_BITMAP_H_

#define X_DISPLAY_MISSING

#include "setup.h"
#include <vdr/osd.h>
#include <vdr/skins.h>
#include <Magick++.h>

using namespace Magick;

class cOSDImageBitmap {
public:
   cOSDImageBitmap();
   ~cOSDImageBitmap();
   bool LoadZoomed(const char *file, int zoomWidth, int zoomHeight, int zoomLeft, int zoomTop);
   bool Load(const char *file);
   void Save(const char *file);
   void Render(cBitmap &bmp, int wWindow, int hWindow, int colors, bool dither);
   inline int Width() { return width; }
   inline int Height() { return height; }

private:	
   bool LoadImageMagick(Image &imgkLoad, const char *file);
   void QuantizeImageMagick(Image &imgkQuant, int colors, bool dither);
   void ConvertImgk2Bmp(cBitmap &bmp, Image &imgkConv, int colors);
   Image imgkZoom;
   int ZoomWidth, ZoomHeight, ZoomLeft, ZoomTop;
   int origWidth, origHeight;
   bool loadingFailed;
   int width, height;
};

#endif



#ifdef HAVE_C295
#include <stl.h>
#endif
#include "bitmap.h"

using namespace std; //need ???
using namespace Magick;

cOSDImageBitmap::cOSDImageBitmap() {
}

cOSDImageBitmap::~cOSDImageBitmap() {
}

bool cOSDImageBitmap::LoadZoomed(const char *file, int zoomWidth, int zoomHeight, int zoomLeft, int zoomTop) {
   bool status;
   status = LoadImageMagick(imgkZoom, file);
   if (zoomWidth != 0)
	imgkZoom.crop(Geometry(zoomWidth, zoomHeight, zoomLeft, zoomTop));
   height = imgkZoom.rows();
   width = imgkZoom.columns();
   return status;
}

bool cOSDImageBitmap::Load(const char *file)
{
   return LoadImageMagick(imgkImage, file);
}

void cOSDImageBitmap::Save(const char *file)
{
   SaveImageMagick(imgkImage, file);
}

void cOSDImageBitmap::Render(cBitmap &bmp, int wWindow, int hWindow, int colors, bool dither) {
   int w = wWindow;
   int h = hWindow;
   int wNew, hNew;
   wNew = wWindow;
   hNew = hWindow;
   if (!loadingFailed)	{
	Image imgkRender = imgkZoom;
	width = imgkRender.columns();
	height = imgkRender.rows();
	if (height != h || width != w) {
	    switch (SkinElchiSetup.resize) {
                case 0:
                    imgkRender.sample(Geometry(wNew, hNew, 0, 0) );
                    break;
                case 1:
                    imgkRender.scale(Geometry(wNew, hNew, 0, 0) );
                    break;
                case 2:
                    imgkRender.zoom(Geometry(wNew, hNew, 0, 0) );
            }
            width = imgkRender.columns();
            height = imgkRender.rows();
            if (colors == 16 && (height != wWindow || width != hWindow))
               colors = 15;
            }
	QuantizeImageMagick(imgkRender, colors, dither);
	ConvertImgk2Bmp(bmp, imgkRender, colors);
	}
    else {
 	 bmp.SetSize(w, h);
	 bmp.SetBpp((colors <= 16) ? 4 : 8);
	 width = w;
	 height = h;
	 const cFont *font = cFont::GetFont(fontSml);
	 int smalllineHeight = font->Height();
	 bmp.DrawRectangle(0, 0, width - 1, height - 1, Theme.Color(clrBackground));
	 bmp.DrawText(0, h - smalllineHeight + 1, tr("Error"), Theme.Color(clrButtonRedBg), Theme.Color(clrBackground), font, w, smalllineHeight, taCenter);
	 }	
}

bool cOSDImageBitmap::LoadImageMagick(Image &imgkLoad, const char *file) {
   try {
   	imgkLoad.read(file);
       if (imgkLoad.fileSize() == 0) {
           loadingFailed = true;
           return false;
           }
       else {
           height = imgkLoad.baseRows();
           width = imgkLoad.baseColumns();
           origWidth = width;
           origHeight = height;
           loadingFailed = false;
           return true;
           }
       }
       catch(exception &error)
   {
           loadingFailed = true;
           return false;
       }
}

void cOSDImageBitmap::SaveImageMagick(Image & imgkSave, const char *file)
{
   imgkImage.write(file);
}

void cOSDImageBitmap::QuantizeImageMagick(Image &imgkQuant, int colors, bool dither) {
	imgkQuant.quantizeColors(colors);
	imgkQuant.quantizeDither(dither);
	imgkQuant.quantize();
}

void cOSDImageBitmap::ConvertImgk2Bmp(cBitmap &bmp, Image &imgkConv, int colors) {
	int w = Width();
	int h = Height();
	tColor col;
	bmp.SetSize(w, h);
	bmp.SetBpp((colors <= 16) ? 4 : 8);
	const PixelPacket *pixels = imgkConv.getConstPixels(0, 0, w, h);
	for (int iy = 0; iy < h; iy++) {
	    for (int ix = 0; ix < w; ix++) {
               col = (0xFF << 24) 
                    | ( (pixels->green * 255 / MaxRGB) << 8) 
                    | ( (pixels->red * 255 / MaxRGB) << 16) 
                    | ( (pixels->blue * 255 / MaxRGB) );
               bmp.DrawPixel(ix, iy, col);
               pixels++;
               }
            }
}


Die Verwendung ist denkbar einfach:

cBitmap icon(75, 75, 4);
cOSDImageBitmap osdbitmap;
if(osdbitmap.Load(/video/vdr.png)){
       osdbitmap.Render(icon, 4);
       osd->DrawBitmap(20, yoff + 20, icon);
}