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/*
38UK 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
68void 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
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.
116void 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.
126}
127
128void 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.
161void 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.
171void 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.
180void 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
196void 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
@ C_ORIGINAL_TRANSPARENCY
Definition: ASN1Codes.h:116
@ C_TILING
Definition: ASN1Codes.h:115
#define MHERROR(__text)
Definition: Logging.h:42
void PrintTabs(FILE *fd, int n)
Definition: ParseNode.cpp:34
@ EventContentAvailable
Definition: Root.h:34
virtual void Draw(int x, int y, QRect rect, bool tiled, bool bUnder)=0
virtual void CreateFromJPEG(const unsigned char *data, int length)=0
virtual QSize GetSize()=0
virtual void CreateFromMPEG(const unsigned char *data, int length)=0
virtual void ScaleImage(int newWidth, int newHeight)=0
virtual bool IsOpaque()=0
virtual void CreateFromPNG(const unsigned char *data, int length)=0
~MHBitmap() override
Definition: Bitmap.cpp:41
int m_nTransparency
Definition: Bitmap.h:71
void PrintMe(FILE *fd, int nTabs) const override
Definition: Bitmap.cpp:68
bool m_fTiling
Definition: Bitmap.h:67
int m_nXDecodeOffset
Definition: Bitmap.h:73
void SetTransparency(int nTransPerCent, MHEngine *engine) override
Definition: Bitmap.cpp:161
void GetBitmapDecodeOffset(MHRoot *pXOffset, MHRoot *pYOffset) override
Definition: Bitmap.cpp:190
void ContentArrived(const unsigned char *data, int length, MHEngine *engine) override
Definition: Bitmap.cpp:116
void Display(MHEngine *d) override
Definition: Bitmap.cpp:196
int m_nYDecodeOffset
Definition: Bitmap.h:74
void SetBitmapDecodeOffset(int newXOffset, int newYOffset, MHEngine *engine) override
Definition: Bitmap.cpp:180
void Initialise(MHParseNode *p, MHEngine *engine) override
Definition: Bitmap.cpp:46
QRegion GetOpaqueArea() override
Definition: Bitmap.cpp:226
QRegion GetVisibleArea() override
Definition: Bitmap.cpp:209
void ContentPreparation(MHEngine *engine) override
Definition: Bitmap.cpp:102
void ScaleBitmap(int xScale, int yScale, MHEngine *engine) override
Definition: Bitmap.cpp:171
void Preparation(MHEngine *engine) override
Definition: Bitmap.cpp:90
void CreateContent(const unsigned char *data, int length, MHEngine *engine)
Definition: Bitmap.cpp:128
int m_nOrigTransparency
Definition: Bitmap.h:68
MHBitmapDisplay * m_pContent
Definition: Bitmap.h:76
virtual MHBitmapDisplay * CreateBitmap(bool tiled)=0
void Redraw(const QRegion &region)
Definition: Engine.cpp:926
MHContext * GetContext()
Definition: Engine.h:154
void EventTriggered(MHRoot *pSource, enum EventType ev)
Definition: Engine.h:94
int GetDefaultBitmapCHook()
Definition: Engine.cpp:1494
void ContentPreparation(MHEngine *engine) override
enum MHIngredient::@4 IN_NoContent
MHOctetString m_includedContent
Definition: Ingredients.h:77
int m_nContentHook
Definition: Ingredients.h:66
@ IN_IncludedContent
Definition: Ingredients.h:71
const unsigned char * Bytes() const
Definition: BaseClasses.h:125
int Size() const
Definition: BaseClasses.h:120
MHParseNode * GetArgN(int n)
Definition: ParseNode.cpp:78
bool GetBoolValue()
Definition: ParseNode.cpp:192
int GetIntValue()
Definition: ParseNode.cpp:170
Definition: Root.h:45
bool m_fRunning
Definition: Root.h:253
bool m_fAvailable
Definition: Root.h:252
virtual void SetVariableValue(const MHUnion &)
Definition: Root.h:109
void PrintMe(FILE *fd, int nTabs) const override
Definition: Visible.cpp:90
int m_nBoxWidth
Definition: Visible.h:79
int m_nBoxHeight
Definition: Visible.h:80
void Initialise(MHParseNode *p, MHEngine *engine) override
Definition: Visible.cpp:56
int m_nPosY
Definition: Visible.h:82
void Preparation(MHEngine *engine) override
Definition: Visible.cpp:111
int m_nPosX
Definition: Visible.h:81
int FILE
Definition: mythburn.py:138
static eu8 clamp(eu8 value, eu8 low, eu8 high)
Definition: pxsup2dast.c:206