File: CoverArt.cc

package info (click to toggle)
flactag 2.0.4-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,096 kB
  • ctags: 477
  • sloc: sh: 10,777; cpp: 3,283; makefile: 62; sed: 7
file content (283 lines) | stat: -rw-r--r-- 6,021 bytes parent folder | download | duplicates (3)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* --------------------------------------------------------------------------

   flactag -- A tagger for single album FLAC files with embedded CUE sheets
   						using data retrieved from the MusicBrainz service

   Copyright (C) 2006-2012 Andrew Hawkins

   This file is part of flactag.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
	 the Free Software Foundation, either version 3 of the License, or
	 (at your option) any later version.

   Flactag is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

     $Id$

----------------------------------------------------------------------------*/

#include "CoverArt.h"

#include "ErrorLog.h"

#include <setjmp.h>

#include "base64.h"

struct my_error_mgr {
  struct jpeg_error_mgr pub;    /* "public" fields */

  jmp_buf setjmp_buffer;        /* for return to caller */
};

typedef struct my_error_mgr * my_error_ptr;

/*
 * Here's the routine that will replace the standard error_exit method:
 */

METHODDEF(void)
my_output_message (j_common_ptr cinfo)
{
	  char buffer[JMSG_LENGTH_MAX];

	  /* Create the message */

	  (*cinfo->err->format_message) (cinfo, buffer);

	  CErrorLog::Log(buffer);
}

METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  my_error_ptr myerr = (my_error_ptr) cinfo->err;

  /* Always display the message. */
  /* We could postpone this until after returning, if we chose. */
  (*cinfo->err->output_message) (cinfo);

  /* Return control to the setjmp point */
  longjmp(myerr->setjmp_buffer, 1);
}

CCoverArt::CCoverArt(const unsigned char *Data, size_t Length)
:	m_Data(0),
	m_Length(0),
	m_Width(0),
	m_Height(0)
{
	SetArt(Data,Length,true);
}

CCoverArt::CCoverArt(const CCoverArt& Other)
:	m_Data(0),
	m_Length(0),
	m_Width(0),
	m_Height(0)
{
	if (this!=&Other)
		*this=Other;
}

CCoverArt::~CCoverArt()
{
	Free();
}

void CCoverArt::Free()
{
	if (m_Data)
		delete[] m_Data;

	m_Data=0;
	m_Length=0;
}

CCoverArt& CCoverArt::operator =(const CCoverArt& Other)
{
	m_Width=Other.m_Width;
	m_Height=Other.m_Height;
	SetArt(Other.m_Data,Other.m_Length,false);

	return *this;
}

bool CCoverArt::operator ==(const CCoverArt& Other) const
{
	bool RetVal=false;

	if (m_Length==0 && Other.m_Length==0)
		RetVal=true;
	else if (m_Length==Other.m_Length && 0==memcmp(m_Data,Other.m_Data,m_Length))
		RetVal=true;

	return RetVal;
}

bool CCoverArt::operator !=(const CCoverArt& Other) const
{
	return !(*this==Other);
}

CCoverArt::operator std::string() const
{
	return rfc822_binary(m_Data,m_Length);
}

CCoverArt::operator bool() const
{
	return m_Length!=0;
}

void CCoverArt::Clear()
{
	Free();
}

void CCoverArt::SetArt(const unsigned char *Data, size_t Length, bool RetrieveDimensions)
{
	Free();

	if (Data && Length)
	{
		m_Data=new unsigned char[Length];
		m_Length=Length;
		memcpy(m_Data,Data,Length);

		if (RetrieveDimensions)
			GetDimensions();
	}
}

unsigned char *CCoverArt::Data() const
{
	return m_Data;
}

size_t CCoverArt::Length() const
{
	return m_Length;
}


typedef struct
{
	struct jpeg_source_mgr pub; /* public fields */
	JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */
} my_source_mgr;

typedef my_source_mgr *my_src_ptr;

void CCoverArt::InitSource(j_decompress_ptr /*cinfo*/)
{
}


boolean CCoverArt::FillInputBuffer(j_decompress_ptr cinfo)
{
	my_src_ptr src = (my_src_ptr) cinfo->src;

	WARNMS(cinfo, JWRN_JPEG_EOF);

	src->eoi_buffer[0] = (JOCTET) 0xFF;
	src->eoi_buffer[1] = (JOCTET) JPEG_EOI;
	src->pub.next_input_byte = src->eoi_buffer;
	src->pub.bytes_in_buffer = 2;

	return true;
}

void CCoverArt::SkipInputData(j_decompress_ptr cinfo, long num_bytes)
{
	my_src_ptr src = (my_src_ptr) cinfo->src;

	if (num_bytes > 0)
	{
		while (num_bytes > (long) src->pub.bytes_in_buffer)
		{
			num_bytes -= (long) src->pub.bytes_in_buffer;
			FillInputBuffer(cinfo);
		}

		src->pub.next_input_byte += (size_t) num_bytes;
		src->pub.bytes_in_buffer -= (size_t) num_bytes;
	}
}

void CCoverArt::TermSource(j_decompress_ptr /*cinfo*/)
{
}

void CCoverArt::JPEGMemorySource(j_decompress_ptr cinfo, const JOCTET * buffer, size_t bufsize)
{
	my_src_ptr src;

	if (cinfo->src == NULL)
	{
		cinfo->src = (struct jpeg_source_mgr *)(*cinfo->mem->alloc_small)
											((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_source_mgr));
	}

	src = (my_src_ptr) cinfo->src;
	src->pub.init_source = CCoverArt::InitSource;
	src->pub.fill_input_buffer = CCoverArt::FillInputBuffer;
	src->pub.skip_input_data = CCoverArt::SkipInputData;
	src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
	src->pub.term_source = CCoverArt::TermSource;

	src->pub.next_input_byte = buffer;
	src->pub.bytes_in_buffer = bufsize;
}

void CCoverArt::GetDimensions()
{
	if (!m_Width && !m_Height)
	{
		struct jpeg_decompress_struct cinfo;
		struct my_error_mgr jerr;

		cinfo.err = jpeg_std_error(&jerr.pub);
		jerr.pub.error_exit = my_error_exit;
		jerr.pub.output_message = my_output_message;

		if (setjmp(jerr.setjmp_buffer))
		{
			/* If we get here, the JPEG code has signaled an error.
			* We need to clean up the JPEG object, close the input file, and return.
			*/
			jpeg_destroy_decompress(&cinfo);
			return;
		}

		jpeg_create_decompress(&cinfo);
		JPEGMemorySource(&cinfo,m_Data,m_Length);
		jpeg_read_header(&cinfo, TRUE);

		m_Width=cinfo.image_width;
		m_Height=cinfo.image_height;

		jpeg_destroy_decompress(&cinfo);
	}
}

int CCoverArt::Width() const
{
	return m_Width;
}

int CCoverArt::Height() const
{
	return m_Height;
}