MHEG5  18.9.0
MHEG5 Documentation
mg_bmp.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2008 Ocean Blue Software Ltd
4  *
5  * This file is part of a DTVKit Software Component
6  * You are permitted to copy, modify or distribute this file subject to the terms
7  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
8  *
9  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
10  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * If you or your organisation is not a member of DTVKit then you have access
14  * to this source code outside of the terms of the licence agreement
15  * and you are expected to delete this and any associated files immediately.
16  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
17  *******************************************************************************/
25 #undef DEBUG_FUNCTION
26 
27 /*---includes for this file--------------------------------------------------*/
28 /* compiler library header files */
29 #include <stdlib.h>
30 #include <string.h> /* memset */
31 #include <stdio.h>
32 
33 /* OBS header files */
34 #include "mg_api.h"
35 #include "glue_memory.h"
36 #include "glue_debug.h"
37 #include "osd_utils.h"
38 #include "mg_osd.h"
39 #include "decoder.h"
40 #include "mh5bitmap.h" /* Bitmap class header file. */
41 
42 /*---constant definitions for this file--------------------------------------*/
43 
44 
45 /*---local typedef structs for this file-------------------------------------*/
46 
47 
48 /*---static function definitions---------------------------------------------*/
49 
50 static void DrawBmpOnSurface( S_IMAGE *s_bmp, S_SURFACE *d_surf, int tiling,
51  int left, int top, int width, int height )
52 {
53  U32BIT d_width, d_height;
54 
55  d_width = s_bmp->width;
56  d_height = s_bmp->height;
57 
58  if (s_bmp->y_resolution == 5315)
59  {
60  /* PNG ready for 1920x1080 screen. */
61  if (mg_ctxt.out_osd_height != 1080)
62  {
63  /* 720 -> 1920 is multiply by 8/3 */
64 
65  /*width is already scaled for 1920x1080
66  need to scale down to actual resolution
67  w' = w / 1920 * actual
68 
69  other order:
70  w' = w * actual / 1920
71 
72  but we don't have the actual resolution. It's stored as 16/9 for 1280x720 (for example)
73  we also don't have 1920, but 8/3
74 
75  so
76 
77  w' = w * (actual/720) / (1920/720)
78  w' = w * (actual * 720) / (1920 * 720)
79 
80  but this may overflow (actually would never overflow)
81 
82  w' = w * (screen_x/norm_x) / (8/3)
83  w' = w * (screen_x/norm_x) * (3/8)
84  w' = (w * screen_x * 3) / (norm_x * 8)
85 
86  We then round it by
87  w' = ( w * screen_x * 3 + (norm_x * 8)/2 ) / (norm_x * 8)
88 
89  */
90 
91  d_width *= mg_ctxt.osd_x.mlt * 3;
92  d_width += (mg_ctxt.osd_x.div << 2);
93  d_width /= (mg_ctxt.osd_x.div << 3);
94  /* 576 -> 1080 is multiply by 15/8 */
95  d_height *= mg_ctxt.osd_y.mlt << 3;
96  d_height += (mg_ctxt.osd_y.div * 15) >> 1;
97  d_height /= (mg_ctxt.osd_y.div * 15);
98  }
99  }
100  else if (s_bmp->y_resolution == 3543)
101  {
102  /* PNG ready for 1280x720 screen */
103  if (mg_ctxt.out_osd_height != 720)
104  {
105  /* 720 -> 1280 is multiply by 16/9 */
106  d_width *= mg_ctxt.osd_x.mlt * 9;
107  d_width += (mg_ctxt.osd_x.div << 3);
108  d_width /= (mg_ctxt.osd_x.div << 4);
109  /* 576 -> 720 is multiply by 5/4 */
110  d_height *= mg_ctxt.osd_y.mlt << 3;
111  d_height += (mg_ctxt.osd_y.div * 5);
112  d_height /= (mg_ctxt.osd_y.div * 10);
113  }
114  }
115  else
116  {
117  if (mg_ctxt.out_osd_height != 576)
118  {
119 
120  /* The PNG is ready for 720x576, needs to be scaled up to actual resolution
121 
122  w' = w * actual_x / 720
123  h' = h * actual_y / 576
124 
125  Not allowed to do this directly, due to intelligent rendering
126  Instead, we have to calculate for left and right separately
127  */
128  d_width = HD_X_SCALEUP(left + d_width) - HD_X_SCALEUP(left);
129  d_height = HD_Y_SCALEUP(top + d_height) - HD_Y_SCALEUP(top);
130  }
131  }
132 
133  d_surf->opaque = s_bmp->opaque;
134  if (!tiling)
135  {
136  d_surf->width = d_width;
137  d_surf->height = d_height;
138  d_surf->buff_pitch = d_width * BytesPerPixel(mg_ctxt.colour_format);
139  d_surf->hw_handle = DEC_OSDCreateBmpSurf( d_width, d_height, d_width, d_height,
140  s_bmp, &d_surf->buff_pitch );
141  }
142  else
143  {
144  d_surf->width = (((left + width) * mg_ctxt.osd_x.mlt) / mg_ctxt.osd_x.div) -
145  ((left * mg_ctxt.osd_x.mlt) / mg_ctxt.osd_x.div);
146  if (d_surf->width < d_width)
147  d_surf->width = d_width;
148  d_surf->height = (((top + height) * mg_ctxt.osd_y.mlt) / mg_ctxt.osd_y.div) -
149  ((top * mg_ctxt.osd_y.mlt) / mg_ctxt.osd_y.div);
150  if (d_surf->height < d_height)
151  d_surf->height = d_height;
152  d_surf->opaque = s_bmp->opaque;
153  d_surf->buff_pitch = d_surf->width * BytesPerPixel(mg_ctxt.colour_format);
154  d_surf->hw_handle = DEC_OSDCreateBmpSurf( d_surf->width, d_surf->height, d_width, d_height,
155  s_bmp, &d_surf->buff_pitch );
156  }
157 }
158 
159 /*---global function definitions---------------------------------------------*/
160 
161 void MG_DrawPng( void *data, int len, MHEG5Visible *visible )
162 {
163  S_IMAGE *bmp;
164  S_SURFACE *b_srf;
165 
166  assert( visible->graphic_data == NULL );
167 
168  if (data && len && visible->boxSize[0] != 0 && visible->boxSize[1] != 0)
169  {
170  bmp = DEC_DecodePng( data, len );
171  if (bmp)
172  {
173  TRACE(TGRAPHICS, ("decode bmp: w=%d h=%d", bmp->width, bmp->height))
174  b_srf = OSD_MemAlloc( sizeof(S_SURFACE));
175  if (b_srf != NULL)
176  {
178  {
179  #ifdef OSD_8_BIT
180  U32BIT sz = bmp->width * bmp->height;
181  /* Not HD so no scaling up */
182  assert( mg_ctxt.osd_y.mlt == mg_ctxt.osd_y.div );
183  b_srf->col_buff = OSD_MemAlloc( sz );
184  if (b_srf->col_buff == NULL)
185  {
186  OSD_MemFree( b_srf );
187  b_srf = NULL;
188  }
189  else
190  {
191  b_srf->width = bmp->width;
192  b_srf->height = bmp->height;
193  b_srf->opaque = bmp->opaque;
194  b_srf->srf_type = SRF_TYPE_PNG;
195  b_srf->buff_pitch = bmp->width;
196  b_srf->hw_handle = NULL; /*assign to something sensible*/
197  memcpy( b_srf->col_buff, bmp->col_buff, sz );
198  TRACE(TGRAPHICS, ("copied bmp"))
199  /*TRACE_BUFF(TGRAPHICS,b_srf->col_buff,bmp->width)*/
200  }
201  #endif /*OSD_8_BIT*/
202  }
203  ELSE
204  {
205  #if defined(OSD_16_BIT) || defined(OSD_31_BIT) || defined(OSD_32_BIT)
206  DrawBmpOnSurface( bmp, b_srf, ((MHEG5Bitmap *) visible)->tiling,
207  visible->position[0], visible->position[1], visible->boxSize[0], visible->boxSize[1] );
208  if (b_srf->hw_handle == NULL)
209  {
210  OSD_MemFree( b_srf );
211  b_srf = NULL;
212  }
213  #ifdef SRF_MEM_DGB
214  else
215  {
216  OSD_DbgAddSurf( b_srf );
217  }
218  #endif
219  #endif
220  }
221  }
222  DEC_FreePng( bmp );
223  }
224  else
225  {
226  if (data != NULL && len > 0)
227  {
228  TRACE(TERROR, ("DEC_DecodePng returned NULL"))
229  }
230  b_srf = 0;
231  }
232  visible->graphic_data = b_srf;
233  }
234 }
235 
236 void MG_DrawJpg( void *data, int len, MHEG5Visible *visible )
237 {
238  S_SURFACE *b_srf = 0;
239  S_IMAGE *bmp;
240 
241  if (data && len && visible->boxSize[0] != 0 && visible->boxSize[1] != 0)
242  {
243  bmp = DEC_DecodeJpg( data, len );
244  if (bmp)
245  {
246  b_srf = OSD_MemAlloc( sizeof(S_SURFACE));
247  if (b_srf != NULL)
248  {
250  {
251  #ifdef OSD_8_BIT
252  U32BIT sz = bmp->width * bmp->height;
253  /* Not HD so no scaling up */
254  assert( mg_ctxt.osd_y.mlt == mg_ctxt.osd_y.div );
255  b_srf->col_buff = OSD_MemAlloc( sz );
256  if (b_srf->col_buff == NULL)
257  {
258  OSD_MemFree( b_srf );
259  b_srf = NULL;
260  }
261  else
262  {
263  b_srf->width = bmp->width;
264  b_srf->height = bmp->height;
265  b_srf->opaque = bmp->opaque;
266  b_srf->srf_type = SRF_TYPE_JPG;
267  b_srf->buff_pitch = bmp->width;
268  b_srf->hw_handle = NULL; /*assign to something sensible*/
269  memcpy( b_srf->col_buff, bmp->col_buff, sz );
270  }
271  #endif /*OSD_8_BIT*/
272  }
273  ELSE
274  {
275  #if defined(OSD_16_BIT) || defined(OSD_31_BIT) || defined(OSD_32_BIT)
276  DrawBmpOnSurface( bmp, b_srf, ((MHEG5Bitmap *) visible)->tiling,
277  visible->position[0], visible->position[1], visible->boxSize[0], visible->boxSize[1] );
278  if (b_srf->hw_handle == NULL)
279  {
280  OSD_MemFree( b_srf );
281  b_srf = NULL;
282  }
283  #ifdef SRF_MEM_DGB
284  else
285  {
286  OSD_DbgAddSurf( b_srf );
287  }
288  #endif
289  #endif
290  }
291  }
292  DEC_FreeJpg( bmp );
293  }
294  }
295  visible->graphic_data = b_srf;
296 }
297 
298 void MG_OSDdisplayBitmap( MHEG5Visible *visible, S_REGION *overlap )
299 {
300  S_SURFACE *surf;
301  int left, top, right, bottom;
302  U32BIT dummy;
303 
304  assert( visible->graphic_data != NULL );
305 
306  surf = (S_SURFACE *)visible->graphic_data;
307 
308  if (surf->width == 1 && surf->height == 1 && ((MHEG5Bitmap *) visible)->tiling)
309  {
310  /* Special case for a tiling 1 pixel bitmap - it's the same as a plain rectangle */
311  OSDColor box_col;
312 
314  {
315  #ifdef OSD_8_BIT
316  box_col = mg_palette[*((U8BIT *)surf->col_buff)];
317  #endif
318  }
320  {
321  #ifdef OSD_16_BIT
322  left = *((OSDColor *)STB_OSDMhegLockBuffer( surf->hw_handle, &dummy ));
323  box_col = (left & 0xf000) << 16 |
324  (left & 0x0f00) << 12 |
325  (left & 0x00f0) << 8 |
326  (left & 0x000f) << 4;
328  #endif
329  }
330 
331  ELSE
332  {
333  #if defined(OSD_31_BIT) || defined(OSD_32_BIT)
334  box_col = *((OSDColor *)STB_OSDMhegLockBuffer( surf->hw_handle, &dummy ));
336  #endif
337  }
338 
339  MG_OSDdisplayRectangle( overlap, visible->position[0], visible->position[1],
340  visible->boxSize[0], visible->boxSize[1], 0, 0, box_col );
341  }
342  else if (surf->width != 0 && surf->height != 0)
343  {
344  left = (visible->position[0] * mg_ctxt.osd_x.mlt) / mg_ctxt.osd_x.div;
345  top = (visible->position[1] * mg_ctxt.osd_y.mlt) / mg_ctxt.osd_y.div;
346 
347  TRACE(TGRAPHICS, ("id=%ld left=%d top=%d", visible->ingredient.root.id, left, top))
348 
349  MG_DisplayImage( surf, overlap, left, top );
350 
351  if (((MHEG5Bitmap *) visible)->tiling)
352  {
353  right = visible->position[0] + visible->boxSize[0];
354  right = (right * mg_ctxt.osd_x.mlt) / mg_ctxt.osd_x.div;
355  bottom = visible->position[1] + visible->boxSize[1];
356  bottom = (bottom * mg_ctxt.osd_y.mlt) / mg_ctxt.osd_y.div;
357 
358  TRACE(TGRAPHICS, (" width=%d height=%d", surf->width, surf->height))
359  left += surf->width;
360 
361  do
362  {
363  while (left < right)
364  {
365  MG_DisplayImage( surf, overlap, left, top );
366  left += surf->width;
367  }
368  left = (visible->rect.left * mg_ctxt.osd_x.mlt) / mg_ctxt.osd_x.div;
369  top += surf->height;
370  }
371  while (top < bottom);
372  }
373  }
374 }
375 
OSD utility functions.
U8BIT colour_format
Definition: mg_ctxt.h:94
#define IF_COL_DEPTH(cd)
Definition: mg_ctxt.h:65
void DEC_FreeJpg(S_IMAGE *image)
Free memory allocated for JPEG bitmap.
Definition: mg_jpg.c:495
MHEG5Int boxSize[2]
Definition: mh5visible.h:58
#define OSD_MemAlloc
Definition: glue_memory.h:96
void MG_DrawPng(void *data, int len, MHEG5Visible *visible)
Definition: mg_bmp.c:161
void * col_buff
Definition: mg_osd.h:41
U16BIT mlt
Definition: mg_ctxt.h:73
const char * data
Definition: mh5gate.c:56
#define HD_Y_SCALEUP(y)
Definition: mg_ctxt.h:106
Debug tracing.
void DEC_FreePng(S_IMAGE *image)
Free memory allocated for PNG bitmap.
Definition: mg_png.c:637
U8BIT * col_buff
Definition: decoder.h:63
U32BIT OSDColor
Definition: osdtypes.h:41
#define COLOUR_FORMAT_ARGB4444
Definition: mheg5_control.h:80
U16BIT out_osd_height
Definition: mg_ctxt.h:82
S_RATIO osd_y
Definition: mg_ctxt.h:86
void * graphic_data
Definition: mh5visible.h:64
#define SRF_TYPE_JPG
Definition: mg_osd.h:34
U32BIT buff_pitch
Definition: mg_osd.h:43
U16BIT y_resolution
Definition: decoder.h:60
uint8_t U8BIT
Definition: techtype.h:82
S_IMAGE * DEC_DecodeJpg(U8BIT *data, U32BIT size)
Allocate memory for JPEG and decode bitmap To free "S_IMAGE" memory, DEC_FreeJpg will be called...
Definition: mg_jpg.c:324
void * hw_handle
Definition: mg_osd.h:42
Memory functions.
U8BIT opaque
Definition: decoder.h:62
S_MGContext mg_ctxt
Definition: mg_osd.c:144
void MG_DisplayImage(S_SURFACE *data, S_REGION *overlap, S32BIT x, S32BIT y)
Copy the image to the screen buffer.
Definition: mg_osd.c:1266
U16BIT height
Definition: mg_osd.h:40
Interface to OSD.
U16BIT height
Definition: decoder.h:59
#define ELSE
Definition: mg_ctxt.h:66
#define OSD_DbgAddSurf(s)
Definition: mg_osd.h:90
int len
Definition: mh5gate.c:57
U8BIT opaque
Definition: mg_osd.h:44
Module Description: Implement the MHEG5 Bitmap Class. 32 Bitmap Class Defines the behaviour of a two-...
void MG_DrawJpg(void *data, int len, MHEG5Visible *visible)
Definition: mg_bmp.c:236
U16BIT width
Definition: decoder.h:58
#define SRF_TYPE_PNG
Definition: mg_osd.h:33
The functions in this file are OPTIONALLY provided by Receiver Platform *.
U16BIT width
Definition: mg_osd.h:39
S_IMAGE * DEC_DecodePng(U8BIT *data, U32BIT size)
Allocate memory for PNG and decode bitmap To free "S_IMAGE" memory, DEC_FreePng will be called...
Definition: mg_png.c:332
void MG_OSDdisplayRectangle(S_REGION *overlap, int x, int y, int width, int height, int lineWidth, OSDColor lineColour, OSDColor fillColour)
Draw rectangle.
Definition: mg_osd.c:566
MHEG5Ingredient ingredient
Definition: mh5visible.h:49
U8BIT srf_type
Definition: mg_osd.h:45
MHEG5Int id
Definition: mh5root.h:48
S16BIT left
Definition: osdtypes.h:45
#define COLOUR_FORMAT_PALETTE
Definition: mheg5_control.h:79
#define HD_X_SCALEUP(x)
Definition: mg_ctxt.h:104
U16BIT div
Definition: mg_ctxt.h:74
void * STB_OSDMhegLockBuffer(void *surface, U32BIT *pPitch)
Converts hardware surface handle returned by STB_OSDMhegCreateSurface() to buffer address that the en...
void * DEC_OSDCreateBmpSurf(U32BIT s_width, U32BIT s_height, U32BIT i_width, U32BIT i_height, S_IMAGE *image, U32BIT *pitch)
This creates a hardware surface of size using STB_OSDMhegCreateSurface() with s_width and s_height...
Definition: mg_bitmap.c:1397
#define BytesPerPixel(cf)
Definition: mg_ctxt.h:69
S_REGION rect
Definition: mh5visible.h:65
MHEG5Int position[2]
Definition: mh5visible.h:59
#define OSD_MemFree
Definition: glue_memory.h:97
S_RATIO osd_x
Definition: mg_ctxt.h:85
uint32_t U32BIT
Definition: techtype.h:86
void STB_OSDMhegUnlockBuffer(void *surface)
This function informs HW that MHEG5 is finished writing to the buffer.
void MG_OSDdisplayBitmap(MHEG5Visible *visible, S_REGION *overlap)
Definition: mg_bmp.c:298
#define TRACE(t, x)
Definition: glue_debug.h:118