Example #1
0
std::wstring utf8string::to_wstring() const {
	// Currently no check for validity
	iconv_t converter = iconv_open((wchar_t_encoding + ignore_tag).c_str(), utf8_encoding.c_str());
	size_t inSize = m_str.size() + 1;
	size_t outSize = (utf8_len(m_str.c_str()) + 1) * 2 * sizeof (wchar_t);
	std::vector<wchar_t> outBuf(outSize);
	char* outBufPtr = reinterpret_cast<char *>(data(outBuf));
	const char* inBufPtr = m_str.c_str();
	iconv(converter, &inBufPtr, &inSize, &outBufPtr, &outSize);
	iconv_close(converter);

	return std::wstring(outBuf.data()); // here goes additional copy
}
Example #2
0
utf8string::utf8string(const std::basic_string<CHAR>& strArg) {
	// Currently no check for validity
	std::string target_enc = utf8_encoding + ignore_tag;
	std::string source_enc = get_encoding<CHAR>().c_str();
	iconv_t converter = iconv_open(target_enc.c_str(), source_enc.c_str());
	size_t inSize = (strArg.length() + 1) * sizeof (CHAR);
	size_t outSize = 6 * strArg.length() + 1; // Maximum Possible UTF-8 size
	std::vector<char> outBuf(outSize);
	char* outBufPtr = outBuf.data();
	const char* inBufPtr = reinterpret_cast<const char *>(strArg.data());
	iconv(converter, &inBufPtr, &inSize, &outBufPtr, &outSize);
	iconv_close(converter);

	m_str.assign(outBuf.data());
}
Example #3
0
std::unique_ptr< OpenImageIO::ImageBuf >
//FIXME remove the 2 once all is said and done
TileCache::getTile(const uint32_t n)
{
    std::unique_ptr< OpenImageIO::ImageBuf >
        outBuf( new OpenImageIO::ImageBuf );

    // returning an unitialized imagebuf is not a good idea
    CHECK( n < nTiles ) << "getTile( " << n << ") request out of range 0-" << nTiles ;

    SMT *smt = nullptr;
    static SMT *lastSmt = nullptr;

    // FIXME, what the f**k does this do?
    auto i = map.begin();
    auto fileName = fileNames.begin();
    while( *i <= n ){
        ++i;
        ++fileName;
    }

    // already open smt file?
    if( lastSmt && (! lastSmt->fileName.compare( *fileName )) ){
        outBuf = lastSmt->getTile( n - *i + lastSmt->nTiles);
    }
    // open a new smt file?
    else if  ( (smt = SMT::open( *fileName )) ){
        //FIXME shouldnt have a manual delete here, use move scemantics instead
        delete lastSmt;
        lastSmt = smt;
        outBuf = lastSmt->getTile( n - *i + lastSmt->nTiles );
    }
    // open the image file?
    else {
        outBuf->reset( *fileName );
    }
    CHECK( outBuf->initialized() ) << "failed to open source for tile: " << n;

#ifdef DEBUG_IMG
    DLOG( INFO ) << "Exporting Image";
    outBuf->write( "TileCache.getTile.tif" , "tif" );
#endif

    return outBuf;
}
Example #4
0
	shared_ptr <Buffer> FuseService::GetVolumeInfo ()
	{
		shared_ptr <Stream> stream (new MemoryStream);

		{
			ScopeLock lock (OpenVolumeInfoMutex);

			OpenVolumeInfo.Set (*MountedVolume);
			OpenVolumeInfo.SlotNumber = SlotNumber;

			OpenVolumeInfo.Serialize (stream);
		}

		ConstBufferPtr infoBuf = dynamic_cast <MemoryStream&> (*stream);
		shared_ptr <Buffer> outBuf (new Buffer (infoBuf.Size()));
		outBuf->CopyFrom (infoBuf);

		return outBuf;
	}
