MythTV  master
Bitmap.cpp
Go to the documentation of this file.
1 /* Bitmap.cpp
2 
3  Copyright (C) David C. J. Matthews 2004, 2008 dm at prolingua.co.uk
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  as published by the Free Software Foundation; either version 2
8  of the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18  Or, point your browser to http://www.gnu.org/copyleft/gpl.html
19 
20 */
21 
22 #include "Bitmap.h"
23 #include "Visible.h"
24 #include "Presentable.h"
25 #include "Ingredients.h"
26 #include "Root.h"
27 #include "BaseClasses.h"
28 #include "ParseNode.h"
29 #include "ASN1Codes.h"
30 #include "Engine.h"
31 #include "Logging.h"
32 #include "freemheg.h"
33 
34 #include <algorithm>
35 #include <cinttypes>
36 
37 /*
38 UK MHEG content hook values: 2 => MPEG I-frame, 4 => PNG bitmap
39 */
40 
42 {
43  delete(m_pContent);
44 }
45 
47 {
48  MHVisible::Initialise(p, engine);
49  // Tiling - optional
50  MHParseNode *pTiling = p->GetNamedArg(C_TILING);
51 
52  if (pTiling)
53  {
54  m_fTiling = pTiling->GetArgN(0)->GetBoolValue();
55  }
56 
57  // Transparency - optional
58  MHParseNode *pTransparency = p->GetNamedArg(C_ORIGINAL_TRANSPARENCY);
59 
60  if (pTransparency)
61  {
62  m_nOrigTransparency = pTransparency->GetArgN(0)->GetIntValue();
63  }
64 
66 }
67 
68 void MHBitmap::PrintMe(FILE *fd, int nTabs) const
69 {
70  PrintTabs(fd, nTabs);
71  fprintf(fd, "{:Bitmap ");
72  MHVisible::PrintMe(fd, nTabs + 1);
73 
74  if (m_fTiling)
75  {
76  PrintTabs(fd, nTabs + 1);
77  fprintf(fd, ":Tiling true\n");
78  }
79 
80  if (m_nOrigTransparency != 0)
81  {
82  PrintTabs(fd, nTabs + 1);
83  fprintf(fd, ":OrigTransparency %d\n", m_nOrigTransparency);
84  }
85 
86  PrintTabs(fd, nTabs);
87  fprintf(fd, "}\n");
88 }
89 
91 {
92  if (m_fAvailable)
93  {
94  return;
95  }
96 
97  m_nTransparency = m_nOrigTransparency; // Transparency isn't used in UK MHEG
98  MHVisible::Preparation(engine);
99 }
100 
101 //
103 {
105 
106  if (m_contentType == IN_NoContent)
107  {
108  MHERROR("Bitmap must contain a content");
109  }
110 
111  if (m_contentType == IN_IncludedContent)
113 }
114 
115 // Decode the content.
116 void MHBitmap::ContentArrived(const unsigned char *data, int length, MHEngine *engine)
117 {
118  if (! m_pContent)
119  {
120  return; // Shouldn't happen.
121  }
122 
123  CreateContent(data, length, engine);
124  // Now signal that the content is available.
125  engine->EventTriggered(this, EventContentAvailable);
126 }
127 
128 void MHBitmap::CreateContent(const unsigned char *data, int length, MHEngine *engine)
129 {
130  QRegion updateArea = GetVisibleArea(); // If there's any content already we have to redraw it.
131 
132  int nCHook = m_nContentHook;
133 
134  if (nCHook == 0)
135  {
136  nCHook = engine->GetDefaultBitmapCHook();
137  }
138 
139  switch (nCHook)
140  {
141  case 4: // PNG.
142  m_pContent->CreateFromPNG(data, length);
143  break;
144  case 2: // MPEG I-frame.
145  case 5: // BBC/Freesat MPEG I-frame background
146  m_pContent->CreateFromMPEG(data, length);
147  break;
148  case 6: // JPEG ISO/IEC 10918-1, JFIF file
149  m_pContent->CreateFromJPEG(data, length);
150  break;
151  default: // 1,3,5,8 are reserved. 7= H.264 Intra Frame
152  MHERROR(QString("Unknown bitmap content hook %1").arg(nCHook));
153  }
154 
155  updateArea += GetVisibleArea(); // Redraw this bitmap.
156  engine->Redraw(updateArea); // Mark for redrawing
157 }
158 
159 
160 // Set the transparency.
161 void MHBitmap::SetTransparency(int nTransPerCent, MHEngine * /*engine*/)
162 {
163  // The object transparency isn't actually used in UK MHEG.
164  // We want a value between 0 and 255
165  nTransPerCent = std::clamp(nTransPerCent, 0, 100);
166 
167  m_nTransparency = ((nTransPerCent * 255) + 50) / 100;
168 }
169 
170 // Scale a bitmap. In UK MHEG this is only used for MPEG I-frames, not PNG.
171 void MHBitmap::ScaleBitmap(int xScale, int yScale, MHEngine *engine)
172 {
173  QRegion updateArea = GetVisibleArea(); // If there's any content already we have to redraw it.
174  m_pContent->ScaleImage(xScale, yScale);
175  updateArea += GetVisibleArea(); // Redraw this bitmap.
176  engine->Redraw(updateArea); // Mark for redrawing
177 }
178 
179 // Added action in UK MHEG.
180 void MHBitmap::SetBitmapDecodeOffset(int newXOffset, int newYOffset, MHEngine *engine)
181 {
182  QRegion updateArea = GetVisibleArea(); // Redraw the area before the offset
183  m_nXDecodeOffset = newXOffset;
184  m_nYDecodeOffset = newYOffset;
185  updateArea += GetVisibleArea(); // Redraw this bitmap.
186  engine->Redraw(updateArea); // Mark for redrawing
187 }
188 
189 // Added action in UK MHEG.
191 {
194 }
195 
196 void MHBitmap::Display(MHEngine * /*engine*/)
197 {
198  if (! m_fRunning || ! m_pContent || m_nBoxWidth == 0 || m_nBoxHeight == 0)
199  {
200  return; // Can't draw zero sized boxes.
201  }
202 
205  m_nContentHook == 5); // 'under video' if BBC MPEG I-frame background
206 }
207 
208 // Return the region drawn by the bitmap.
210 {
211  if (! m_fRunning || m_pContent == nullptr)
212  {
213  return {};
214  }
215 
216  // The visible area is the intersection of the containing box with the, possibly offset,
217  // bitmap.
218  QSize imageSize = m_pContent->GetSize();
219  QRegion boxRegion = QRegion(m_nPosX, m_nPosY, m_nBoxWidth, m_nBoxHeight);
220  QRegion bitmapRegion = QRegion(m_nPosX + m_nXDecodeOffset, m_nPosY + m_nYDecodeOffset,
221  imageSize.width(), imageSize.height());
222  return boxRegion & bitmapRegion;
223 }
224 
225 // Return the area actually obscured by the bitmap.
227 {
228  // The area is empty unless the bitmap is opaque.
229  // and it's not a BBC MPEG I-frame background
230  if (! m_fRunning || m_nContentHook == 5 || m_pContent == nullptr || ! m_pContent->IsOpaque())
231  {
232  return {};
233  }
234  return GetVisibleArea();
235 }
236 
MHContext::CreateBitmap
virtual MHBitmapDisplay * CreateBitmap(bool tiled)=0
Presentable.h
MHRoot::m_fAvailable
bool m_fAvailable
Definition: Root.h:252
MHBitmap::Preparation
void Preparation(MHEngine *engine) override
Definition: Bitmap.cpp:90
MHEngine::GetContext
MHContext * GetContext()
Definition: Engine.h:154
ASN1Codes.h
MHEngine
Definition: Engine.h:72
MHParseNode::GetIntValue
int GetIntValue()
Definition: ParseNode.cpp:170
MHIngredient::IN_NoContent
enum MHIngredient::@10 IN_NoContent
MHBitmap::CreateContent
void CreateContent(const unsigned char *data, int length, MHEngine *engine)
Definition: Bitmap.cpp:128
Bitmap.h
MHOctetString::Bytes
const unsigned char * Bytes() const
Definition: BaseClasses.h:125
MHVisible::PrintMe
void PrintMe(FILE *fd, int nTabs) const override
Definition: Visible.cpp:90
MHIngredient::m_includedContent
MHOctetString m_includedContent
Definition: Ingredients.h:77
Visible.h
MHRoot::SetVariableValue
virtual void SetVariableValue(const MHUnion &)
Definition: Root.h:109
MHBitmap::ContentPreparation
void ContentPreparation(MHEngine *engine) override
Definition: Bitmap.cpp:102
EventContentAvailable
@ EventContentAvailable
Definition: Root.h:34
MHIngredient::ContentPreparation
void ContentPreparation(MHEngine *engine) override
Definition: Ingredients.cpp:182
MHBitmap::SetBitmapDecodeOffset
void SetBitmapDecodeOffset(int newXOffset, int newYOffset, MHEngine *engine) override
Definition: Bitmap.cpp:180
MHBitmap::m_nTransparency
int m_nTransparency
Definition: Bitmap.h:71
MHVisible::Preparation
void Preparation(MHEngine *engine) override
Definition: Visible.cpp:111
mythburn.FILE
int FILE
Definition: mythburn.py:139
MHVisible::m_nPosY
int m_nPosY
Definition: Visible.h:82
C_TILING
@ C_TILING
Definition: ASN1Codes.h:115
MHBitmapDisplay::Draw
virtual void Draw(int x, int y, QRect rect, bool tiled, bool bUnder)=0
MHRoot::m_fRunning
bool m_fRunning
Definition: Root.h:253
MHEngine::Redraw
void Redraw(const QRegion &region)
Definition: Engine.cpp:898
MHBitmap::ContentArrived
void ContentArrived(const unsigned char *data, int length, MHEngine *engine) override
Definition: Bitmap.cpp:116
MHEngine::EventTriggered
void EventTriggered(MHRoot *pSource, enum EventType ev)
Definition: Engine.h:94
MHEngine::GetDefaultBitmapCHook
int GetDefaultBitmapCHook()
Definition: Engine.cpp:1451
hardwareprofile.config.p
p
Definition: config.py:33
ParseNode.h
PrintTabs
void PrintTabs(FILE *fd, int n)
Definition: ParseNode.cpp:34
MHParseNode::GetArgN
MHParseNode * GetArgN(int n)
Definition: ParseNode.cpp:78
MHVisible::m_nBoxHeight
int m_nBoxHeight
Definition: Visible.h:80
MHBitmap::SetTransparency
void SetTransparency(int nTransPerCent, MHEngine *engine) override
Definition: Bitmap.cpp:161
MHBitmapDisplay::CreateFromJPEG
virtual void CreateFromJPEG(const unsigned char *data, int length)=0
MHVisible::m_nPosX
int m_nPosX
Definition: Visible.h:81
Engine.h
clamp
static eu8 clamp(eu8 value, eu8 low, eu8 high)
Definition: pxsup2dast.c:204
MHBitmap::Initialise
void Initialise(MHParseNode *p, MHEngine *engine) override
Definition: Bitmap.cpp:46
Ingredients.h
MHBitmap::ScaleBitmap
void ScaleBitmap(int xScale, int yScale, MHEngine *engine) override
Definition: Bitmap.cpp:171
MHBitmapDisplay::CreateFromMPEG
virtual void CreateFromMPEG(const unsigned char *data, int length)=0
MHBitmap::GetOpaqueArea
QRegion GetOpaqueArea() override
Definition: Bitmap.cpp:226
MHBitmapDisplay::GetSize
virtual QSize GetSize()=0
MHParseNode::GetBoolValue
bool GetBoolValue()
Definition: ParseNode.cpp:192
Root.h
MHOctetString::Size
int Size() const
Definition: BaseClasses.h:120
MHBitmapDisplay::IsOpaque
virtual bool IsOpaque()=0
MHBitmap::m_nYDecodeOffset
int m_nYDecodeOffset
Definition: Bitmap.h:74
BaseClasses.h
MHVisible::Initialise
void Initialise(MHParseNode *p, MHEngine *engine) override
Definition: Visible.cpp:56
MHBitmap::~MHBitmap
~MHBitmap() override
Definition: Bitmap.cpp:41
MHBitmap::m_nOrigTransparency
int m_nOrigTransparency
Definition: Bitmap.h:68
MHParseNode
Definition: ParseNode.h:38
MHIngredient::IN_IncludedContent
@ IN_IncludedContent
Definition: Ingredients.h:71
MHBitmapDisplay::CreateFromPNG
virtual void CreateFromPNG(const unsigned char *data, int length)=0
freemheg.h
MHERROR
#define MHERROR(__text)
Definition: Logging.h:42
Logging.h
MHVisible::m_nBoxWidth
int m_nBoxWidth
Definition: Visible.h:79
MHIngredient::m_nContentHook
int m_nContentHook
Definition: Ingredients.h:66
C_ORIGINAL_TRANSPARENCY
@ C_ORIGINAL_TRANSPARENCY
Definition: ASN1Codes.h:116
MHBitmap::Display
void Display(MHEngine *d) override
Definition: Bitmap.cpp:196
MHBitmap::PrintMe
void PrintMe(FILE *fd, int nTabs) const override
Definition: Bitmap.cpp:68
MHBitmap::GetBitmapDecodeOffset
void GetBitmapDecodeOffset(MHRoot *pXOffset, MHRoot *pYOffset) override
Definition: Bitmap.cpp:190
MHBitmap::GetVisibleArea
QRegion GetVisibleArea() override
Definition: Bitmap.cpp:209
MHBitmap::m_nXDecodeOffset
int m_nXDecodeOffset
Definition: Bitmap.h:73
MHBitmap::m_pContent
MHBitmapDisplay * m_pContent
Definition: Bitmap.h:76
MHBitmap::m_fTiling
bool m_fTiling
Definition: Bitmap.h:67
MHRoot
Definition: Root.h:44
MHBitmapDisplay::ScaleImage
virtual void ScaleImage(int newWidth, int newHeight)=0