-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundAlphaTexture.cpp
executable file
·181 lines (144 loc) · 4.88 KB
/
BackgroundAlphaTexture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// BackgroundAlphaTexture.cpp: implementation of the CBackgroundAlphaTexture class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CdCoverCreator2.h"
#include "CdCoverCreator2Doc.h"
#include "BackgroundAlphaTexture.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL (CBackgroundAlphaTexture, CObject, VERSIONABLE_SCHEMA | 1)
CBackgroundAlphaTexture::CBackgroundAlphaTexture()
{
m_nAlphaFormat = 1;
m_nSourceConstantAlpha = 0x7F;
m_bImgFilenameChanged = false;
}
using namespace Gdiplus;
CBackgroundAlphaTexture::CBackgroundAlphaTexture (CString strImgFilename, int nAlphaFormat, int nSourceConstantAlpha)
{
m_strImgFilename = strImgFilename;
m_nAlphaFormat = nAlphaFormat;
m_nSourceConstantAlpha = nSourceConstantAlpha;
m_bImgFilenameChanged = false;
// load the image
BSTR bstrFilename = strImgFilename.AllocSysString ();
m_pBitmap = Bitmap::FromFile (bstrFilename);
::SysFreeString (bstrFilename);
}
CBackgroundAlphaTexture::~CBackgroundAlphaTexture()
{
}
bool CBackgroundAlphaTexture::operator== (CBackgroundAlphaTexture& bi)
{
if (m_strImgFilename != bi.GetImageFilename ())
return false;
if (m_nAlphaFormat != bi.m_nAlphaFormat)
return false;
if (m_nSourceConstantAlpha != bi.m_nSourceConstantAlpha)
return false;
return true;
}
bool CBackgroundAlphaTexture::operator!= (CBackgroundAlphaTexture& bi)
{
return !(*this == bi);
}
void CBackgroundAlphaTexture::Render (CDC* pDC, HANDLE hndPrinter, CRect r, bool bIsPrinting)
{
if (m_strImgFilename.IsEmpty ())
return;
if (m_pBitmap == NULL)
return;
Graphics graphics (pDC->GetSafeHdc (), hndPrinter);
graphics.SetPageUnit (UnitPixel);
/* TEXTUREBRUSH DOES NOT PRINT CORRECTLY :-( */
// create the color matrix
ColorMatrix cm;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
cm.m[i][j] = i == j ? 1.0 : 0.0; // identity matrix
cm.m[3][0] = cm.m[3][1] = cm.m[3][2] = (double) m_nAlphaFormat / 3;
cm.m[3][3] = (double) m_nSourceConstantAlpha / 0xff;
// create a texture brush
ImageAttributes atts;
atts.SetColorMatrix (&cm);
Rect rctDest (0, 0, m_pBitmap->GetWidth (), m_pBitmap->GetHeight ());
TextureBrush brush (m_pBitmap, rctDest, &atts);
brush.SetWrapMode (WrapModeTile);
//TextureBrush brush (m_pBitmap, WrapModeTile, rctDest);
graphics.FillRectangle (&brush, r.left, r.top, r.Width (), r.Height ());
/*
// set the alpha values of the image
int nMaxX = m_pBitmap->GetWidth ();
int nMaxY = m_pBitmap->GetHeight ();
Color pixel;
for (int x = 0; x < nMaxX; x++)
for (int y = 0; y < nMaxY; y++)
{
m_pBitmap->GetPixel (x, y, &pixel);
m_pBitmap->SetPixel (x, y, Color (m_nSourceConstantAlpha, pixel.GetRed (), pixel.GetGreen (), pixel.GetBlue ()));
}
graphics.DrawImage (m_pBitmap, r.left, r.top, 0, 0, nMaxX, nMaxY, UnitPixel);
*/
// etc. etc.
// Doesn't work with printing either: no transparency, the texture will be opaque
// Forget it...
}
void CBackgroundAlphaTexture::Serialize (CArchive& ar)
{
if (ar.IsStoring ())
{
ar << m_strImgFilename;
if (!m_strImgFilename.IsEmpty () && !((CCdCoverCreator2Doc*) ar.m_pDocument)->IsSerializingFromCUndo ())
{
ar << m_nAlphaFormat << m_nSourceConstantAlpha;
CFile f (m_strImgFilename, CFile::modeRead | CFile::shareDenyWrite);
DWORD dwCount = f.GetLength ();
BYTE* pBuf = new BYTE[f.GetLength ()];
f.Read (pBuf, dwCount);
ar << dwCount;
ar.Write (pBuf, dwCount);
delete pBuf;
}
}
else
{
if (((CCdCoverCreator2Doc*) ar.m_pDocument)->m_nObjectSchema >= 4)
{
// Alpha texture
ar >> m_strImgFilename;
if (!m_strImgFilename.IsEmpty () && !((CCdCoverCreator2Doc*) ar.m_pDocument)->IsSerializingFromCUndo ())
{
ar >> m_nAlphaFormat >> m_nSourceConstantAlpha;
DWORD dwLen;
ar >> dwLen;
BYTE* pBuf = new BYTE[dwLen];
ar.Read (pBuf, dwLen);
OFSTRUCT of;
if (::OpenFile (m_strImgFilename, &of, OF_EXIST) == HFILE_ERROR)
{
// image file does not exist.
// read from file and create image file
CString strPath = ar.GetFile ()->GetFilePath ();
m_strImgFilename = strPath.Left (strPath.ReverseFind ('\\')) +
m_strImgFilename.Mid (m_strImgFilename.ReverseFind ('\\'));
CFile f (m_strImgFilename, CFile::modeCreate | CFile::modeWrite);
f.Write (pBuf, dwLen);
f.Close ();
m_bImgFilenameChanged = true;
}
delete pBuf;
// load the image
BSTR bstrFilename = m_strImgFilename.AllocSysString ();
m_pBitmap = Bitmap::FromFile (bstrFilename);
::SysFreeString (bstrFilename);
}
}
}
}