summaryrefslogtreecommitdiff
path: root/src/drivers/Android
diff options
context:
space:
mode:
authorMatthias Melcher <fltk@matthiasm.com>2018-03-31 21:29:33 +0000
committerMatthias Melcher <fltk@matthiasm.com>2018-03-31 21:29:33 +0000
commit936fbd096f3cf1df506bf98a7dd9bece5391b624 (patch)
treefe873e6e4c3fd1a5304971be6ec8847dd2a48199 /src/drivers/Android
parente4916d617e989faefa8f8c1004e20174a0efcbdb (diff)
Android: Drawing RGB image data (and probaly rgba and grayscale as well). Testing unsing test/color_chooser.cxx
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12817 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/drivers/Android')
-rw-r--r--src/drivers/Android/Fl_Android_Graphics_Driver.H62
-rw-r--r--src/drivers/Android/Fl_Android_Graphics_Driver.cxx101
-rw-r--r--src/drivers/Android/Fl_Android_Graphics_Font.H15
-rw-r--r--src/drivers/Android/Fl_Android_Graphics_Font.cxx18
4 files changed, 157 insertions, 39 deletions
diff --git a/src/drivers/Android/Fl_Android_Graphics_Driver.H b/src/drivers/Android/Fl_Android_Graphics_Driver.H
index 08f9de674..198d32937 100644
--- a/src/drivers/Android/Fl_Android_Graphics_Driver.H
+++ b/src/drivers/Android/Fl_Android_Graphics_Driver.H
@@ -53,24 +53,23 @@ protected:
// - excluded by #if/#endif means that we have not implemneted this yet
// - methods marked with // super: use the implemnetation of the super class
// - virtual ... override functions are implemented for Android
-#if 0
private:
// some platforms may need to reimplement this
- virtual void set_current_();
+ // This is called from the surface device, see: end_current_()
+ // super: virtual void set_current_();
protected:
- float scale_; // scale between user and graphical coordinates: graphical = user * scale_
/** Sets the current value of the scaling factor */
- virtual void scale(float f) { scale_ = f; }
+ // super: virtual void scale(float f) { scale_ = f; } // we do not support any scaling at this point
protected:
- virtual void global_gc();
-#endif
+ // set fl_gc, which we do not use in the Android port at this point
+ // super: virtual void global_gc();
/** Support function for Fl_Pixmap drawing */
virtual fl_uintptr_t cache(Fl_Pixmap *img) override;
/** Support function for Fl_Bitmap drawing */
virtual fl_uintptr_t cache(Fl_Bitmap *img) override;
-#if 0
/** Support function for Fl_RGB_Image drawing */
- virtual void uncache(Fl_RGB_Image *img, fl_uintptr_t &id_, fl_uintptr_t &mask_) { }
+ virtual void uncache(Fl_RGB_Image *img, fl_uintptr_t &id_, fl_uintptr_t &mask_) override;
+#if 0
// --- implementation is in src/drivers/xxx/Fl_xxx_Graphics_Driver_image.cxx
/** see fl_draw_image(const uchar* buf, int X,int Y,int W,int H, int D, int L) */
virtual void draw_image(const uchar* buf, int X,int Y,int W,int H, int D=3, int L=0) {}
@@ -80,13 +79,9 @@ protected:
virtual void draw_image(Fl_Draw_Image_Cb cb, void* data, int X,int Y,int W,int H, int D=3) {}
/** see fl_draw_image_mono(Fl_Draw_Image_Cb cb, void* data, int X,int Y,int W,int H, int D) */
virtual void draw_image_mono(Fl_Draw_Image_Cb cb, void* data, int X,int Y,int W,int H, int D=1) {}
- /** \brief Draws an Fl_RGB_Image object using this graphics driver.
- *
- Specifies a bounding box for the image, with the origin (upper left-hand corner) of
- the image offset by the cx and cy arguments.
- */
- virtual void draw(Fl_RGB_Image * rgb,int XP, int YP, int WP, int HP, int cx, int cy) {}
#endif
+ /** \brief Draws an Fl_RGB_Image object using this graphics driver. */
+ virtual void draw(Fl_RGB_Image * rgb,int XP, int YP, int WP, int HP, int cx, int cy) override;
/** \brief Draws an Fl_Pixmap object using this graphics driver.
*
Specifies a bounding box for the image, with the origin (upper left-hand corner) of
@@ -106,14 +101,9 @@ protected:
virtual Fl_Bitmask create_bitmask(int w, int h, const uchar *array) {return 0; }
/** Support function for image drawing */
virtual void delete_bitmask(Fl_Bitmask bm) {}
- /** For internal library use only */
- static void change_image_size(Fl_Image *img, int W, int H) {
- img->w(W);
- img->h(H);
- }
- // Support function for image drawing
- virtual void uncache_pixmap(fl_uintptr_t p);
#endif
+ // Support function for image drawing
+ virtual void uncache_pixmap(fl_uintptr_t p) override;
public:
/** Constructor, C++11 initialises member variables in-line */
Fl_Android_Graphics_Driver();
@@ -267,16 +257,19 @@ public:
/** Support for PostScript drawing */
virtual float scale_font_for_PostScript(Fl_Font_Descriptor *desc, int s) override { return float(s); }
// default implementation may be enough
-#if 0
- /** Support for PostScript drawing */
- virtual float scale_bitmap_for_PostScript() { return 2; }
- virtual void set_spot(int font, int size, int X, int Y, int W, int H, Fl_Window *win);
- virtual void reset_spot();
+ /** Support for PostScript drawing - no documentation found on this call*/
+ // super: virtual float scale_bitmap_for_PostScript() { return 2; }
+ // super: virtual void set_spot(int font, int size, int X, int Y, int W, int H, Fl_Window *win);
+ // super: virtual void reset_spot();
// each platform implements these 3 functions its own way
- virtual void add_rectangle_to_region(Fl_Region r, int x, int y, int w, int h);
- virtual Fl_Region XRectangleRegion(int x, int y, int w, int h);
- virtual void XDestroyRegion(Fl_Region r);
-#endif
+ /* TODO: Android: we can implement this to have a redraw region based on Fl::damage
+ * calls. Currently, we do not implement damage regions, but we can probably
+ * implement this using our clipping regions. This may become neccesary when
+ * we allow desktop-style window movement.
+ */
+ // super: virtual void add_rectangle_to_region(Fl_Region r, int x, int y, int w, int h);
+ // super: virtual Fl_Region XRectangleRegion(int x, int y, int w, int h);
+ // super: virtual void XDestroyRegion(Fl_Region r);
/** Support for Fl::get_font_name() */
virtual const char* get_font_name(Fl_Font fnum, int* ap) override;
/** Support for Fl::get_font_sizes() */
@@ -291,13 +284,12 @@ public:
virtual const char *font_name(int num) override;
/** Support for Fl::set_font() */
virtual void font_name(int num, const char *name) override;
-#if 0
// Draws an Fl_Image scaled to width W & height H
- virtual int draw_scaled(Fl_Image *img, int X, int Y, int W, int H);
+ // TODO: we don't seem to need this until we introduce a scaling graphis driver
+ // super: virtual int draw_scaled(Fl_Image *img, int X, int Y, int W, int H);
/** Support function for fl_overlay_rect() and scaled GUI.
Defaut implementation may be enough */
- virtual bool overlay_rect_unscaled();
-#endif
+ // super: virtual bool overlay_rect_unscaled();
/** Support function for fl_overlay_rect() and scaled GUI.
Defaut implementation may be enough */
// super: virtual void overlay_rect(int x, int y, int w , int h) { loop(x, y, x+w-1, y, x+w-1, y+h-1, x, y+h-1); }
@@ -305,7 +297,7 @@ public:
// --- start of Android additions --------------------------------------------
// start drawing with this driver into the given window
- // TODO: how is this different to Fl_Graphics_Driver::set_current_() above
+ // The virtual call `set_current_()` changes surface, not windows
void make_current(Fl_Window*);
protected:
diff --git a/src/drivers/Android/Fl_Android_Graphics_Driver.cxx b/src/drivers/Android/Fl_Android_Graphics_Driver.cxx
index 7b74c0c01..a53cb7795 100644
--- a/src/drivers/Android/Fl_Android_Graphics_Driver.cxx
+++ b/src/drivers/Android/Fl_Android_Graphics_Driver.cxx
@@ -1031,10 +1031,10 @@ fl_uintptr_t Fl_Android_Graphics_Driver::cache(Fl_Pixmap *img)
const uchar *src = rgba + yy*rowBytes;
uint32_t *dst = cache->pWords + yy*cache->pStride;
for (int xx=0; xx<w; xx++) {
- uint32_t c = ((((src[0] << 8) & 0xf800) |
- ((src[1] << 3) & 0x07e0) |
- ((src[2] >> 3) & 0x001f) ) << 16) | src[3]; // FIXME: alpha
- *dst++ = c;
+// uint32_t c = ((((src[0] << 8) & 0xf800) |
+// ((src[1] << 3) & 0x07e0) |
+// ((src[2] >> 3) & 0x001f) ) << 16) | src[3]; // FIXME: alpha
+ *dst++ = Fl_Android_565A_Map::toRGBA(src[0],src[1], src[2], src[3]);
src+=4;
}
}
@@ -1044,6 +1044,86 @@ fl_uintptr_t Fl_Android_Graphics_Driver::cache(Fl_Pixmap *img)
}
+void Fl_Android_Graphics_Driver::uncache_pixmap(fl_uintptr_t p)
+{
+ Fl_Android_565A_Map *img = (Fl_Android_565A_Map*)p;
+ delete img;
+}
+
+
+void Fl_Android_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int cx, int cy)
+{
+ int X, Y, W, H;
+ // Don't draw an empty image...
+ if (!img->d() || !img->array) {
+ Fl_Graphics_Driver::draw_empty(img, XP, YP);
+ return;
+ }
+ if (start_image(img, XP, YP, WP, HP, cx, cy, X, Y, W, H)) {
+ return;
+ }
+ Fl_Android_565A_Map *cgimg = (Fl_Android_565A_Map*)*Fl_Graphics_Driver::id(img);
+ if (!cgimg) {
+ int w = img->w(), h = img->h(), d = img->d(), stride = w*d + img->ld();
+ cgimg = new Fl_Android_565A_Map(w, h);
+ *Fl_Graphics_Driver::id(img) = (fl_uintptr_t)cgimg;
+ if (d==1) { // grayscale
+ for (int iy=0; iy<h; iy++) {
+ const uchar *src = img->array + iy*stride;
+ uint32_t *dst = cgimg->pWords + iy*cgimg->pStride;
+ for (int ix=0; ix<w; ix++) {
+ uchar l = *src++;
+ uint32_t rgba = Fl_Android_565A_Map::toRGBA(l, l, l, 255);
+ *dst++ = rgba;
+ }
+ }
+ } else if (d==2) { // gray + alpha
+ for (int iy=0; iy<h; iy++) {
+ const uchar *src = img->array + iy*stride;
+ uint32_t *dst = cgimg->pWords + iy*cgimg->pStride;
+ for (int ix=0; ix<w; ix++) {
+ uchar l = *src++, a = *src++;
+ uint32_t rgba = Fl_Android_565A_Map::toRGBA(l, l, l, a);
+ *dst++ = rgba;
+ }
+ }
+ } else if (d==3) { // rgb
+ for (int iy=0; iy<h; iy++) {
+ const uchar *src = img->array + iy*stride;
+ uint32_t *dst = cgimg->pWords + iy*cgimg->pStride;
+ for (int ix=0; ix<w; ix++) {
+ uchar r = *src++, g = *src++, b = *src++;
+ uint32_t rgba = Fl_Android_565A_Map::toRGBA(r, g, b, 255);
+ *dst++ = rgba;
+ }
+ }
+ } else if (d==4) { // rgb + alpha
+ for (int iy=0; iy<h; iy++) {
+ const uchar *src = img->array + iy*stride;
+ uint32_t *dst = cgimg->pWords + iy*cgimg->pStride;
+ for (int ix=0; ix<w; ix++) {
+ uchar r = *src++, g = *src++, b = *src++, a = *src++;
+ uint32_t rgba = Fl_Android_565A_Map::toRGBA(r, g, b, a);
+ *dst++ = rgba;
+ }
+ }
+ }
+ }
+ if (cgimg) {
+ for (const auto &it: pClippingRegion.overlapping(Fl_Rect_Region(X, Y, W, H))) {
+ draw(XP, YP, cgimg, it->clipped_rect());
+ }
+ }
+}
+
+
+void Fl_Android_Graphics_Driver::uncache(Fl_RGB_Image*, fl_uintptr_t &id_, fl_uintptr_t&)
+{
+ Fl_Android_565A_Map *cgimg = (Fl_Android_565A_Map*)id_;
+ delete cgimg;
+}
+
+
void Fl_Android_Graphics_Driver::set_color(Fl_Color i, unsigned int c)
{
if (i>255) return;
@@ -1056,6 +1136,19 @@ void Fl_Android_Graphics_Driver::color(uchar r, uchar g, uchar b)
color( (((Fl_Color)r)<<24)|(((Fl_Color)g)<<16)|(((Fl_Color)b)<<8) );
}
+/**
+ * Draw a rectangle that may be dithered if we are in colormap mode (which in
+ * the year 2018 is as likely has a user with a berstein colored tube TV).
+ * FIXME: This function should be virtual as well, or should not exist at all.
+ */
+void fl_rectf(int x, int y, int w, int h, uchar r, uchar g, uchar b) {
+#if USE_COLORMAP
+ // ...
+#endif
+ fl_color(r,g,b);
+ fl_rectf(x,y,w,h);
+}
+
//
// End of "$Id$".
diff --git a/src/drivers/Android/Fl_Android_Graphics_Font.H b/src/drivers/Android/Fl_Android_Graphics_Font.H
index 4bab47786..358af6fa0 100644
--- a/src/drivers/Android/Fl_Android_Graphics_Font.H
+++ b/src/drivers/Android/Fl_Android_Graphics_Font.H
@@ -23,7 +23,10 @@
#include "Fl_Android_Graphics_Driver.H"
// We violate FLTKs avoidance of STL because we live in a defined driver space
+#define FL_ALLOW_STL 1
+#ifdef FL_ALLOW_STL
#include <map>
+#endif
#include "stb_truetype.h"
@@ -31,6 +34,8 @@
/**
* A bytemap is an array of bytes, used as an alpha channel when redering glyphs
* in a given color.
+ * TODO: reate a class for RGB only and for grayscale and grayscale with alpha
+ * TODO: derive all this from a baseclass, so we can create the correct class for the required image
*/
class Fl_Android_Bytemap
{
@@ -58,6 +63,12 @@ public:
Fl_Android_565A_Map();
Fl_Android_565A_Map(int w, int h);
~Fl_Android_565A_Map();
+ static inline uint32_t toRGBA(uchar r, uchar g, uchar b, uchar a)
+ {
+ return ((((r << 8) & 0xf800) |
+ ((g << 3) & 0x07e0) |
+ ((b >> 3) & 0x001f)) << 16) | a;
+ }
public:
int pWidth = 0, pHeight = 0, pStride = 0;
@@ -98,7 +109,11 @@ public:
*/
class Fl_Android_Font_Descriptor : public Fl_Font_Descriptor
{
+#ifdef FL_ALLOW_STL
typedef std::map<uint32_t, Fl_Android_Bytemap*> BytemapTable;
+#else
+ typedef Fl_Android_Bytemap* BytemapTable[256];
+#endif
private:
Fl_Android_Font_Source *pFontSource;
Fl_Font pFontIndex;
diff --git a/src/drivers/Android/Fl_Android_Graphics_Font.cxx b/src/drivers/Android/Fl_Android_Graphics_Font.cxx
index 18ad02e2c..f15a0c585 100644
--- a/src/drivers/Android/Fl_Android_Graphics_Font.cxx
+++ b/src/drivers/Android/Fl_Android_Graphics_Font.cxx
@@ -476,10 +476,16 @@ Fl_Android_Font_Descriptor::Fl_Android_Font_Descriptor(const char *fname, Fl_And
*/
Fl_Android_Font_Descriptor::~Fl_Android_Font_Descriptor()
{
+#ifdef FL_ALLOW_STL
// Life is easy in C++11.
for (auto &i: pBytemapTable) {
delete i.second; i.second = nullptr;
}
+#else
+ for (int i=0; i<256; i++) {
+ if (pBytemapTable[i]) delete pBytemapTable[i];
+ }
+#endif
}
/*
@@ -508,6 +514,7 @@ float Fl_Android_Font_Descriptor::get_advance(uint32_t c)
Fl_Android_Bytemap *Fl_Android_Font_Descriptor::get_bytemap(uint32_t c)
{
Fl_Android_Bytemap *bm = 0;
+#ifdef FL_ALLOW_STL
auto it = pBytemapTable.find(c);
if (it==pBytemapTable.end()) {
bm = pFontSource->get_bytemap(c, size);
@@ -516,6 +523,17 @@ Fl_Android_Bytemap *Fl_Android_Font_Descriptor::get_bytemap(uint32_t c)
} else {
bm = it->second;
}
+#else
+ if (c<256) {
+ if (pBytemapTable[c]) {
+ bm = pBytemapTable[c];
+ } else {
+ bm = pFontSource->get_bytemap(c, size);
+ if (bm)
+ pBytemapTable[c] = bm;
+ }
+ }
+#endif
return bm;
}