version 2.8
gdisp_image_native.c
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.org/license.html
6  */
7 
8 #include "../../gfx.h"
9 
10 #if GFX_USE_GDISP && GDISP_NEED_IMAGE && GDISP_NEED_IMAGE_NATIVE
11 
12 #include "gdisp_image_support.h"
13 
14 /**
15  * How big a pixel array to allocate for blitting
16  * Bigger is faster but uses more RAM.
17  */
18 #define BLIT_BUFFER_SIZE_NATIVE 32
19 
20 #define HEADER_SIZE_NATIVE 8
21 #define FRAME0POS_NATIVE (HEADER_SIZE_NATIVE)
22 
23 /**
24  * Helper Routines Needed
25  */
26 void *gdispImageAlloc(gdispImage *img, size_t sz);
27 void gdispImageFree(gdispImage *img, void *ptr, size_t sz);
28 
29 typedef struct gdispImagePrivate_NATIVE {
30  pixel_t *frame0cache;
31  pixel_t buf[BLIT_BUFFER_SIZE_NATIVE];
32  } gdispImagePrivate_NATIVE;
33 
34 void gdispImageClose_NATIVE(gdispImage *img) {
35  gdispImagePrivate_NATIVE * priv;
36 
37  priv = (gdispImagePrivate_NATIVE *)img->priv;
38  if (priv) {
39  if (priv->frame0cache)
40  gdispImageFree(img, (void *)priv->frame0cache, img->width * img->height * sizeof(pixel_t));
41  gdispImageFree(img, (void *)priv, sizeof(gdispImagePrivate_NATIVE));
42  img->priv = 0;
43  }
44 }
45 
46 gdispImageError gdispImageOpen_NATIVE(gdispImage *img) {
47  uint8_t hdr[HEADER_SIZE_NATIVE];
48 
49  /* Read the 8 byte header */
50  if (gfileRead(img->f, hdr, 8) != 8)
51  return GDISP_IMAGE_ERR_BADFORMAT; // It can't be us
52 
53  if (hdr[0] != 'N' || hdr[1] != 'I')
54  return GDISP_IMAGE_ERR_BADFORMAT; // It can't be us
55 
56  if (hdr[6] != GDISP_PIXELFORMAT/256 || hdr[7] != (GDISP_PIXELFORMAT & 0xFF))
57  return GDISP_IMAGE_ERR_UNSUPPORTED; // Unsupported pixel format
58 
59  /* We know we are a native format image */
60  img->flags = 0;
61  img->width = (((uint16_t)hdr[2])<<8) | (hdr[3]);
62  img->height = (((uint16_t)hdr[4])<<8) | (hdr[5]);
63  if (img->width < 1 || img->height < 1)
64  return GDISP_IMAGE_ERR_BADDATA;
65  if (!(img->priv = gdispImageAlloc(img, sizeof(gdispImagePrivate_NATIVE))))
66  return GDISP_IMAGE_ERR_NOMEMORY;
67  ((gdispImagePrivate_NATIVE *)(img->priv))->frame0cache = 0;
68 
69  img->type = GDISP_IMAGE_TYPE_NATIVE;
70  return GDISP_IMAGE_ERR_OK;
71 }
72 
73 gdispImageError gdispImageCache_NATIVE(gdispImage *img) {
74  size_t len;
75  gdispImagePrivate_NATIVE * priv;
76 
77  /* If we are already cached - just return OK */
78  priv = (gdispImagePrivate_NATIVE *)img->priv;
79  if (priv->frame0cache)
80  return GDISP_IMAGE_ERR_OK;
81 
82  /* We need to allocate the cache */
83  len = img->width * img->height * sizeof(pixel_t);
84  priv->frame0cache = (pixel_t *)gdispImageAlloc(img, len);
85  if (!priv->frame0cache)
86  return GDISP_IMAGE_ERR_NOMEMORY;
87 
88  /* Read the entire bitmap into cache */
89  gfileSetPos(img->f, FRAME0POS_NATIVE);
90  if (gfileRead(img->f, priv->frame0cache, len) != len)
91  return GDISP_IMAGE_ERR_BADDATA;
92 
93  return GDISP_IMAGE_ERR_OK;
94 }
95 
96 gdispImageError gdispGImageDraw_NATIVE(GDisplay *g, gdispImage *img, coord_t x, coord_t y, coord_t cx, coord_t cy, coord_t sx, coord_t sy) {
97  coord_t mx, mcx;
98  size_t pos, len;
99  gdispImagePrivate_NATIVE * priv;
100 
101  priv = (gdispImagePrivate_NATIVE *)img->priv;
102 
103  /* Check some reasonableness */
104  if (sx >= img->width || sy >= img->height) return GDISP_IMAGE_ERR_OK;
105  if (sx + cx > img->width) cx = img->width - sx;
106  if (sy + cy > img->height) cy = img->height - sy;
107 
108  /* Draw from the image cache - if it exists */
109  if (priv->frame0cache) {
110  gdispGBlitArea(g, x, y, cx, cy, sx, sy, img->width, priv->frame0cache);
111  return GDISP_IMAGE_ERR_OK;
112  }
113 
114  /* For this image decoder we cheat and just seek straight to the region we want to display */
115  pos = FRAME0POS_NATIVE + (img->width * sy + sx) * sizeof(pixel_t);
116 
117  /* Cycle through the lines */
118  for(;cy;cy--, y++) {
119  /* Move to the start of the line */
120  gfileSetPos(img->f, pos);
121 
122  /* Draw the line in chunks using BitBlt */
123  for(mx = x, mcx = cx; mcx > 0; mcx -= len, mx += len) {
124  // Read the data
125  len = gfileRead(img->f,
126  priv->buf,
127  mcx > BLIT_BUFFER_SIZE_NATIVE ? (BLIT_BUFFER_SIZE_NATIVE*sizeof(pixel_t)) : (mcx * sizeof(pixel_t)))
128  / sizeof(pixel_t);
129  if (!len)
130  return GDISP_IMAGE_ERR_BADDATA;
131 
132  /* Blit the chunk of data */
133  gdispGBlitArea(g, mx, y, len, 1, 0, 0, len, priv->buf);
134  }
135 
136  /* Get the position for the start of the next line */
137  pos += img->width*sizeof(pixel_t);
138  }
139 
140  return GDISP_IMAGE_ERR_OK;
141 }
142 
143 delaytime_t gdispImageNext_NATIVE(gdispImage *img) {
144  (void) img;
145 
146  /* No more frames/pages */
147  return TIME_INFINITE;
148 }
149 
150 #endif /* GFX_USE_GDISP && GDISP_NEED_IMAGE && GDISP_NEED_IMAGE_NATIVE */
int16_t coord_t
The type for a coordinate or length on the screen.
Definition: gdisp.h:39
size_t gfileRead(GFILE *f, void *buf, size_t len)
Read from file.
bool_t gfileSetPos(GFILE *f, long int pos)
Set the position of the read/write cursor.
The structure for an image.
Definition: gdisp_image.h:59
void gdispGBlitArea(GDisplay *g, coord_t x, coord_t y, coord_t cx, coord_t cy, coord_t srcx, coord_t srcy, coord_t srccx, const pixel_t *buffer)
Fill an area using the supplied bitmap.
GDISP image support routines header file.
uint16_t gdispImageError
An image error code.
Definition: gdisp_image.h:37
color_t pixel_t
The pixel format.
Definition: gdisp.h:226