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 <cinttypes>
35 
36 /*
37 UK MHEG content hook values: 2 => MPEG I-frame, 4 => PNG bitmap
38 */
39 
41 {
42  delete(m_pContent);
43 }
44 
46 {
47  MHVisible::Initialise(p, engine);
48  // Tiling - optional
49  MHParseNode *pTiling = p->GetNamedArg(C_TILING);
50 
51  if (pTiling)
52  {
53  m_fTiling = pTiling->GetArgN(0)->GetBoolValue();
54  }
55 
56  // Transparency - optional
57  MHParseNode *pTransparency = p->GetNamedArg(C_ORIGINAL_TRANSPARENCY);
58 
59  if (pTransparency)
60  {
61  m_nOrigTransparency = pTransparency->GetArgN(0)->GetIntValue();
62  }
63 
65 }
66 
67 void MHBitmap::PrintMe(FILE *fd, int nTabs) const
68 {
69  PrintTabs(fd, nTabs);
70  fprintf(fd, "{:Bitmap ");
71  MHVisible::PrintMe(fd, nTabs + 1);
72 
73  if (m_fTiling)
74  {
75  PrintTabs(fd, nTabs + 1);
76  fprintf(fd, ":Tiling true\n");
77  }
78 
79  if (m_nOrigTransparency != 0)
80  {
81  PrintTabs(fd, nTabs + 1);
82  fprintf(fd, ":OrigTransparency %d\n", m_nOrigTransparency);
83  }
84 
85  PrintTabs(fd, nTabs);
86  fprintf(fd, "}\n");
87 }
88 
90 {
91  if (m_fAvailable)
92  {
93  return;
94  }
95 
96  m_nTransparency = m_nOrigTransparency; // Transparency isn't used in UK MHEG
97  MHVisible::Preparation(engine);
98 }
99 
100 //
102 {
104 
105  if (m_contentType == IN_NoContent)
106  {
107  MHERROR("Bitmap must contain a content");
108  }
109 
110  if (m_contentType == IN_IncludedContent)
112 }
113 
114 // Decode the content.
115 void MHBitmap::ContentArrived(const unsigned char *data, int length, MHEngine *engine)
116 {
117  if (! m_pContent)
118  {
119  return; // Shouldn't happen.
120  }
121 
122  CreateContent(data, length, engine);
123  // Now signal that the content is available.
124  engine->EventTriggered(this, EventContentAvailable);
125 }
126 
127 void MHBitmap::CreateContent(const unsigned char *data, int length, MHEngine *engine)
128 {
129  QRegion updateArea = GetVisibleArea(); // If there's any content already we have to redraw it.
130 
131  int nCHook = m_nContentHook;
132 
133  if (nCHook == 0)
134  {
135  nCHook = engine->GetDefaultBitmapCHook();
136  }
137 
138  switch (nCHook)
139  {
140  case 4: // PNG.
141  m_pContent->CreateFromPNG(data, length);
142  break;
143  case 2: // MPEG I-frame.
144  case 5: // BBC/Freesat MPEG I-frame background
145  m_pContent->CreateFromMPEG(data, length);
146  break;
147  case 6: // JPEG ISO/IEC 10918-1, JFIF file
148  m_pContent->CreateFromJPEG(data, length);
149  break;
150  default: // 1,3,5,8 are reserved. 7= H.264 Intra Frame
151  MHERROR(QString("Unknown bitmap content hook %1").arg(nCHook));
152  }
153 
154  updateArea += GetVisibleArea(); // Redraw this bitmap.
155  engine->Redraw(updateArea); // Mark for redrawing
156 }
157 
158 
159 // Set the transparency.
160 void MHBitmap::SetTransparency(int nTransPerCent, MHEngine * /*engine*/)
161 {
162  // The object transparency isn't actually used in UK MHEG.
163  // We want a value between 0 and 255
164  if (nTransPerCent < 0)
165  {
166  nTransPerCent = 0; // Make sure it's in the bounds
167  }
168 
169  if (nTransPerCent > 100)
170  {
171  nTransPerCent = 100;
172  }
173 
174  m_nTransparency = ((nTransPerCent * 255) + 50) / 100;
175 }
176 
177 // Scale a bitmap. In UK MHEG this is only used for MPEG I-frames, not PNG.
178 void MHBitmap::ScaleBitmap(int xScale, int yScale, MHEngine *engine)
179 {
180  QRegion updateArea = GetVisibleArea(); // If there's any content already we have to redraw it.
181  m_pContent->ScaleImage(xScale, yScale);
182  updateArea += GetVisibleArea(); // Redraw this bitmap.
183  engine->Redraw(updateArea); // Mark for redrawing
184 }
185 
186 // Added action in UK MHEG.
187 void MHBitmap::SetBitmapDecodeOffset(int newXOffset, int newYOffset, MHEngine *engine)
188 {
189  QRegion updateArea = GetVisibleArea(); // Redraw the area before the offset
190  m_nXDecodeOffset = newXOffset;
191  m_nYDecodeOffset = newYOffset;
192  updateArea += GetVisibleArea(); // Redraw this bitmap.
193  engine->Redraw(updateArea); // Mark for redrawing
194 }
195 
196 // Added action in UK MHEG.
198 {
201 }
202 
203 void MHBitmap::Display(MHEngine * /*engine*/)
204 {
205  if (! m_fRunning || ! m_pContent || m_nBoxWidth == 0 || m_nBoxHeight == 0)
206  {
207  return; // Can't draw zero sized boxes.
208  }
209 
212  m_nContentHook == 5); // 'under video' if BBC MPEG I-frame background
213 }
214 
215 // Return the region drawn by the bitmap.
217 {
218  if (! m_fRunning || m_pContent == nullptr)
219  {
220  return {};
221  }
222 
223  // The visible area is the intersection of the containing box with the, possibly offset,
224  // bitmap.
225  QSize imageSize = m_pContent->GetSize();
226  QRegion boxRegion = QRegion(m_nPosX, m_nPosY, m_nBoxWidth, m_nBoxHeight);
227  QRegion bitmapRegion = QRegion(m_nPosX + m_nXDecodeOffset, m_nPosY + m_nYDecodeOffset,
228  imageSize.width(), imageSize.height());
229  return boxRegion & bitmapRegion;
230 }
231 
232 // Return the area actually obscured by the bitmap.
234 {
235  // The area is empty unless the bitmap is opaque.
236  // and it's not a BBC MPEG I-frame background
237  if (! m_fRunning || m_nContentHook == 5 || m_pContent == nullptr || ! m_pContent->IsOpaque())
238  {
239  return {};
240  }
241  return GetVisibleArea();
242 }
243 
MHContext::CreateBitmap
virtual MHBitmapDisplay * CreateBitmap(bool tiled)=0
C_TILING
@ C_TILING
Definition: ASN1Codes.h:115
Presentable.h
EventContentAvailable
@ EventContentAvailable
Definition: Root.h:33
MHRoot::m_fAvailable
bool m_fAvailable
Definition: Root.h:251
MHBitmap::Preparation
void Preparation(MHEngine *engine) override
Definition: Bitmap.cpp:89
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:127
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:84
MHIngredient::m_includedContent
MHOctetString m_includedContent
Definition: Ingredients.h:77
Visible.h
MHRoot::SetVariableValue
virtual void SetVariableValue(const MHUnion &)
Definition: Root.h:108
MHBitmap::ContentPreparation
void ContentPreparation(MHEngine *engine) override
Definition: Bitmap.cpp:101
MHIngredient::ContentPreparation
void ContentPreparation(MHEngine *engine) override
Definition: Ingredients.cpp:181
MHBitmap::SetBitmapDecodeOffset
void SetBitmapDecodeOffset(int newXOffset, int newYOffset, MHEngine *engine) override
Definition: Bitmap.cpp:187
MHBitmap::m_nTransparency
int m_nTransparency
Definition: Bitmap.h:71
MHVisible::Preparation
void Preparation(MHEngine *engine) override
Definition: Visible.cpp:105
mythburn.FILE
int FILE
Definition: mythburn.py:139
MHVisible::m_nPosY
int m_nPosY
Definition: Visible.h:82
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:252
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:115
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:160
MHBitmapDisplay::CreateFromJPEG
virtual void CreateFromJPEG(const unsigned char *data, int length)=0
MHVisible::m_nPosX
int m_nPosX
Definition: Visible.h:81
Engine.h
MHBitmap::Initialise
void Initialise(MHParseNode *p, MHEngine *engine) override
Definition: Bitmap.cpp:45
Ingredients.h
MHBitmap::ScaleBitmap
void ScaleBitmap(int xScale, int yScale, MHEngine *engine) override
Definition: Bitmap.cpp:178
MHBitmapDisplay::CreateFromMPEG
virtual void CreateFromMPEG(const unsigned char *data, int length)=0
MHBitmap::GetOpaqueArea
QRegion GetOpaqueArea() override
Definition: Bitmap.cpp:233
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:50
MHBitmap::~MHBitmap
~MHBitmap() override
Definition: Bitmap.cpp:40
MHBitmap::m_nOrigTransparency
int m_nOrigTransparency
Definition: Bitmap.h:68
MHParseNode
Definition: ParseNode.h:38
MHBitmapDisplay::CreateFromPNG
virtual void CreateFromPNG(const unsigned char *data, int length)=0
C_ORIGINAL_TRANSPARENCY
@ C_ORIGINAL_TRANSPARENCY
Definition: ASN1Codes.h:116
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
MHBitmap::Display
void Display(MHEngine *d) override
Definition: Bitmap.cpp:203
MHBitmap::PrintMe
void PrintMe(FILE *fd, int nTabs) const override
Definition: Bitmap.cpp:67
MHBitmap::GetBitmapDecodeOffset
void GetBitmapDecodeOffset(MHRoot *pXOffset, MHRoot *pYOffset) override
Definition: Bitmap.cpp:197
MHBitmap::GetVisibleArea
QRegion GetVisibleArea() override
Definition: Bitmap.cpp:216
MHBitmap::m_nXDecodeOffset
int m_nXDecodeOffset
Definition: Bitmap.h:73
MHBitmap::m_pContent
MHBitmapDisplay * m_pContent
Definition: Bitmap.h:76
MHIngredient::IN_IncludedContent
@ IN_IncludedContent
Definition: Ingredients.h:71
MHBitmap::m_fTiling
bool m_fTiling
Definition: Bitmap.h:67
MHRoot
Definition: Root.h:43
MHBitmapDisplay::ScaleImage
virtual void ScaleImage(int newWidth, int newHeight)=0