IplImage* CvCapture_Android::retrieveFrame( int outputType ) { IplImage* image = NULL; cv::Mat m_frameYUV420_ref = m_frameYUV420; unsigned char *current_frameYUV420=m_frameYUV420_ref.ptr(); //Attention! all the operations in this function below should occupy less time than the period between two frames from camera if (NULL != current_frameYUV420) { if (m_frameFormat == noformat) { union {double prop; const char* name;} u; u.prop = getProperty(CV_CAP_PROP_PREVIEW_FORMAT); if (0 == strcmp(u.name, "yuv420sp")) m_frameFormat = yuv420sp; else if (0 == strcmp(u.name, "yvu420sp")) m_frameFormat = yvu420sp; else m_frameFormat = yuvUnknown; } switch(outputType) { case CV_CAP_ANDROID_GREY_FRAME: if (!m_hasGray) if (!(m_hasGray = convertYUV2Grey(m_width, m_height, current_frameYUV420, m_frameGray.mat))) return NULL; image = m_frameGray.getIplImagePtr(); break; case CV_CAP_ANDROID_COLOR_FRAME_BGR: case CV_CAP_ANDROID_COLOR_FRAME_RGB: if (!m_hasColor) if (!(m_hasColor = convertYUV2BGR(m_width, m_height, current_frameYUV420, m_frameColor.mat, outputType == CV_CAP_ANDROID_COLOR_FRAME_RGB, false))) return NULL; image = m_frameColor.getIplImagePtr(); break; case CV_CAP_ANDROID_COLOR_FRAME_BGRA: case CV_CAP_ANDROID_COLOR_FRAME_RGBA: if (!m_hasColor) if (!(m_hasColor = convertYUV2BGR(m_width, m_height, current_frameYUV420, m_frameColor.mat, outputType == CV_CAP_ANDROID_COLOR_FRAME_RGBA, true))) return NULL; image = m_frameColor.getIplImagePtr(); break; default: LOGE("Unsupported frame output format: %d", outputType); CV_Error( CV_StsOutOfRange, "Output frame format is not supported." ); image = NULL; break; } } return image; }
virtual void startBlock( const Block *b, uint64_t ) { if(0<=cutoffBlock && cutoffBlock<b->height) wrapup(); uint8_t blockHash[kSHA256ByteSize]; sha256Twice(blockHash, b->data, 80); const uint8_t *p = b->data; SKIP(uint32_t, version, p); SKIP(uint256_t, prevBlkHash, p); SKIP(uint256_t, blkMerkleRoot, p); LOAD(uint32_t, blkTime, p); // id BIGINT PRIMARY KEY // hash BINARY(32) // time BIGINT fprintf(blockFile, "%" PRIu64 "\t", (blkID = b->height-1)); writeEscapedBinaryBuffer(blockFile, blockHash, kSHA256ByteSize); fputc('\t', blockFile); fprintf(blockFile, "%" PRIu64 "\n", (uint64_t)blkTime); if(0==(b->height)%500) { fprintf( stderr, "block=%8" PRIu64 " " "nbOutputs=%8" PRIu64 "\n", b->height, outputMap.size() ); } }
virtual void edge( uint64_t value, const uint8_t *upTXHash, uint64_t outputIndex, const uint8_t *outputScript, uint64_t outputScriptSize, const uint8_t *downTXHash, uint64_t inputIndex, const uint8_t *inputScript, uint64_t inputScriptSize ) { uint256_t h; uint32_t oi = outputIndex; memcpy(h.v, upTXHash, kSHA256ByteSize); uintptr_t ih = reinterpret_cast<uintptr_t>(h.v); uint32_t *h32 = reinterpret_cast<uint32_t *>(ih); h32[0] ^= oi; auto src = outputMap.find(h.v); if (outputMap.end() == src) { errFatal("unconnected input"); } if (blkID >= firstBlock) { fprintf( inputFile, "%" PRIu64 "|" "%" PRIu64 "|" "%" PRIu64 "|" "%" PRIu32 "\n" , inputID++, src->second, txID, (uint32_t)inputIndex ); } else { inputID++; } }
virtual void edge( uint64_t value, const uint8_t *upTXHash, uint64_t outputIndex, const uint8_t *outputScript, uint64_t outputScriptSize, const uint8_t *downTXHash, uint64_t inputIndex, const uint8_t *inputScript, uint64_t inputScriptSize ) { uint256_t h; uint32_t oi = outputIndex; memcpy(h.v, upTXHash, kSHA256ByteSize); uintptr_t ih = reinterpret_cast<uintptr_t>(h.v); uint32_t *h32 = reinterpret_cast<uint32_t*>(ih); h32[0] ^= oi; auto src = outputMap.find(h.v); if(outputMap.end()==src) errFatal("unconnected input"); // id BIGINT PRIMARY KEY // outputID BIGINT // txID BIGINT // offset INT fprintf( inputFile, "%" PRIu64 "\t" "%" PRIu64 "\t" "%" PRIu64 "\t" "%" PRIu32 "\n" , inputID++, src->second, txID, (uint32_t)outputIndex ); }
IplImage* CvCapture_Android::retrieveFrame( int outputType ) { IplImage* image = NULL; unsigned char *current_frameYUV420i=m_frameYUV420i; //Attention! all the operations in this function below should occupy less time than the period between two frames from camera if (NULL != current_frameYUV420i) { switch(outputType) { case CV_CAP_ANDROID_GREY_FRAME: if (!m_hasGray) if (!(m_hasGray = convertYUV420i2Grey(m_width, m_height, current_frameYUV420i, m_frameGray.mat))) return NULL; image = m_frameGray.getIplImagePtr(); break; case CV_CAP_ANDROID_COLOR_FRAME_BGR: case CV_CAP_ANDROID_COLOR_FRAME_RGB: if (!m_hasColor) if (!(m_hasColor = convertYUV420i2BGR(m_width, m_height, current_frameYUV420i, m_frameColor.mat, outputType == CV_CAP_ANDROID_COLOR_FRAME_RGB, false))) return NULL; image = m_frameColor.getIplImagePtr(); break; case CV_CAP_ANDROID_COLOR_FRAME_BGRA: case CV_CAP_ANDROID_COLOR_FRAME_RGBA: if (!m_hasColor) if (!(m_hasColor = convertYUV420i2BGR(m_width, m_height, current_frameYUV420i, m_frameColor.mat, outputType == CV_CAP_ANDROID_COLOR_FRAME_RGBA, true))) return NULL; image = m_frameColor.getIplImagePtr(); break; default: LOGE("Unsupported frame output format: %d", outputType); CV_Error( CV_StsOutOfRange, "Output frame format is not supported." ); image = NULL; break; } } return image; }
virtual void startBlock( const Block *b, uint64_t chainSize ) { uint8_t blockHash[kSHA256ByteSize]; sha256Twice(blockHash, b->data, 80); const uint8_t *p = b->data; SKIP(uint32_t, version, p); SKIP(uint256_t, prevBlkHash, p); SKIP(uint256_t, blkMerkleRoot, p); LOAD(uint32_t, blkTime, p); blkID = b->height - 1; if (blkID >= cutoffBlock) { // block_id BIGINT PRIMARY KEY // block_hash BINARY(32) // time BIGINT fprintf(blockFile, "%" PRIu64 "|", blkID); writeEscapedBinaryBuffer(blockFile, blockHash, kSHA256ByteSize); fputc('|', blockFile); fprintf(blockFile, "%" PRIu64 "\n", (uint64_t)blkTime); } if (0 == (blkID) % 5000) { fprintf( stderr, "block=%8" PRIu64 " " "nbOutputs=%9" PRIu64 "\n", blkID, outputMap.size() ); } }
virtual void startBlock( const Block *b, uint64_t ) { if (lastBlock < b->height) { wrapup(); } auto p = b->chunk->getData(); uint8_t blockHash[kSHA256ByteSize]; sha256Twice(blockHash, p, 80); SKIP(uint32_t, version, p); SKIP(uint256_t, prevBlkHash, p); SKIP(uint256_t, blkMerkleRoot, p); LOAD(uint32_t, blkTime, p); blkID = b->height - 1; if (blkID >= firstBlock) { fprintf(blockFile, "%" PRIu64 "|", blkID); writeHexEscapedBinaryBuffer(blockFile, blockHash, kSHA256ByteSize); fputc('|', blockFile); fprintf(blockFile, "%" PRIu64 "\n", (uint64_t)blkTime); } if (0 == blkID % 5000) { fprintf( stderr, "block=%8" PRIu64 " " "nbOutputs=%9" PRIu64 "\n", blkID, outputMap.size() ); } }
virtual int init( int argc, const char *argv[] ) { txID = -1; blkID = 0; inputID = 0; outputID = 0; static uint64_t sz = 32 * 1000 * 1000; outputMap.setEmptyKey(empty); outputMap.resize(sz); optparse::Values& values = parser.parse_args(argc, argv); lastBlock = values.get("atBlock").asUInt64(); if (!file_exists("../blockchain/blockchain.sqlite")) { firstBlock = 0; } else { sqlite3pp::database db("../blockchain/blockchain.sqlite"); sqlite3pp::query qry(db, "SELECT MAX(block_id) FROM blocks"); sqlite3pp::query::iterator i = qry.begin(); firstBlock = (uint64_t)atoi((*i).get<char const *>(0)) + 1; info("Resuming from block %" PRIu64 ".\n", firstBlock); } info("Dumping the blockchain..."); txFile = fopen("tx.txt", "w"); if (!txFile) sysErrFatal("couldn't open file txs.txt for writing\n"); blockFile = fopen("blocks.txt", "w"); if (!blockFile) sysErrFatal("couldn't open file blocks.txt for writing\n"); inputFile = fopen("txin.txt", "w"); if (!inputFile) sysErrFatal("couldn't open file inputs.txt for writing\n"); outputFile = fopen("txout.txt", "w"); if (!outputFile) sysErrFatal("couldn't open file outputs.txt for writing\n"); FILE *sqlFile = fopen("blockchain.sql", "w"); if (!sqlFile) sysErrFatal("couldn't open file blockChain.sql for writing\n"); fprintf( sqlFile, "PRAGMA journal_mode=MEMORY;\n" "PRAGMA synchronous=0;\n" "CREATE TABLE IF NOT EXISTS blocks(\n" " block_id BIGINT NOT NULL PRIMARY KEY,\n" " block_hash TEXT NOT NULL,\n" " time BIGINT NOT NULL\n" ");\n" "\n" "CREATE TABLE IF NOT EXISTS tx(\n" " tx_id BIGINT NOT NULL PRIMARY KEY,\n" " tx_hash TEXT NOT NULL,\n" " block_id BIGINT NOT NULL,\n" " FOREIGN KEY (block_id) REFERENCES blocks (block_id)\n" ");\n" "\n" "CREATE TABLE IF NOT EXISTS txout(\n" " txout_id BIGINT NOT NULL PRIMARY KEY,\n" " address CHAR(40),\n" " txout_value BIGINT NOT NULL,\n" " tx_id BIGINT NOT NULL,\n" " txout_pos INT NOT NULL,\n" " FOREIGN KEY (tx_id) REFERENCES tx (tx_id)\n" ");\n" "\n" "CREATE TABLE IF NOT EXISTS txin(\n" " txin_id BIGINT NOT NULL PRIMARY KEY,\n" " txout_id BIGINT NOT NULL,\n" " tx_id BIGINT NOT NULL,\n" " txin_pos INT NOT NULL,\n" " FOREIGN KEY (tx_id) REFERENCES tx (tx_id)\n" ");\n" "CREATE INDEX IF NOT EXISTS x_txin_txout ON txin (txout_id);\n" "CREATE INDEX IF NOT EXISTS x_txout_address ON txout (address);\n" "CREATE INDEX IF NOT EXISTS x_txin_txid ON txin (tx_id);\n" "CREATE INDEX IF NOT EXISTS x_txout_txid ON txout (tx_id);\n" "CREATE INDEX IF NOT EXISTS x_txout_value ON txout (txout_value);\n" "CREATE VIEW IF NOT EXISTS tx_full AS SELECT blocks.time, tx.tx_hash, tx.tx_id, txout.address, txout.txout_value FROM txout LEFT JOIN tx ON (tx.tx_id = txout.tx_id) LEFT JOIN blocks ON (tx.block_id = blocks.block_id);\n" "\n" ); fclose(sqlFile); FILE *bashFile = fopen("blockchain.sh", "w"); if (!bashFile) sysErrFatal("Couldn't open file blockchain.sh for writing!\n"); fprintf( bashFile, "\n" "#!/bin/bash\n" "\n" "echo 'Recreating DB blockchain...'\n" "mkdir ../blockchain\n" "sqlite3 ../blockchain/blockchain.sqlite < blockchain.sql\n" "rm -f blockchain.sql\n" "echo done.\n" "echo\n" "rm -f blockchain.sql\n" "\n" "for i in blocks txin txout tx\n" "do\n" " echo \"Importing table ${i}...\"\n" " echo \".import $i.txt $i\" | sqlite3 ../blockchain/blockchain.sqlite\n" " echo done.\n" " rm -f $i.txt\n" " echo\n" "done\n" "rm -f blockchain.sh\n" "\n" ); fclose(bashFile); return 0; }
virtual int init( int argc, const char *argv[] ) { txID = 0; blkID = 0; inputID = 0; outputID = 0; static uint64_t sz = 32 * 1000 * 1000; outputMap.setEmptyKey(empty); outputMap.resize(sz); optparse::Values &values = parser.parse_args(argc, argv); cutoffBlock = values.get("atBlock"); info("dumping the blockchain ..."); txFile = fopen("transactions.txt", "w"); if(!txFile) sysErrFatal("couldn't open file txs.txt for writing\n"); blockFile = fopen("blocks.txt", "w"); if(!blockFile) sysErrFatal("couldn't open file blocks.txt for writing\n"); inputFile = fopen("inputs.txt", "w"); if(!inputFile) sysErrFatal("couldn't open file inputs.txt for writing\n"); outputFile = fopen("outputs.txt", "w"); if(!outputFile) sysErrFatal("couldn't open file outputs.txt for writing\n"); FILE *sqlFile = fopen("blockChain.sql", "w"); if(!sqlFile) sysErrFatal("couldn't open file blockChain.sql for writing\n"); fprintf( sqlFile, "\n" "DROP DATABASE IF EXISTS blockChain;\n" "CREATE DATABASE blockChain;\n" "USE blockChain;\n" "\n" "DROP TABLE IF EXISTS transactions;\n" "DROP TABLE IF EXISTS outputs;\n" "DROP TABLE IF EXISTS inputs;\n" "DROP TABLE IF EXISTS blocks;\n" "\n" "CREATE TABLE blocks(\n" " id BIGINT PRIMARY KEY,\n" " hash BINARY(32),\n" " time BIGINT\n" ");\n" "\n" "CREATE TABLE transactions(\n" " id BIGINT PRIMARY KEY,\n" " hash BINARY(32),\n" " blockID BIGINT\n" ");\n" "\n" "CREATE TABLE outputs(\n" " id BIGINT PRIMARY KEY,\n" " dstAddress CHAR(36),\n" " value BIGINT,\n" " txID BIGINT,\n" " offset INT\n" ");\n" "\n" "CREATE TABLE inputs(\n" " id BIGINT PRIMARY KEY,\n" " outputID BIGINT,\n" " txID BIGINT,\n" " offset INT\n" ");\n" "\n" ); fclose(sqlFile); FILE *bashFile = fopen("blockChain.bash", "w"); if(!bashFile) sysErrFatal("couldn't open file blockChain.bash for writing\n"); fprintf( bashFile, "\n" "#!/bin/bash\n" "\n" "echo\n" "\n" "echo 'wiping/re-creating DB blockChain ...'\n" "time mysql -u root -p -hlocalhost --password='' < blockChain.sql\n" "echo done\n" "echo\n" "\n" "for i in blocks inputs outputs transactions\n" "do\n" " echo Importing table $i ...\n" " time mysqlimport -u root -p -hlocalhost --password='' --lock-tables --use-threads=3 --local blockChain $i.txt\n" " echo done\n" " echo\n" "done\n" "\n" ); fclose(bashFile); return 0; }