Example #5
0
QByteArray EncryptWrapper::encryptData(){
	int outLen, inLen, finalLen;
	inLen = enData.count();
	QByteArray outBuf(inLen+EVP_MAX_BLOCK_LENGTH,0);

	if(!EVP_EncryptUpdate(&encrypt, (unsigned char *)outBuf.data(), &outLen, (unsigned char *)enData.constData(), inLen)){
		emit errors("EVP_EncryptUpdate failed");
		return QByteArray();
	}

	if(!EVP_EncryptFinal(&encrypt, ((unsigned char*)outBuf.data())+outLen,&finalLen)){
		emit errors("EVP_EncryptFinal failed");
		return QByteArray();
	}

	outLen +=finalLen;
	EVP_CIPHER_CTX_cleanup(&encrypt);
	EVP_cleanup();
	outBuf.resize(outLen);
	return outBuf;
}
Example #6
0
// TGA Function Definitions
void WriteImageTGA(const std::string &name, const uint8_t *pixels, int xRes,
                   int yRes, int totalXRes, int totalYRes, int xOffset,
                   int yOffset) {
    // Reformat to BGR layout.
    std::unique_ptr<uint8_t> outBuf(new uint8_t[3 * xRes * yRes]);
    uint8_t *dst = outBuf.get();
    const uint8_t *src = pixels;
    for (int y = 0; y < yRes; ++y) {
        for (int x = 0; x < xRes; ++x) {
            dst[0] = src[2];
            dst[1] = src[1];
            dst[2] = src[0];
            dst += 3;
            src += 3;
        }
    }

    tga_result result;
    if ((result = tga_write_bgr(name.c_str(), outBuf.get(), xRes, yRes, 24)) !=
        TGA_NOERR)
        Error("Unable to write output file \"%s\" (%s)", name.c_str(),
              tga_error(result));
}
void ResamplingUnitTest::runTest()
{
    juce::ScopedPointer<IResampler> pResamp;

	// Basic Upsamplers
    {
		Buffer inBuf0(4);
		Buffer inBuf1(4);
        Buffer outBuf(16);
        
        float const inValsUp0[4] = {1.0f, -2.3f, -0.0f, 0.3f};
        float const inValsUp1[4] = {0.1f, 3.2f, -600.0f, 2.1f};

		juce::FloatVectorOperations::copy(inBuf0.Get(), inValsUp0, 4);
		juce::FloatVectorOperations::copy(inBuf1.Get(), inValsUp1, 4);

        beginTest("Testing ZeroPadUpsampler");
        {
            float expVals0[16] = { 1.0f, 0.0f, 0.0f, 0.0f, -2.3f, 0.0f, 0.0f, 0.0f,   -0.0f, 0.0f, 0.0f, 0.0f, 0.3f, 0.0f, 0.0f, 0.0f };
			float expVals1[16] = { 0.1f, 0.0f, 0.0f, 0.0f, 3.2f,  0.0f, 0.0f, 0.0f, -600.0f, 0.0f, 0.0f, 0.0f, 2.1f, 0.0f, 0.0f, 0.0f };
            
			pResamp = new Utils::ZeroPadUpsampler(4, false);
            pResamp->Process(outBuf, inBuf0);
			expect(CheckEquals_(outBuf, Buffer(expVals0, 16)));
            
			pResamp = new Utils::ZeroPadUpsampler(4, false);
			pResamp->Process(outBuf, inBuf1);
			expect(CheckEquals_(outBuf, Buffer(expVals1, 16)));

			juce::FloatVectorOperations::multiply(expVals0, 4.0f, 16);
			juce::FloatVectorOperations::multiply(expVals1, 4.0f, 16);

            pResamp = new Utils::ZeroPadUpsampler(4, true);
			pResamp->Process(outBuf, inBuf0);
			expect(CheckEquals_(outBuf, Buffer(expVals0, 16)));

			pResamp = new Utils::ZeroPadUpsampler(4, true);
			pResamp->Process(outBuf, inBuf1);
			expect(CheckEquals_(outBuf, Buffer(expVals1, 16)));
        }
        
        beginTest("Testing NearestUpsampler");
        {
			float expVals0[16] = { 1.0f, 1.0f, 1.0f, 1.0f, -2.3f, -2.3f, -2.3f, -2.3f,   -0.0f, -0.0f, -0.0f, -0.0f, 0.3f, 0.3f, 0.3f, 0.3f };
			float expVals1[16] = { 0.1f, 0.1f, 0.1f, 0.1f,  3.2f,  3.2f,  3.2f,  3.2f, -600.0f, -600.0f, -600.0f, -600.0f, 2.1f, 2.1f, 2.1f, 2.1f };

            pResamp = new Utils::NearestUpsampler(4);
			pResamp->Process(outBuf, inBuf0);
			expect(CheckEquals_(outBuf, Buffer(expVals0, 16)));

			pResamp = new Utils::NearestUpsampler(4);
			pResamp->Process(outBuf, inBuf1);
			expect(CheckEquals_(outBuf, Buffer(expVals1, 16)));
        }
        
        beginTest("Testing LinearInterpUpsampler");
        {
            {
				float expVals0[16] = { 0.0f, 0.25f, 0.50f, 0.75f, 1.0f, 0.175f, -0.65f, -1.475f, -2.3f, -1.725f, -1.15f, -0.575f, 0.0f, 0.075f, 0.15f, 0.225f };
				float expVals1[16] = { 0.0f, 0.025f, 0.05f, 0.075f, 0.1f, 0.875f, 1.65f, 2.425f, 3.2f, -147.6f, -298.4f, -449.2f, -600.0f, -449.475f, -298.95f, -148.425f };

                pResamp = new Utils::LinearInterpUpsampler(4, 2, false);
				pResamp->Process(outBuf, inBuf0);
                expect(CheckEquals_(outBuf, Buffer(expVals0, 16)));
				
				pResamp = new Utils::LinearInterpUpsampler(4, 2, false);
				pResamp->Process(outBuf, inBuf1);
				expect(CheckEquals_(outBuf, Buffer(expVals1, 16)));
            }
        
            {
				float expVals0[16] = { 0.25f, 0.50f, 0.75f, 1.0f, 0.175f, -0.65f, -1.475f, -2.3f, -1.725f, -1.15f, -0.575f, 0.0f, 0.075f, 0.15f, 0.225f, 0.3f };
				float expVals1[16] = { 0.025f, 0.05f, 0.075f, 0.1f, 0.875f, 1.65f, 2.425f, 3.2f, -147.6f, -298.4f, -449.2f, -600.0f, -449.475f, -298.95f, -148.425f, 2.1f };

                pResamp = new Utils::LinearInterpUpsampler(4, 2, true);
				pResamp->Process(outBuf, inBuf0);
                expect(CheckEquals_(outBuf, Buffer(expVals0, 16)));

				pResamp = new Utils::LinearInterpUpsampler(4, 2, true);
				pResamp->Process(outBuf, inBuf1);
				expect(CheckEquals_(outBuf, Buffer(expVals1, 16)));
            }
        }
    }
    
	// Basic Downsamplers
    {
        Buffer inBuf0(16);
		Buffer inBuf1(16);
        
        float const inValsDown0[16] = {1.0f, -2.3f, -0.0f, 0.3f, 4.1f, -200.0f, 3.14f, 2.71f, -6.0f, 4.2f, 0.01f, -10.0f, -0.0f, 512.0f, -2.1f, 6.0f};
        float const inValsDown1[16] = {0.1f, 3.2f, -600.0f, 2.1f, 3.14f, -1.0f, 6.2f, -7.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, -1.5f, 4.01f, 2.2f};
        juce::FloatVectorOperations::copy(inBuf0.Get(), inValsDown0, 16);
        juce::FloatVectorOperations::copy(inBuf1.Get(), inValsDown1, 16);
        
        beginTest("Testing NaiveDownsampler");
        {
            Buffer outBuf(4);
        
            float expVals0[4] = {inValsDown0[0], inValsDown0[4], inValsDown0[8], inValsDown0[12]};
            float expVals1[4] = {inValsDown1[0], inValsDown1[4], inValsDown1[8], inValsDown1[12]};
            
            pResamp = new Utils::NaiveDownsampler(4);
			pResamp->Process(outBuf, inBuf0);
            expect(CheckEquals_(outBuf, Buffer(expVals0, 4)));

			pResamp = new Utils::NaiveDownsampler(4);
			pResamp->Process(outBuf, inBuf1);
			expect(CheckEquals_(outBuf, Buffer(expVals1, 4)));

            juce::FloatVectorOperations::multiply(expVals0, 4.0f, 4);
            juce::FloatVectorOperations::multiply(expVals1, 4.0f, 4);
            
            pResamp = new Utils::NaiveDownsampler(4, true);
			pResamp->Process(outBuf, inBuf0);
			expect(CheckEquals_(outBuf, Buffer(expVals0, 4)));

			pResamp = new Utils::NaiveDownsampler(4, true);
			pResamp->Process(outBuf, inBuf1);
			expect(CheckEquals_(outBuf, Buffer(expVals1, 4)));
        }
        
        beginTest("Testing AveragingDownsampler");
        {
            Buffer outBuf(4);
        
            #define AVG4(X, N) (X[N] + X[N+1] + X[N+2] + X[N+3]) / 4.0f
            #define AVG4x4(X) {AVG4(X,0), AVG4(X,4), AVG4(X,8), AVG4(X,12)}
        
            float expVals0[4] = AVG4x4(inValsDown0);
            float expVals1[4] = AVG4x4(inValsDown1);

            #undef AVG4
            #undef AVG4x4
            
            pResamp = new Utils::AveragingDownsampler(4);
			pResamp->Process(outBuf, inBuf0);
            expect(CheckEquals_(outBuf, Buffer(expVals0, 4)));
			
			pResamp = new Utils::AveragingDownsampler(4);
			pResamp->Process(outBuf, inBuf1);
			expect(CheckEquals_(outBuf, Buffer(expVals1, 4)));
        }
	}
	
	// IIR Downsamplers
	/*
	These tests fail. I'm not sure why.
	
	I suspect it's an error in measuring the sine amplitude, but it's also
	possible there's a problem with the filter itself - either something's
	wrong with the filter design, or it's a digital precision issue, or it's
	a good-ol' bug in the IIR downsamplers.
	
	I'm guessing it's not a precision issue, because if so, I suspect the
	high-order version would perform much worse than the cascaded-biquad
	version, and they actually do quite similarly.
	*/
	{
		size_t const nSamp = 65536;
		Buffer buf(nSamp);
		
		std::vector<float> const freqs = {
			100.f, 440.f, 1000.f, 3000.f, 10000.f, 20000.f, // Audible freqs
			22000.f, 24100.f, 28000.f,
			40e3f, 47e3f, 49e3f, 60e3f,
			68.2e3f, 76e3f, // Will alias to 20 kHz at 44.1 and 48, respectively
			80e3f, 88.1e3f, 90e3f, 95.9e3f
		};

		for (size_t n = 0; n < 4; ++n) {

			juce::String testName;
			float sampleRate;
			size_t nResamp;

			switch (n) {
			case 0:
				pResamp = new Utils::IirDownsampler_176_44();
				testName = "Testing IIR resampler 176.4 kHz -> 44.1 kHz";
				sampleRate = 176400.f;
				nResamp = 4;
				break;
			case 1:
				pResamp = new Utils::IirDownsampler_192_48();
				testName = "Testing IIR resampler 192 kHz -> 48 kHz";
				sampleRate = 192000.f;
				nResamp = 4;
				break;
			case 2:
				pResamp = new Utils::IirDownsampler_176_88();
				testName = "Testing IIR resampler 176.4 kHz -> 88.2 kHz";
				sampleRate = 176400.f;
				nResamp = 2;
				break;
			case 3:
				pResamp = new Utils::IirDownsampler_192_96();
				testName = "Testing IIR resampler 192 kHz -> 96 kHz";
				sampleRate = 192000.f;
				nResamp = 2;
				break;
			default:
				std::cout << "ERROR: n out of bounds\n";
				expect(false);
			}

			
			beginTest(testName);

			Buffer downBuf(nSamp / nResamp);

			std::vector<floatPair> results;

			sample_t phase = 0.f;
			for (auto it = freqs.begin(); it != freqs.end(); ++it) {

				float f = *it;

				float w = f / sampleRate;

				if (w > 0.5f) break;

				phase = GenerateSine(buf, w, phase);

				downBuf.Clear(); // Shouldn't be necessary?

				pResamp->Process(downBuf, buf);

				float ampl = Detail::GetSinAmpdB(downBuf, 1000);
				results.push_back(floatPair(f, ampl));
			}

			float const passbandFailThresh = 0.75f;
			float const aliasFailThresh = -60.0f;
			float maxPassbandError = 0.0f;
			float maxAlias = -99999.9f;

			for (auto it = results.begin(); it != results.end(); ++it) {
				float const inFreq = it->first;
				float const finalFreq = Detail::CalcAliasedFreq(inFreq, sampleRate/nResamp);
				bool const bAlias = !Utils::ApproxEqual(inFreq, finalFreq, 1.0f);
				
				float const ampl = it->second;

				if (inFreq <= 20001.f) {
					maxPassbandError = std::max(maxPassbandError, abs(ampl));
					std::cout << "Audible:    " << inFreq << ", ampl: " << ampl << " dB\n";
				}
				else if (finalFreq <= 20001.f) {
					maxAlias = std::max(maxAlias, ampl);
					std::cout << "Alias:      " << inFreq << " => " << finalFreq << ", ampl: " << ampl << " dB\n";
				}
				else {
					// Don't care about value, but print
					if (!bAlias) {
						std::cout << "Transition: " << inFreq << ", ampl: " << ampl << " dB\n";
					} 
					else {
						std::cout << "Transition: " << inFreq << " => " << finalFreq << ", ampl: " << ampl << " dB\n";
					}
				}
			}

			if (maxPassbandError > passbandFailThresh)
				std::cout << "Fail: max passband error " << maxPassbandError << " dB\n";

			expect(maxPassbandError <= passbandFailThresh);

			if (maxAlias > aliasFailThresh)
				std::cout << "Fail: max aliasing " << maxAlias << " dB\n";

			expect(maxAlias <= aliasFailThresh);
		}
    }
}
int WPSRegistrar::SendACK()
{
    BufferObj outBuf((unsigned char *) "WPS Message ACK", strlen("WPS Message ACK") + 1);
	SendWPSMessage(this, m_peer, m_callback, outBuf);
	return sent_ack;
}