Beispiel #1
0
voidpf ZCALLBACK qiodevice_open_file_func (
   voidpf opaque,
   voidpf file,
   int mode)
{
    QIODevice_descriptor *d = reinterpret_cast<QIODevice_descriptor*>(opaque);
    QIODevice *iodevice = reinterpret_cast<QIODevice*>(file);
    QIODevice::OpenMode desiredMode;
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
        desiredMode = QIODevice::ReadOnly;
    else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
        desiredMode = QIODevice::ReadWrite;
    else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
        desiredMode = QIODevice::WriteOnly;
    if (iodevice->isOpen()) {
        if ((iodevice->openMode() & desiredMode) == desiredMode) {
            if (desiredMode != QIODevice::WriteOnly
                    && iodevice->isSequential()) {
                // We can use sequential devices only for writing.
                delete d;
                return NULL;
            } else {
                if ((desiredMode & QIODevice::WriteOnly) != 0) {
                    // open for writing, need to seek existing device
                    if (!iodevice->isSequential()) {
                        iodevice->seek(0);
                    } else {
                        d->pos = iodevice->pos();
                    }
                }
            }
            return iodevice;
        } else {
            delete d;
            return NULL;
        }
    }
    iodevice->open(desiredMode);
    if (iodevice->isOpen()) {
        if (desiredMode != QIODevice::WriteOnly && iodevice->isSequential()) {
            // We can use sequential devices only for writing.
            iodevice->close();
            delete d;
            return NULL;
        } else {
            return iodevice;
        }
    } else {
        delete d;
        return NULL;
    }
}
bool QIODeviceProto::isSequential() const
{
  QIODevice *item = qscriptvalue_cast<QIODevice*>(thisObject());
  if (item)
    return item->isSequential();
  return false;
}
Beispiel #3
0
voidpf ZCALLBACK qiodevice_open_file_func (
   voidpf opaque UNUSED,
   voidpf file,
   int mode)
{
    QIODevice *iodevice = reinterpret_cast<QIODevice*>(file);
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
        iodevice->open(QIODevice::ReadOnly);
    else
    if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
        iodevice->open(QIODevice::ReadWrite);
    else
    if (mode & ZLIB_FILEFUNC_MODE_CREATE)
        iodevice->open(QIODevice::WriteOnly);

    if (iodevice->isOpen()) {
        if (iodevice->isSequential()) {
            iodevice->close();
            return NULL;
        } else {
            return iodevice;
        }
    } else
        return NULL;
}
bool QHttpMultiPartIODevice::isSequential() const
{
    for (int a = 0; a < multiPart->parts.count(); a++) {
        QIODevice *device = multiPart->parts.at(a).d->bodyDevice;
        // we are sequential if any of the bodyDevices of our parts are sequential;
        // when reading from a byte array, we are not sequential
        if (device && device->isSequential())
            return true;
    }
    return false;
}
Beispiel #5
0
bool WavHeader::writeDataLength(QIODevice &device, qint64 dataLength)
{
    bool result = false;
    if (!device.isSequential()) {
        device.seek(40);
        unsigned char dataLengthLE[4];
        qToLittleEndian<quint32>(quint32(dataLength), dataLengthLE);
        result = (device.write(reinterpret_cast<const char *>(dataLengthLE), 4) == 4);
    }
    return result;
}
Beispiel #6
0
voidpf ZCALLBACK qiodevice_open_file_func (
   voidpf /*opaque UNUSED*/,
   voidpf file,
   int mode)
{
    QIODevice *iodevice = reinterpret_cast<QIODevice*>(file);
    QIODevice::OpenMode desiredMode;
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
        desiredMode = QIODevice::ReadOnly;
    else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
        desiredMode = QIODevice::ReadWrite;
    else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
        desiredMode = QIODevice::WriteOnly;
    if (iodevice->isOpen()) {
        if ((iodevice->openMode() & desiredMode) == desiredMode) {
            if (iodevice->isSequential()) {
                return NULL;
            } else {
                if ((desiredMode & QIODevice::WriteOnly) != 0) {
                    // open for writing, need to seek existing device
                    iodevice->seek(0);
                }
            }
            return iodevice;
        } else {
            return NULL;
        }
    }
    iodevice->open(desiredMode);
    if (iodevice->isOpen()) {
        if (iodevice->isSequential()) {
            iodevice->close();
            return NULL;
        } else {
            return iodevice;
        }
    } else
        return NULL;
}
Beispiel #7
0
ZPOS64_T ZCALLBACK qiodevice64_tell_file_func (
   voidpf opaque,
   voidpf stream)
{
    QIODevice_descriptor *d = reinterpret_cast<QIODevice_descriptor*>(opaque);
    QIODevice *iodevice = reinterpret_cast<QIODevice*>(stream);
    qint64 ret;
    if (iodevice->isSequential()) {
        ret = d->pos;
    } else {
        ret = iodevice->pos();
    }
    return static_cast<ZPOS64_T>(ret);
}
Beispiel #8
0
bool WavHeader::read(QIODevice &device)
{
    bool result = true;

    if (!device.isSequential())
        result = device.seek(0);
    // else, assume that current position is the start of the header

    if (result) {
        CombinedHeader header;
        result = (device.read(reinterpret_cast<char *>(&header), HeaderLength) == HeaderLength);
        if (result) {
            if ((memcmp(&header.riff.descriptor.id, "RIFF", 4) == 0
                || memcmp(&header.riff.descriptor.id, "RIFX", 4) == 0)
                && memcmp(&header.riff.type, "WAVE", 4) == 0
                && memcmp(&header.wave.descriptor.id, "fmt ", 4) == 0
                && header.wave.audioFormat == 1 // PCM
            ) {
                if (memcmp(&header.riff.descriptor.id, "RIFF", 4) == 0)
                    m_format.setByteOrder(QAudioFormat::LittleEndian);
                else
                    m_format.setByteOrder(QAudioFormat::BigEndian);

                m_format.setChannels(qFromLittleEndian<quint16>(header.wave.numChannels));
                m_format.setCodec("audio/pcm");
                m_format.setFrequency(qFromLittleEndian<quint32>(header.wave.sampleRate));
                m_format.setSampleSize(qFromLittleEndian<quint16>(header.wave.bitsPerSample));

                switch(header.wave.bitsPerSample) {
                case 8:
                    m_format.setSampleType(QAudioFormat::UnSignedInt);
                    break;
                case 16:
                    m_format.setSampleType(QAudioFormat::SignedInt);
                    break;
                default:
                    result = false;
                }

                m_dataLength = device.size() - HeaderLength;
            } else {
                result = false;
            }
        }
    }

    return result;
}
Beispiel #9
0
uLong ZCALLBACK qiodevice_tell_file_func (
   voidpf opaque,
   voidpf stream)
{
    QIODevice_descriptor *d = reinterpret_cast<QIODevice_descriptor*>(opaque);
    QIODevice *iodevice = reinterpret_cast<QIODevice*>(stream);
    uLong ret;
    qint64 ret64;
    if (iodevice->isSequential()) {
        ret64 = d->pos;
    } else {
        ret64 = iodevice->pos();
    }
    ret = static_cast<uLong>(ret64);
    return ret;
}
Dynamic::ReplacementBias::ReplacementBias( const QString &n, QXmlStreamReader *reader )
    : m_name( n )
{
    // -- read the original bias data as one block
    quint64 start = reader->characterOffset();
    reader->skipCurrentElement();
    quint64 end = reader->characterOffset();

    QIODevice *device = reader->device();
    if( device->isSequential() )
    {
        warning() << "Cannot read xml for bias"<<n<<"from sequential device.";
        return;
    }
    device->seek( start );
    m_html = device->read( end - start );

    debug() << "replacement bias for"<<n<<"is"<<m_html;

    connect( BiasFactory::instance(), SIGNAL(changed()), this, SLOT(factoryChanged()) );
}
Beispiel #11
0
/*! Read and parse the header block from the given input device stream. 
    The stream must be random access, binary and positioned to 0.

	 \retval true the header was read successfully.
	 \retval false the header failed to read for some reason.
 */
