bool PropertyBitmaskConverter::applyMaskWithInValues_helper(QVariant &inVariant, QVariant &outVariant) { bool ok(true); QBitArray inBitRepres = variantToBitArray(inVariant, ok); qDebug()<<"In value \t"<<inBitRepres; if (!ok) return ok; QBitArray outBitRepres = variantToBitArray(outVariant, ok); qDebug()<<"Out value \t"<<outBitRepres; if (!ok) return ok; Q_ASSERT(!_bitmaskFromInternal.isNull()); QBitArray maskCopy = _bitmaskFromInternal; if (outBitRepres.count() != 0) maskCopy.truncate(outBitRepres.size()); else outBitRepres.resize(maskCopy.count()); inBitRepres.truncate(maskCopy.count()); qDebug()<<"Mask (mod)\t"<<maskCopy; //! \todo optimize and beautify it if (inVariant.type() == QVariant::Bool) inBitRepres = maskCopy; else inBitRepres &= (maskCopy);//get only masked bits outBitRepres &= (~maskCopy);//clear mask bits outBitRepres |= inBitRepres;//set relevant bits from mask qDebug()<<"Out res\t"<<outBitRepres; return variantFromBitArray(outVariant, outBitRepres); }
quint32 CBerBitStringStorage::deserialize(CBerByteArrayInputStream& iStream, QObject* obj, CBerLength& length, quint32 codeLength) { length.decode(iStream); qDebug() << "CBerBitStringStorage::deserialize, length extracted: " << length.getVal(); qint32 lenVal = length.getVal(); QByteArray data(lenVal, Qt::Initialization::Uninitialized); if (data.size() > 0) { QBitArray val; qint32 rdLength = iStream.read(data, 0, lenVal); if (rdLength == lenVal) { val.resize(rdLength); for (qint32 i=0; i<rdLength; ++i) { if (data[i]) val.setBit(i); else val.clearBit(i); } codeLength += rdLength + 1; QBitArray* pVal = &val; QVariant wrvar(PtrMetaTypes::s_QBitArrayPtrMetaType, &pVal); obj->metaObject()->property(3).write(obj, wrvar); } } return codeLength; }
// Compare images: QBitArray OKFilter::imageToVect(const QString &inputImage) { // Check src file: QFileInfo fi(inputImage); // Check files: if (!fi.exists()) return QBitArray(); QImage img(inputImage); if (img.isNull()) return QBitArray(); QBitArray result; result.resize(img.width()*img.height()); int c = 0; for (int j=0; j<img.height(); j++) for (int i=0; i<img.width(); i++) { QRgb color = img.pixel(i, j); int alpha = qAlpha(color); result[c++] = (alpha>0); } return result; }
QByteArray dehuffman(QByteArray data, MainWindow *mainWindow) { QHash<quint8, QBitArray> codes; qint32 size; QBitArray bits; { QDataStream stream(data); stream >> codes >> size >> bits; Q_ASSERT(stream.status() == QDataStream::Ok); } QHash<QBitArray, quint8> table; for (int i = 0; i < tableSize; ++i) if (codes.contains(i)) table[codes[i]] = i; QByteArray result; result.reserve(data.size()); int index = 0; for (int i = 0; i < size; ++i) { mainWindow->setProgress(qRound(static_cast<qreal>(100 * i) / size)); QBitArray b; while (!table.contains(b)) { b.resize(b.size() + 1); if (bits[index]) b.setBit(b.size() - 1); ++index; } result.append(table[b]); } return result; }
void tst_QBitArray::countBits2() { QBitArray bitArray; for (int i = 0; i < 4017; ++i) { bitArray.resize(i); bitArray.fill(true); QCOMPARE(bitArray.count(true), i); QCOMPARE(bitArray.count(false), 0); bitArray.fill(false); QCOMPARE(bitArray.count(true), 0); QCOMPARE(bitArray.count(false), i); } }
void tst_QBitArray::resize() { // -- check that a resize handles the bits correctly QBitArray a = QStringToQBitArray(QString("11")); a.resize(10); QVERIFY(a.size() == 10); QCOMPARE( a, QStringToQBitArray(QString("1100000000")) ); a.setBit(9); a.resize(9); // now the bit in a should have been gone: QCOMPARE( a, QStringToQBitArray(QString("110000000")) ); // grow the array back and check the new bit a.resize(10); QCOMPARE( a, QStringToQBitArray(QString("1100000000")) ); // other test with and a.resize(9); QBitArray b = QStringToQBitArray(QString("1111111111")); b &= a; QCOMPARE( b, QStringToQBitArray(QString("1100000000")) ); }
/** * Helper function to initialize a bitarray from a string */ static QBitArray QStringToQBitArray(const QString &str) { QBitArray ba; ba.resize(str.length()); int i; QChar tru('1'); for (i = 0; i < str.length(); i++) { if (str.at(i) == tru) { ba.setBit(i, true); } } return ba; }
void KoList::setContinueNumbering(int level, bool enable) { Q_ASSERT(level > 0 && level <= 10); level = qMax(qMin(level, 10), 1); QBitArray bitArray = d->properties[ContinueNumbering].toBitArray(); if (bitArray.isEmpty()) bitArray.resize(10); bitArray.setBit(level-1, enable); d->properties[ContinueNumbering] = bitArray; QTextList *textList = d->textLists.value(level-1).data(); if (!textList) return; QTextListFormat format = textList->format(); if (enable) { format.setProperty(KListStyle::ContinueNumbering, true); } else { format.clearProperty(KListStyle::ContinueNumbering); } textList->setFormat(format); }
void CFillTool::doFill() { // This is not the nicest code, but.. it works? auto map = widget()->map(); int toReplace = map->get(m_whatType, m_hovered); if (toReplace == m_whatValue) return; QBitArray checked; checked.resize(map->width() * map->height()); QQueue<CMapPoint> queue; queue.enqueue(m_hovered); while (!queue.isEmpty()) { CMapPoint point = queue.dequeue(); int bitID = ((point.x / 2) * map->width()) + point.y; checked.setBit(bitID); CEditCommand::Change change; change.before = toReplace; change.after = m_whatValue; change.type = m_whatType; change.pos = point; m_command->changes.append(change); // Check every side to see what we've got to add to the list CMapPoint nw = point; nw.moveNW(); int nwBit = ((nw.x / 2) * map->width()) + nw.y; if (map->positionValid(nw) && !checked.testBit(nwBit) && map->get(m_whatType, nw) == toReplace) { checked.setBit(nwBit); queue.enqueue(nw); } CMapPoint ne = point; ne.moveNE(); int neBit = ((ne.x / 2) * map->width()) + ne.y; if (map->positionValid(ne) && !checked.testBit(neBit) && map->get(m_whatType, ne) == toReplace) { checked.setBit(neBit); queue.enqueue(ne); } CMapPoint sw = point; sw.moveSW(); int swBit = ((sw.x / 2) * map->width()) + sw.y; if (map->positionValid(sw) && !checked.testBit(swBit) && map->get(m_whatType, sw) == toReplace) { checked.setBit(swBit); queue.enqueue(sw); } CMapPoint se = point; se.moveSE(); int seBit = ((se.x / 2) * map->width()) + se.y; if (map->positionValid(se) && !checked.testBit(seBit) && map->get(m_whatType, se) == toReplace) { checked.setBit(seBit); queue.enqueue(se); } } /* while (!queue.isEmpty()) { CMapPoint point = queue.dequeue(); // First off, check the NW side of this point CMapPoint check = point; while (true) { check.moveNW(); if (!map->positionValid(check)) break; if (map->get(m_whatType, check) != toReplace) break; int bitID = ((check.x / 2) * map->width()) + check.y; if (checked.testBit(bitID)) break; checked.setBit(bitID); CEditCommand::Change change; change.before = toReplace; change.after = m_whatValue; change.type = m_whatType; change.pos = check; m_command->changes.append(change); // Check if we need to add anything SW/NE of this one CMapPoint checkSW = check; checkSW.moveSW(); if (map->positionValid(checkSW) && map->get(m_whatType, checkSW) == toReplace) queue.enqueue(checkSW); CMapPoint checkNE = check; checkNE.moveNE(); if (map->positionValid(checkNE) && map->get(m_whatType, checkNE) == toReplace) queue.enqueue(checkNE); } // Now check the SE side check = point; while (true) { check.moveSE(); if (!map->positionValid(check)) break; if (map->get(m_whatType, check) != toReplace) break; int bitID = ((check.x / 2) * map->width()) + check.y; if (checked.testBit(bitID)) break; checked.setBit(bitID); CEditCommand::Change change; change.before = toReplace; change.after = m_whatValue; change.type = m_whatType; change.pos = check; m_command->changes.append(change); // Check if we need to add anything SW/NE of this one CMapPoint checkSW = check; checkSW.moveSW(); if (map->positionValid(checkSW) && map->get(m_whatType, checkSW) == toReplace) queue.enqueue(checkSW); CMapPoint checkNE = check; checkNE.moveNE(); if (map->positionValid(checkNE) && map->get(m_whatType, checkNE) == toReplace) queue.enqueue(checkNE); } }*/ }
bool Compress::compress() { File * f = new File(m_path, m_fileName); QByteArray qba = f->read(); if (qba != NULL) { CountOccurrence * co = new CountOccurrence(qba); QList<Occurrence> occur = co->orderByOccurrence(); HuffmanTree * cht = new HuffmanTree(occur); Tree * tree = cht->createTree(); tree->createRep(); cht->createHash(tree); QString data; for (int i = 0; i < qba.size(); ++i) { QString pathNode = cht->hash()->value(qba.at(i)); data.append(pathNode); } int garbageSize = 8 - data.size()%8; if (garbageSize == 8) { garbageSize = 0; } for (int i = 0; i < garbageSize; ++i) { data.append("0"); } QBitArray toByte; toByte.resize(data.size()); for (int i = 0; i < data.size(); ++i) { if (data.at(i) == '0') { toByte[i] = true; } else { toByte[i] = false; } } bool ok; QByteArray encoded; QString c; for (int i = 0; i < data.size(); i+=8) { QString h = data.mid(i,8); encoded.append(QChar(h.toInt(&ok, 2))); c.append(h); } QString binaryGarbageSize = QString::number(garbageSize,2); QString binaryTreeSize = QString::number(tree->rep().size(),2); int zeros = 16 - (binaryGarbageSize.size() + binaryTreeSize.size()); for (int i = 0; i < zeros; ++i) { binaryTreeSize.prepend(QString::number(0)); } QString toBit = binaryGarbageSize; toBit.append(binaryTreeSize); int h1 = toBit.mid(0,8).toInt(&ok, 2); int h2 = toBit.mid(8,8).toInt(&ok, 2); QByteArray toWrite; toWrite.clear(); toWrite.append(QChar(h1)); toWrite.append(QChar(h2)); toWrite.append(m_fileName); for (int i = m_fileName.size(); i < 128; ++i ) { toWrite.append("#"); } toWrite.append(tree->rep()); toWrite.append(encoded); f->write(toWrite, m_path + m_compressedFileName); qDebug() << m_fileName << " comprimido"; return true; } else { qDebug() << "Arquivo não encontrado"; return false; } }
QByteArray huffman(QByteArray data, MainWindow *mainWindow) { // count int count[tableSize]; qFill(&count[0], &count[tableSize - 1], 0); for (int i = 0; i < data.size(); ++i) ++count[static_cast<quint8>(data[i])]; QMultiMap<int, QList<QPair<quint8, QBitArray> > > p; // <count, <symbol, code> > for (int i = 0; i < tableSize; ++i) { if (count[i] == 0) continue; QList<QPair<quint8, QBitArray> > list; list.append(qMakePair(static_cast<quint8>(i), QBitArray())); p.insert(count[i], list); } // caculate codes from bottom to top while (p.size() > 1) { const int count0 = p.begin().key(); QList<QPair<quint8, QBitArray> > list0 = p.begin().value(); p.erase(p.begin()); const int count1 = p.begin().key(); QList<QPair<quint8, QBitArray> > list1 = p.begin().value(); p.erase(p.begin()); for (QList<QPair<quint8, QBitArray> >::Iterator iter = list0.begin(); iter != list0.end(); ++iter) { iter->second.resize(iter->second.size() + 1); iter->second.setBit(iter->second.size() - 1, false); } for (QList<QPair<quint8, QBitArray> >::Iterator iter = list1.begin(); iter != list1.end(); ++iter) { iter->second.resize(iter->second.size() + 1); iter->second.setBit(iter->second.size() - 1, true); } p.insert(count0 + count1, list0 + list1); } // extract codes QHash<quint8, QBitArray> codes; for (QList<QPair<quint8, QBitArray> >::ConstIterator iter = p.begin().value().constBegin(); iter != p.begin().value().constEnd(); ++iter) { QBitArray code; code.resize(iter->second.size()); for (int j = 0; j < code.size(); ++j) if (iter->second[code.size() - j - 1]) code.setBit(j); codes[iter->first] = code; } // encode QBitArray bits; for (int i = 0; i < data.size(); ++i) { mainWindow->setProgress(qRound(static_cast<qreal>(100 * i) / data.size())); const QBitArray &code = codes[static_cast<quint8>(data[i])]; const int oldSize = bits.size(); bits.resize(oldSize + code.size()); for (int i = 0; i < code.size(); ++i) if (code[i]) bits.setBit(oldSize + i); } QByteArray result; { QDataStream stream(&result, QIODevice::WriteOnly); stream << codes << static_cast<qint32>(data.size()) << bits; } return result; }
void SerialDecoderNode::inputsUpdated( qint64 pTimeStamp ) { if( mPinInputBits->isUpdated( pTimeStamp ) ) { QBitArray InpDat = variant( mPinInputBits ).toBitArray(); if( !InpDat.isEmpty() ) { QBitArray SrcDat; QByteArray DstDat; // Prepend any buffered data if( !mBitBuf.isEmpty() ) { int InpSze = InpDat.size(); int BufSze = mBitBuf.size(); SrcDat.resize( BufSze + InpSze ); for( int i = 0 ; i < BufSze ; i++ ) { SrcDat.setBit( i, mBitBuf.testBit( i ) ); } for( int i = 0 ; i < InpSze ; i++ ) { SrcDat.setBit( BufSze + i, InpDat.testBit( i ) ); } mBitBuf.clear(); } else { SrcDat.swap( InpDat ); } // Look for data // qDebug() << "R:" << SrcDat; int SrcOff; for( SrcOff = 0 ; SrcOff < SrcDat.size() - 9 ; ) { if( SrcDat.testBit( SrcOff ) || !SrcDat.testBit( SrcOff + 9 ) ) { SrcOff++; continue; } QBitArray T( 8 ); quint8 C = 0; for( int j = 0 ; j < 8 ; j++ ) { C |= ( SrcDat.testBit( SrcOff + 1 + j ) ? 0x01 : 0x00 ) << j; T.setBit( j, SrcDat.testBit( SrcOff + 1 + j ) ); } // qDebug() << SrcOff << T; DstDat.append( C ); SrcOff += 10; } if( SrcOff < SrcDat.size() ) { if( SrcOff > 0 ) { mBitBuf.resize( SrcDat.size() - SrcOff ); for( int i = 0 ; i < mBitBuf.size() ; i++ ) { mBitBuf.setBit( i, SrcDat.testBit( SrcOff + i ) ); } } else { SrcDat.swap( mBitBuf ); } // qDebug() << "B" << mBitBuf; } if( !DstDat.isEmpty() ) { mValOutputData->setVariant( DstDat ); pinUpdated( mPinOutputData ); } } } }