summaryrefslogtreecommitdiff
path: root/glcdgraphics/extformats.c
blob: 75d369386e50a7d77e43412c3ca6caf40b81aca4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * GraphLCD graphics library
 *
 * extformats.c  -  loading and saving of external formats (via ImageMagick)
 *
 * based on bitmap.[ch] from text2skin: http://projects.vdr-developer.org/projects/show/plg-text2skin
 *
 * This file is released under the GNU General Public License. Refer
 * to the COPYING file distributed with this package.
 *
 * (c) 2011      Wolfgang Astleitner <mrwastl AT users sourceforge net>
 */

#include <stdio.h>
#include <stdint.h>
#include <syslog.h>

#include <cstring>

#include "bitmap.h"
#include "extformats.h"
#include "image.h"

#ifdef HAVE_IMAGEMAGICK
#include <Magick++.h>
using namespace Magick;
//#elif defined(HAVE_IMLIB2)
//#include "quantize.h"
//#include <Imlib2.h>
#endif


namespace GLCD
{

using namespace std;


cExtFormatFile::cExtFormatFile()
{
#ifdef HAVE_IMAGEMAGICK
  InitializeMagick(NULL);
#endif    
}

cExtFormatFile::~cExtFormatFile()
{
}

bool cExtFormatFile::Load(cImage & image, const string & fileName)
{
#ifdef HAVE_IMAGEMAGICK
  std::vector<Image> extimages;
  try {
    uint16_t width = 0;
    uint16_t height = 0;
    //uint16_t count;
    uint32_t delay;

    std::vector<Image>::iterator it;
    readImages(&extimages, fileName);
    if (extimages.size() == 0) {
      syslog(LOG_ERR, "ERROR: graphlcd: Couldn't load %s", fileName.c_str());
      return false;
    }

    delay = (uint32_t)(extimages[0].animationDelay() * 10);

    image.Clear();
    image.SetDelay(delay);

    bool firstImage = true;

    for (it = extimages.begin(); it != extimages.end(); ++it) {
      bool ignoreImage = false;

      //if (colors != 0){
        //(*it).opacity(OpaqueOpacity);
        //(*it).backgroundColor( Color ( 0,0,0,0) );
        (*it).quantizeColorSpace( RGBColorspace );
        (*it).quantizeColors( 256*256*256 /*colors*/ );
        (*it).quantize();
      //}

      if (firstImage) {
        width = (uint16_t)((*it).columns());
        height = (uint16_t)((*it).rows());
        image.SetWidth(width);
        image.SetHeight(height);
        firstImage = false;
      } else {
        if ( (width != (uint16_t)((*it).columns())) || (height != (uint16_t)((*it).rows())) ) {
          ignoreImage = true;
        }
      }

      if (! ignoreImage) {
        /*
        if ((*it).depth() > 8) {
          esyslog("ERROR: text2skin: More than 8bpp images are not supported");
          return false;
        }
        */
        uint32_t * bmpdata = new uint32_t[height * width];
        //Dprintf("this image has %d colors\n", (*it).totalColors());

        bool isMatte = (*it).matte();
        const PixelPacket *pix = (*it).getConstPixels(0, 0, (int)width, (int)height);

        for (int iy = 0; iy < (int)height; ++iy) {
          for (int ix = 0; ix < (int)width; ++ix) {
            if ( isMatte && pix->opacity == MaxRGB ) {
                bmpdata[iy*width+ix] = cColor::Transparent;
            } else {
                //bmpdata[iy*width+ix] = (uint32_t)( 0xFF000000 | (int(pix->red * 255 / MaxRGB) << 16) | (int(pix->green * 255 / MaxRGB) << 8) | int(pix->blue * 255 / MaxRGB));
                bmpdata[iy*width+ix] = (uint32_t)( (int(255 - (pix->opacity * 255 / MaxRGB)) << 24)  | (int(pix->red * 255 / MaxRGB) << 16) | (int(pix->green * 255 / MaxRGB) << 8) | int(pix->blue * 255 / MaxRGB));
            }
            ++pix;
          }
        }
        cBitmap * b = new cBitmap(width, height, bmpdata);
        b->SetMonochrome(false);
        image.AddBitmap(b);
        delete[] bmpdata;
        bmpdata = NULL;
      }
    }
  } catch (Exception &e) {
    syslog(LOG_ERR, "ERROR: graphlcd: Couldn't load %s: %s", fileName.c_str(), e.what());
    return false;
  } catch (...) {
    syslog(LOG_ERR, "ERROR: graphlcd: Couldn't load %s: Unknown exception caught", fileName.c_str());
    return false;
  }
  return true;
#else
  return false;
#endif    
}

// to be done ...
bool cExtFormatFile::Save(cImage & image, const string & fileName)
{
  return false;
}

} // end of namespace