コード例 #1
0
ファイル: ImageFormat_JPEG.cpp プロジェクト: Siv3D/OpenSiv3D
	Image ImageFormat_JPEG::decode(IReader& reader) const
	{
		const int64 size = reader.size();
		
		uint8* buffer = static_cast<uint8*>(::malloc(static_cast<size_t>(size)));
		
		reader.read(buffer, size);

		int width, height;

		tjhandle tj = ::tjInitDecompress();

		::tjDecompressHeader(tj, buffer, static_cast<unsigned long>(size), &width, &height);

		Image image(width, height);

		::tjDecompress(
			tj,
			buffer,
			static_cast<unsigned long>(size),
			image.dataAsUint8(),
			image.width(),
			image.stride(),
			image.height(),
			sizeof(Color),
			0);

		::tjDestroy(tj);

		::free(buffer);

		return image;
	}
コード例 #2
0
ファイル: flash.cpp プロジェクト: AndriiDSD/pixy
void Flash::program(const QString &filename)
{
#if 0
    QFile file(filename);
    uint32_t len;
    uint32_t addr;
    int32_t response;

    if (!file.open(QIODevice::ReadOnly))
        throw std::runtime_error((QString("Cannot open file ") + filename + QString(".")).toStdString());

    for(addr=0x14000000; !file.atEnd(); addr+=len)
    {
        len =(uint32_t)file.read(m_buf, m_sectorSize);
        m_chirp.callSync(m_programProc, UINT32(addr), UINTS8(len, m_buf), END_OUT_ARGS,
                         &response, END_IN_ARGS);
        if (response==-1)
            throw std::runtime_error("Invalid address range.");
        else if (response==-3)
            throw std::runtime_error("Error during verify.");
        else if (response<0)
            throw std::runtime_error("Error during programming.");
    }

#else
    IReader *reader;
    unsigned long addr, len;
    int32_t res, response;

    reader = createReader(filename);
    while(1)
    {
        res = reader->read((unsigned char *)m_buf, m_sectorSize, &addr, &len);
        if (len)
        {
            if (m_chirp.callSync(m_programProc, UINT32(addr), UINTS8(len, m_buf), END_OUT_ARGS,
                             &response, END_IN_ARGS)<0)
                throw std::runtime_error("communication error during programming.");
            if (response==-1)
                throw std::runtime_error("invalid address range.");
            else if (response==-3)
                throw std::runtime_error("during verify.");
            else if (response<-100)
            {
                QString str = "I/O: " + QString::number(-response-100) + ".";
                throw std::runtime_error(str.toStdString());
            }
            else if (response<0)
            {
                QString str = "programming: " + QString::number(response) + ".";
                throw std::runtime_error(str.toStdString());
            }
        }
        if (res<0)
            break;
    }
#endif
    // reset Pixy
    if (m_chirp.callSync(m_reset, END_OUT_ARGS,
                         &response, END_IN_ARGS)<0)
        throw std::runtime_error("Unable to reset.");
    destroyReader(reader);
}
コード例 #3
0
ファイル: AudioFormat_WAVE.cpp プロジェクト: Siv3D/OpenSiv3D
	Wave AudioFormat_WAVE::decode(IReader& reader) const
	{
		RiffHeader riffHeader;

		if (!reader.read(riffHeader))
		{
			return Wave();
		}

		if (!detail::MemEqual(riffHeader.riff, detail::RIFF_SIGN) || !detail::MemEqual(riffHeader.type, detail::WAVE_SIGN))
		{
			return Wave();
		}

		ChunkHeader chunkHeader;

		for (;;)
		{
			if (!reader.read(chunkHeader))
			{
				return Wave();
			}

			if (detail::MemEqual(chunkHeader.chunkID, detail::FMT_CHUNK))
			{
				break;
			}
			else
			{
				reader.setPos(reader.getPos() + chunkHeader.chunkSize);
			}
		}

		FormatHeader formatHeader;

		if (!reader.read(formatHeader))
		{
			return Wave();
		}

		if (chunkHeader.chunkSize > sizeof(formatHeader))
		{
			reader.skip(chunkHeader.chunkSize - sizeof(formatHeader));
		}

		for (;;)
		{
			if (!reader.read(chunkHeader))
			{
				return Wave();
			}

			if (detail::MemEqual(chunkHeader.chunkID, detail::DATA_CHUNK))
			{
				break;
			}
			else
			{
				reader.setPos(reader.getPos() + chunkHeader.chunkSize);
			}
		}

		const uint32 size_bytes = chunkHeader.chunkSize;
		const size_t num_samples = size_bytes / (formatHeader.channels * (formatHeader.bitsWidth / 8));

		Wave wave(num_samples, Arg::samplingRate = formatHeader.samplerate);

		if (formatHeader.bitsWidth == 8 && formatHeader.channels == 1)
		{
			// PCM 8bit 1ch
			Array<uint8> samples(num_samples);

			reader.read(samples.data(), size_bytes);

			for (size_t i = 0; i < num_samples; ++i)
			{
				wave[i].set(samples[i] / 127.5f - 1.0f);
			}
		}
		else if (formatHeader.bitsWidth == 8 && formatHeader.channels == 2)
		{
			// PCM 8bit 2ch
			Array<WS8bit> samples(num_samples);

			reader.read(samples.data(), size_bytes);

			for (uint32 i = 0; i < num_samples; ++i)
			{
				wave[i].set(samples[i].left / 127.5f - 1.0f, samples[i].right / 127.5f - 1.0f);
			}
		}
		else if (formatHeader.bitsWidth == 16 && formatHeader.channels == 1)
		{
			// PCM 16bit 1ch
			Array<int16> samples(num_samples);

			reader.read(samples.data(), size_bytes);

			for (uint32 i = 0; i < num_samples; ++i)
			{
				wave[i].set(samples[i] / 32768.0f);
			}
		}
		else if (formatHeader.bitsWidth == 16 && formatHeader.channels == 2)
		{
			// PCM 16bit 2ch
			Array<WaveSampleS16> samples(num_samples);

			reader.read(samples.data(), size_bytes);

			for (uint32 i = 0; i < num_samples; ++i)
			{
				wave[i].set(samples[i].left / 32768.0f, samples[i].right / 32768.0f);
			}
		}
		else if (formatHeader.bitsWidth == 24 && formatHeader.channels == 1)
		{
			// PCM 24bit 1ch
			size_t samplesToRead = size_bytes / sizeof(WaveSmaple24S_Mono);

			const uint32 bufferSize = 16384;

			Array<WaveSmaple24S_Mono> buffer(bufferSize);

			WaveSample* pDst = &wave[0];

			for (;;)
			{
				WaveSmaple24S_Mono* pSrc = buffer.data();

				if (samplesToRead > bufferSize)
				{
					reader.read(pSrc, bufferSize * sizeof(WaveSmaple24S_Mono));

					for (uint32 i = 0; i < bufferSize; ++i)
					{
						const int32 s = ((pSrc->mono[2] << 24) | (pSrc->mono[1] << 16) | (pSrc->mono[0] << 8)) / 65536;
						pDst->set(s / 32768.0f);
						++pDst;
						++pSrc;
					}

					samplesToRead -= bufferSize;
				}
				else
				{
					reader.read(pSrc, samplesToRead * sizeof(WaveSmaple24S_Mono));

					for (uint32 i = 0; i < samplesToRead; ++i)
					{
						const int32 s = ((pSrc->mono[2] << 24) | (pSrc->mono[1] << 16) | (pSrc->mono[0] << 8)) / 65536;
						pDst->set(s / 32768.0f);
						++pDst;
						++pSrc;
					}

					break;
				}
			}
		}
		else if (formatHeader.bitsWidth == 24 && formatHeader.channels == 2)
		{
			// PCM 24bit 2ch
			size_t samplesToRead = size_bytes / sizeof(WaveSmaple24S_Stereo);

			const uint32 bufferSize = 16384;

			Array<WaveSmaple24S_Stereo> buffer(bufferSize);

			WaveSample* pDst = &wave[0];

			for (;;)
			{
				WaveSmaple24S_Stereo* pSrc = buffer.data();

				if (samplesToRead > bufferSize)
				{
					reader.read(pSrc, bufferSize * sizeof(WaveSmaple24S_Stereo));

					for (uint32 i = 0; i < bufferSize; ++i)
					{
						const int32 sL = ((pSrc->left[2] << 24) | (pSrc->left[1] << 16) | (pSrc->left[0] << 8)) / 65536;
						const int32 sR = ((pSrc->right[2] << 24) | (pSrc->right[1] << 16) | (pSrc->right[0] << 8)) / 65536;
						pDst->left = sL / 32768.0f;
						pDst->right = sR / 32768.0f;
						++pDst;
						++pSrc;
					}

					samplesToRead -= bufferSize;
				}
				else
				{
					reader.read(pSrc, samplesToRead * sizeof(WaveSmaple24S_Stereo));

					for (uint32 i = 0; i < samplesToRead; ++i)
					{
						const int32 sL = ((pSrc->left[2] << 24) | (pSrc->left[1] << 16) | (pSrc->left[0] << 8)) / 65536;
						const int32 sR = ((pSrc->right[2] << 24) | (pSrc->right[1] << 16) | (pSrc->right[0] << 8)) / 65536;
						pDst->left = sL / 32768.0f;
						pDst->right = sR / 32768.0f;
						++pDst;
						++pSrc;
					}

					break;
				}
			}
		}
		else if (formatHeader.formatID == 0x0003 && formatHeader.bitsWidth == 32 && formatHeader.channels == 1)
		{
			// PCM 32bit float 1ch
			Array<float> samples(num_samples);

			reader.read(samples.data(), size_bytes);

			for (uint32 i = 0; i < num_samples; ++i)
			{
				wave[i].set(samples[i]);
			}
		}
		else if (formatHeader.formatID == 0x0003 && formatHeader.bitsWidth == 32 && formatHeader.channels == 2)
		{
			// PCM 32bit float 2ch
			reader.read(wave.data(), size_bytes);
		}
		else
		{
			return Wave();
		}

		return wave;
	}