Entwicklung - Tipps und Tricks

Aus VDR Wiki
(Unterschied zwischen Versionen)
Wechseln zu: Navigation, Suche
(Image rändern)
(Download aus dem Internet)
Zeile 1: Zeile 1:
Hier sind ein paar Tips und Tricks die beim entwickeln von Plugins helfen können:
 
  
== Bilder ==
 
Es gibt zwei Möglichkeiten Bilder im Plugin zu verwenden.
 
# Das XPM Format
 
# 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. In der Datei wird das ganze dann als C Array gespeichert. Die Datei kann dann mit einem Texteditor bearbeitet werden.
 
 
Das folgende Bild einmal als Image und einmal als "Text"
 
 
[[Bild:Entwicklung-tipsundtricks-00.png]]
 
 
/* XPM */
 
static char * vdr_xpm[] = {
 
"100 50 2 1",
 
". c #FFFFFF",
 
" c #000000",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"..................    .............    ...          .............          .....................",
 
"..................    .............    ....              .........              ...................",
 
"..................    ............    ....              ........              ..................",
 
"...................    ...........    ....                .......              ..................",
 
"...................    ..........    ....    .....        ......    .....      .................",
 
"...................    .........    .....    .......      ......    .......    .................",
 
"...................    .........    .....    ........      ....    .......    .................",
 
"...................    ........    ......    ..........    ....    .......    .................",
 
"....................    ........    .......    ..........    ....    .......    .................",
 
"....................    .......    ......    ..........    ....    .......    ..................",
 
"....................    ......    .......    ..........    ....    .......    ..................",
 
"....................    .....    .......    ..........    ....    .....      ...................",
 
"....................    .....    ........    ...........    ...                ...................",
 
".....................    ....    .........    ...........    ...              ....................",
 
".....................    ....    ........    ...........    ....            ......................",
 
".....................    ...    .........    ..........    ....          ........................",
 
".....................    .    .........    ..........    .....    .    .........................",
 
"......................    .    ..........    .........    ....    ..    ........................",
 
"......................        ..........    .........    .....    ..      .......................",
 
"......................        ..........    ........      .....    ...    .......................",
 
"......................      ...........    .....        ......    ...      ......................",
 
"......................      ...........                .......    .....    ......................",
 
".......................    ............                ........    .....      .....................",
 
".......................    ............              .........    ......    .....................",
 
".......................    .............          ............    ......      ....................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"....................................................................................................",
 
"...................................................................................................."};
 
 
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 mit Transparent ersetzen kann.
 
 
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.
 
 
Einige Beispiele findet man in dem Skinelchi oder den Skinreel.
 
 
 
=== Image rändern ===
 
 
Auf diese Weise kann man recht gut fast jedes Bildformat zur Laufzeit einbinden. Dabei wird das Image auf ein cBitmap vom VDR gerändert.
 
Sicher gibt es hier mehrere Möglichkeiten. Eine recht einfache Möglichkeit ist aber das verwenden von der Magick++ Libary.
 
 
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.
 
 
 
{{Box Datei|bitmap.h|
 
<pre>
 
#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);   
 
    void Render(cBitmap &bmp, int colors, int alpha=255);
 
    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
 
</pre>
 
}}
 
 
{{Box Datei|bitmap.c|
 
<pre>
 
#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 colors, int alpha)
 
{
 
dsyslog("start to rande image");
 
    if (!loadingFailed) {
 
        // quantize the picture
 
        QuantizeImageMagick(imgkImage, colors, false);
 
 
        // generate cBitmap
 
        ConvertImgk2Bmp(bmp, imgkImage, alpha);
 
 
    } else {
 
        dsyslog("can't rander image, loading failed!!!!!!!!!!!!!!!!!");
 
    }
 
}
 
 
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++;
 
                }
 
            }
 
}
 
</pre>
 
}}
 
 
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);
 
}
 
 
== Download aus dem Internet ==
 
 
Es gibt immer wieder das Problem das Leute was aus dem Inet laden wollen.
 
Hier ist mein Vorschlag wie man es sehr schnell lösen kann. Ich verwende dazu "wget", das hat den Vorteil das es durch Proxy/Firewall durch geht. Die Datei wird dann local abgespeichert und kann von dort aus weiter verarbeitet werden.
 
 
Hier mein Vorschlag für einen download:
 
 
bool cServer::download(const char* uid, int numbers){
 
 
char tmpSystem [300];
 
 
dsyslog("Download a File from the Internet");
 
//Baue Befehl zusammen
 
sprintf(tmpSystem, "wget -O %s -o %s -T %s \"%s/%s?v=%s&n=%d\"",TMPLOCALFILENAME,LOGWGET,SERVERTIMEOUT,BASEURL,FILENAME,FIXPARAMETER,numbers);
 
dsyslog("DEBUG %s",tmpSystem);
 
//execute command
 
int dow_i = system (tmpSystem);
 
 
if (dow_i != 0){
 
dsyslog("ERROR: can't get File from %s/%s!",BASEURL,FILENAME);
 
return false;
 
}else{
 
dsyslog("succenful downloaded.");
 
}
 
return true;
 
}
 
 
== Darstellen von Textdateien ==
 
kommt bald
 

Version vom 27. März 2006, 12:58 Uhr