bool BlockHeader::Read(
	QIODevice& fIn	//! Input stream (binary, random access).
							 )
{
	ASSERT(fIn.isSequential()==false);
	ASSERT(fIn.isReadable()==true);
	ASSERT(fIn.isTextModeEnabled()==false);
	ASSERT(fIn.pos() == 0);
	ASSERT(sizeof(float) == 4);
	
	QDataStream in(&fIn);
	// Check the magic number
	quint32 uiMagic;
	in >> uiMagic;
	// OK, try to read the rest of the header.
	if(uiMagic == MAGIC_NUMBER) {
		in >> m_uiVersion;
		in >> m_iJulianDay;
		
		in >> m_fOriginLon;
		in >> m_fOriginLat;
		in >> m_fArcLon;
		in >> m_fArcLat;
		
		in >> m_uiRasterPos;
		in >> m_uiVectorPos;
		
		// The next 24 bytes are reserved. 
		// They should be zero, but we do not read them, just skip them.
		// Some additional checks are fast and harmless.
		if(   m_uiVersion >= 1 
		   && m_iJulianDay > 2454110 // 9.Jan.2007
			&& m_fArcLon >= 1.0f
			&& m_fArcLat >= 1.0f
			&& fIn.size() >= MBH_HEADER_SIZE
		  ) {
			fIn.seek(MBH_HEADER_SIZE); // position the file pointer to the 64-th byte.
			return true;
		}
	}
Beispiel #12
0
static
void CALLBACK_CALL_TYPE iod_read_fn(png_structp png_ptr, png_bytep data, png_size_t length)
{
    QPngHandlerPrivate *d = (QPngHandlerPrivate *)png_get_io_ptr(png_ptr);
    QIODevice *in = d->q->device();

    if (d->state == QPngHandlerPrivate::ReadingEnd && !in->isSequential() && (in->size() - in->pos()) < 4 && length == 4) {
        // Workaround for certain malformed PNGs that lack the final crc bytes
        uchar endcrc[4] = { 0xae, 0x42, 0x60, 0x82 };
        qMemCopy(data, endcrc, 4);
        in->seek(in->size());
        return;
    }

    while (length) {
        int nr = in->read((char*)data, length);
        if (nr <= 0) {
            png_error(png_ptr, "Read Error");
            return;
        }
        length -= nr;
    }
}
Beispiel #13
0
/*!
    Only Size option is supported
*/
QVariant QWbmpHandler::option(ImageOption option) const
{
    if (option == QImageIOHandler::Size) {
        QIODevice *device = QImageIOHandler::device();
        if (device->isSequential())
            return QVariant();

        // Save old position
        qint64 oldPos = device->pos();

        WBMPHeader hdr;
        if (readWBMPHeader(device, &hdr)) {
            device->seek(oldPos);
            return QSize(hdr.width, hdr.height);
        }

        device->seek(oldPos);

    } else if (option == QImageIOHandler::ImageFormat) {
        return QVariant(QImage::Format_Mono);
    }

    return QVariant();
}
Beispiel #14
0
int test_1(QObject* a)
{
    QAudioFormat format;
    // Set up the format, eg.

    format.setSampleRate(44100);
    format.setChannelCount(2);
    format.setCodec("audio/pcm");
    format.setSampleType(QAudioFormat::SignedInt);
    format.setSampleSize(16);
    format.setByteOrder(QAudioFormat::LittleEndian);

    QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
    if (!info.isFormatSupported(format)) {
        qDebug() << "Raw audio format not supported by backend, cannot play audio.";
        return -1;
    }

    char filename[1024];
    printf("input file name :");
    scanf("%s",filename);
    fflush(stdin);
    fflush(stdout);

    printf("start audio \n");
    QAudioOutput *audio;
    audio = new QAudioOutput(format, a);

    QIODevice *out = audio->start();

#if 0
    qDebug()<< out->isSequential() << " swquential"; // 不是顺序设备,是随机设备
#endif

    decodeAndPlay(filename,out);
}
Beispiel #15
0
int ZCALLBACK qiodevice64_seek_file_func (
   voidpf /*opaque UNUSED*/,
   voidpf stream,
   ZPOS64_T offset,
   int origin)
{
    QIODevice *iodevice = reinterpret_cast<QIODevice*>(stream);
    if (iodevice->isSequential()) {
        if (origin == ZLIB_FILEFUNC_SEEK_END
                && offset == 0) {
            // sequential devices are always at end (needed in mdAppend)
            return 0;
        } else {
            qWarning("qiodevice_seek_file_func() called for sequential device");
            return -1;
        }
    }
    qint64 qiodevice_seek_result=0;
    int ret;
    switch (origin)
    {
    case ZLIB_FILEFUNC_SEEK_CUR :
        qiodevice_seek_result = ((QIODevice*)stream)->pos() + offset;
        break;
    case ZLIB_FILEFUNC_SEEK_END :
        qiodevice_seek_result = ((QIODevice*)stream)->size() - offset;
        break;
    case ZLIB_FILEFUNC_SEEK_SET :
        qiodevice_seek_result = offset;
        break;
    default:
        return -1;
    }
    ret = !iodevice->seek(qiodevice_seek_result);
    return ret;
}
Beispiel #16
0
image::Image image::readTGA(QIODevice& content) {
    enum class TGAImageType : uint8_t {
        NoImageData = 0,
        UncompressedColorMapped = 1,
        UncompressedTrueColor = 2,
        UncompressedBlackWhite = 3,
        RunLengthEncodedColorMapped = 9,
        RunLengthEncodedTrueColor = 10,
        RunLengthEncodedBlackWhite = 11,
    };

    struct TGAHeader {
        uint8_t idLength;
        uint8_t colorMapType;
        TGAImageType imageType;
        struct {
            uint64_t firstEntryIndex : 16;
            uint64_t length : 16;
            uint64_t entrySize : 8;
        } colorMap;
        uint16_t xOrigin;
        uint16_t yOrigin;
        uint16_t width;
        uint16_t height;
        uint8_t pixelDepth;
        struct {
            uint8_t attributeBitsPerPixel : 4;
            uint8_t reserved : 1;
            uint8_t orientation : 1;
            uint8_t padding : 2;
        } imageDescriptor;
    };

    constexpr bool WANT_DEBUG { false };
    constexpr qint64 TGA_HEADER_SIZE_BYTES { 18 };

    // BottomLeft: 0, TopLeft: 1
    constexpr uint8_t ORIENTATION_BOTTOM_LEFT { 0 };

    TGAHeader header;

    if (content.isSequential()) {
        qWarning(imagelogging) << "TGA - Sequential devices are not supported for reading";
        return QImage();
    }

    if (content.bytesAvailable() < TGA_HEADER_SIZE_BYTES) {
        qWarning(imagelogging) << "TGA - Unexpectedly reached end of file";
        return QImage();
    }

    content.read((char*)&header.idLength, 1);
    content.read((char*)&header.colorMapType, 1);
    content.read((char*)&header.imageType, 1);
    content.read((char*)&header.colorMap, 5);
    content.read((char*)&header.xOrigin, 2);
    content.read((char*)&header.yOrigin, 2);
    content.read((char*)&header.width, 2);
    content.read((char*)&header.height, 2);
    content.read((char*)&header.pixelDepth, 1);
    content.read((char*)&header.imageDescriptor, 1);

    if (WANT_DEBUG) {
        qDebug(imagelogging) << "Id Length: " << (int)header.idLength;
        qDebug(imagelogging) << "Color map: " << (int)header.colorMap.firstEntryIndex << header.colorMap.length << header.colorMap.entrySize;
        qDebug(imagelogging) << "Color map type: " << (int)header.colorMapType;
        qDebug(imagelogging) << "Image type: " << (int)header.imageType;
        qDebug(imagelogging) << "Origin: " << header.xOrigin << header.yOrigin;
        qDebug(imagelogging) << "Size: " << header.width << header.height;
        qDebug(imagelogging) << "Depth: " << header.pixelDepth;
        qDebug(imagelogging) << "Image desc: " << header.imageDescriptor.attributeBitsPerPixel << (int)header.imageDescriptor.orientation;
    }

    if (header.xOrigin != 0 || header.yOrigin != 0) {
        qWarning(imagelogging) << "TGA - origin not supporter";
        return QImage();
    }

    if (!(header.pixelDepth == 24 && header.imageDescriptor.attributeBitsPerPixel == 0) && header.pixelDepth != 32) {
        qWarning(imagelogging) << "TGA - Only pixel depths of 24 (with no alpha) and 32 bits are supported";
        return QImage();
    }

    if (header.imageDescriptor.attributeBitsPerPixel != 0 && header.imageDescriptor.attributeBitsPerPixel != 8) {
        qWarning(imagelogging) << "TGA - Only 0 or 8 bits for the alpha channel is supported";
        return QImage();
    }

    char alphaMask = header.imageDescriptor.attributeBitsPerPixel == 8 ? 0x00 : 0xFF;
    int bytesPerPixel = header.pixelDepth / 8;

    content.skip(header.idLength);
    if (header.imageType == TGAImageType::UncompressedTrueColor) {
        qint64 minimumSize = header.width * header.height * bytesPerPixel;
        if (content.bytesAvailable() < minimumSize) {
            qWarning(imagelogging) << "TGA - Unexpectedly reached end of file";
            return QImage();
        }

        QImage image{ header.width, header.height, QImage::Format_ARGB32 };
        char* line;
        for (int y = 0; y < header.height; ++y) {
            if (header.imageDescriptor.orientation == ORIENTATION_BOTTOM_LEFT) {
                line = (char*)image.scanLine(header.height - y - 1);
            } else {
                line = (char*)image.scanLine(y);
            }
            for (int x = 0; x < header.width; ++x) {
                content.read(line, bytesPerPixel);
                *(line + 3) |= alphaMask;

                line += 4;
            }
        }
        return image;
    } else if (header.imageType == TGAImageType::RunLengthEncodedTrueColor) {
        QImage image{ header.width, header.height, QImage::Format_ARGB32 };

        for (int y = 0; y < header.height; ++y) {
            char* line;
            if (header.imageDescriptor.orientation == ORIENTATION_BOTTOM_LEFT) {
                line = (char*)image.scanLine(header.height - y - 1);
            } else {
                line = (char*)image.scanLine(y);
            }
            int col = 0;
            while (col < header.width) {
                constexpr char IS_REPETITION_MASK{ (char)0x80 };
                constexpr char LENGTH_MASK{ (char)0x7f };
                char repetition;
                if (content.read(&repetition, 1) != 1) {
                    qWarning(imagelogging) << "TGA - Unexpectedly reached end of file";
                    return QImage();
                }
                bool isRepetition = repetition & IS_REPETITION_MASK;

                // The length in `repetition` is always 1 less than the number of following pixels,
                // so we need to increment it by 1. Because of this, the length is never 0.
                int length = (repetition & LENGTH_MASK) + 1;

                if (isRepetition) {
                    // Read into temporary buffer
                    char color[4];
                    if (content.read(color, bytesPerPixel) != bytesPerPixel) {
                        qWarning(imagelogging) << "TGA - Unexpectedly reached end of file";
                        return QImage();
                    }
                    color[3] |= alphaMask;

                    // Copy `length` number of times
                    col += length;
                    while (length-- > 0) {
                        *line = color[0];
                        *(line + 1) = color[1];
                        *(line + 2) = color[2];
                        *(line + 3) = color[3];

                        line += 4;
                    }
                } else {
                    qint64 minimumSize = length * bytesPerPixel;
                    if (content.bytesAvailable() < minimumSize) {
                        qWarning(imagelogging) << "TGA - Unexpectedly reached end of file";
                        return QImage();
                    }

                    // Read in `length` number of pixels
                    col += length;
                    while (length-- > 0) {
                        content.read(line, bytesPerPixel);
                        *(line + 3) |= alphaMask;

                        line += 4;
                    }
                }
            }
        }
        return image;
    } else {
        qWarning(imagelogging) << "TGA - Unsupported image type: " << (int)header.imageType;
    }

    return QImage();
}
Beispiel #17
0
//***************************************************************************
bool Kwave::MP3Decoder::open(QWidget *widget, QIODevice &src)
{
    qDebug("MP3Decoder::open()");
    metaData().clear();
    Q_ASSERT(!m_source);
    if (m_source) qWarning("MP3Decoder::open(), already open !");

    /* open the file in readonly mode with seek enabled */
    if (src.isSequential()) return false;
    if (!src.open(QIODevice::ReadOnly)) {
	qWarning("unable to open source in read-only mode!");
	return false;
    }

    /* read all available ID3 tags */
    ID3_Tag tag;
    ID3_QIODeviceReader adapter(src);
    tag.Link(adapter, static_cast<flags_t>(ID3TT_ALL));

    qDebug("NumFrames = %u", Kwave::toUint(tag.NumFrames()));
    /** @bug: id3lib crashes in this line on some MP3 files */
    if (tag.GetSpec() != ID3V2_UNKNOWN) {
	qDebug("Size = %u",      Kwave::toUint(tag.Size()));
    }
    qDebug("HasLyrics = %d", tag.HasLyrics());
    qDebug("HasV1Tag = %d",  tag.HasV1Tag());
    qDebug("HasV2Tag = %d",  tag.HasV2Tag());

    m_prepended_bytes = tag.GetPrependedBytes();
    m_appended_bytes  = tag.GetAppendedBytes();
    qDebug("prepended=%lu, appended=%lu", m_prepended_bytes, m_appended_bytes);

    const Mp3_Headerinfo *mp3hdr = tag.GetMp3HeaderInfo();
    if (!mp3hdr) {
	Kwave::MessageBox::sorry(widget,
	    i18n("The opened file is no MPEG file or it is damaged.\n"
	    "No header information has been found."));
	return false;
    }

    /* parse the MP3 header */
    if (!parseMp3Header(*mp3hdr, widget)) return false;

    /* parse the ID3 tags */
    if (!parseID3Tags(tag)) return false;

    /* accept the source */
    m_source = &src;
    Kwave::FileInfo info(metaData());
    info.set(Kwave::INF_MIMETYPE, _("audio/mpeg"));
    metaData().replace(Kwave::MetaDataList(info));

    // allocate a transfer buffer with 128 kB
    if (m_buffer) free(m_buffer);
    m_buffer_size = (128 << 10);

    m_buffer = static_cast<unsigned char *>(malloc(m_buffer_size));
    if (!m_buffer) return false; // out of memory :-(

    return true;
}