void ClipboardChunk::send(synergy::IStream* stream, void* data) { ClipboardChunk* clipboardData = reinterpret_cast<ClipboardChunk*>(data); LOG((CLOG_DEBUG1 "sending clipboard chunk")); char* chunk = clipboardData->m_chunk; ClipboardID id = chunk[0]; UInt32* seq = reinterpret_cast<UInt32*>(&chunk[1]); UInt32 sequence = *seq; UInt8 mark = chunk[5]; String dataChunk(&chunk[6], clipboardData->m_dataSize); switch (mark) { case kDataStart: LOG((CLOG_DEBUG2 "sending clipboard chunk start: size=%s", dataChunk.c_str())); break; case kDataChunk: LOG((CLOG_DEBUG2 "sending clipboard chunk data: size=%i", dataChunk.size())); break; case kDataEnd: LOG((CLOG_DEBUG2 "sending clipboard finished")); break; } ProtocolUtil::writef(stream, kMsgDClipboard, id, sequence, mark, &dataChunk); }
bool LibSingleFileInterface::copyFiles(const QList<QVariant> & files, const QString & destinationDirectory, Kerfuffle::ExtractionOptions options) { Q_UNUSED(files) Q_UNUSED(options) QString outputFileName = destinationDirectory; if (!destinationDirectory.endsWith(QLatin1Char('/'))) { outputFileName += QLatin1Char('/'); } outputFileName += uncompressedFileName(); outputFileName = overwriteFileName(outputFileName); if (outputFileName.isEmpty()) { return true; } kDebug() << "Extracting to" << outputFileName; QFile outputFile(outputFileName); if (!outputFile.open(QIODevice::WriteOnly)) { kDebug() << "Failed to open output file" << outputFile.errorString(); emit error(i18nc("@info", "Ark could not extract <filename>%1</filename>.", outputFile.fileName())); return false; } QIODevice *device = KFilterDev::deviceForFile(filename(), m_mimeType, false); if (!device) { kDebug() << "Could not create KFilterDev"; emit error(i18nc("@info", "Ark could not open <filename>%1</filename> for extraction.", filename())); return false; } device->open(QIODevice::ReadOnly); qint64 bytesRead; QByteArray dataChunk(1024*16, '\0'); // 16Kb while (true) { bytesRead = device->read(dataChunk.data(), dataChunk.size()); if (bytesRead == -1) { emit error(i18nc("@info", "There was an error while reading <filename>%1</filename> during extraction.", filename())); break; } else if (bytesRead == 0) { break; } outputFile.write(dataChunk.data(), bytesRead); } delete device; return true; }
bool LoadSample(const char *name) { SampleLoaded = 0; if (!name) return true; EMUFILE_FILE inf(name,"rb"); if (inf.fail()) return false; //wav reading code adapted from AUDIERE (LGPL) // read the RIFF header u8 riff_id[4]; u32 riff_length; u8 riff_datatype[4]; inf.fread(riff_id, 4); inf.read_32LE(riff_length); inf.fread(riff_datatype, 4); if (inf.size() < 12 || memcmp(riff_id, "RIFF", 4) != 0 || riff_length == 0 || memcmp(riff_datatype, "WAVE", 4) != 0) { MessageBox(0,"not a valid RIFF WAVE file",0,0); return false; } if (!formatChunk(inf)) return false; if (!dataChunk(inf)) { MessageBox(0,"not a valid WAVE file. some unknown problem.",0,0); return false; } delete[] samplebuffer; samplebuffersize = (int)newWavData.size(); samplebuffer = new char[samplebuffersize]; memcpy(samplebuffer,newWavData.buf(),samplebuffersize); new(&newWavData) EMUFILE_MEMORY(); SampleLoaded=1; return true; }
bool DecompressLZOCompressedPackage(UPKReader *Package) { if (!Package->IsCompressed()) { _LogError("Package is not compressed!", "DecompressLZO"); return false; } if (!Package->IsLZOCompressed() && !Package->IsFullyCompressed()) { _LogError("Cannot decompress non-LZO compressed packages!", "DecompressLZO"); return false; } /// init lzo library int lzo_err; lzo_uint in_len; lzo_uint out_len; lzo_uint new_len; if (lzo_init() != LZO_E_OK) { _LogError("LZO library internal error: lzo_init() failed!", "DecompressLZO"); return false; } lzo_memset(in, 0, IN_LEN); std::stringstream decompressed_stream; unsigned int NumCompressedChunks = Package->Summary.NumCompressedChunks; if (Package->IsFullyCompressed()) { NumCompressedChunks = 1; } else { _LogDebug("Resetting package compression flags...", "DecompressLZO"); /// reset compression flags Package->Summary.CompressionFlags = 0; Package->Summary.PackageFlags ^= (uint32_t)UPackageFlags::Compressed; Package->Summary.NumCompressedChunks = 0; /// serialize package summary std::vector<char> sVect = Package->SerializeSummary(); decompressed_stream.write(sVect.data(), sVect.size()); } _LogDebug("Decompressing...", "DecompressLZO"); for (unsigned int i = 0; i < NumCompressedChunks; ++i) { if (Package->IsFullyCompressed()) { Package->UPKStream.seekg(0); } else { Package->UPKStream.seekg(Package->Summary.CompressedChunks[i].CompressedOffset); } _LogDebug("Decompressing chunk #" + ToString(i), "DecompressLZO"); uint32_t tag = 0; Package->UPKStream.read(reinterpret_cast<char*>(&tag), 4); if (tag != 0x9E2A83C1) { _LogError("Missing 0x9E2A83C1 signature!", "DecompressLZO"); return false; } uint32_t blockSize = 0; Package->UPKStream.read(reinterpret_cast<char*>(&blockSize), 4); if (blockSize != IN_LEN) { _LogError("Incorrect max block size!", "DecompressLZO"); return false; } std::vector<uint32_t> sizes(2); /// compressed/uncompressed pairs Package->UPKStream.read(reinterpret_cast<char*>(sizes.data()), 4 * sizes.size()); size_t dataSize = sizes[1]; /// uncompressed data chunk size unsigned numBlocks = (dataSize + blockSize - 1) / blockSize; _LogDebug("numBlocks = " + ToString(numBlocks), "DecompressLZO"); if (numBlocks < 1) { _LogError("Bad data!", "DecompressLZO"); return false; } sizes.resize((numBlocks + 1)*2); Package->UPKStream.read(reinterpret_cast<char*>(sizes.data()) + 8, 4 * sizes.size() - 8); for (unsigned i = 0; i <= numBlocks; ++i) { _LogDebug("Compressed size = " + ToString(sizes[i * 2]) + + "\tUncompressed size = " + ToString(sizes[i * 2 + 1]), "DecompressLZO"); } std::vector<unsigned char> dataChunk(dataSize); std::vector<unsigned char> compressedData(sizes[0]); Package->UPKStream.read(reinterpret_cast<char*>(compressedData.data()), compressedData.size()); size_t blockOffset = 0; size_t dataOffset = 0; for (unsigned i = 1; i <= numBlocks; ++i) { out_len = sizes[i * 2]; /// compressed size lzo_memcpy(out, compressedData.data() + blockOffset, out_len); in_len = sizes[i * 2 + 1]; /// uncompressed size new_len = in_len; lzo_err = lzo1x_decompress(out, out_len, in, &new_len, NULL); if (lzo_err == LZO_E_OK && new_len == in_len) { _LogDebug("Decompressed " + ToString(out_len) + " bytes back into " + ToString(in_len), "DecompressLZO"); } else { _LogError("LZO library internal error: decompression failed!", "DecompressLZO"); return false; } lzo_memcpy(dataChunk.data() + dataOffset, in, in_len); blockOffset += out_len; dataOffset += in_len; } decompressed_stream.write(reinterpret_cast<char*>(dataChunk.data()), dataSize); } _LogDebug("Package decompressed successfully.", "DecompressLZO"); Package->UPKStream.str(decompressed_stream.str()); return Package->ReadPackageHeader(); }
void JPEGLSCodec::compress(const FrameBuffer &frame) { const Box2i dataW = dataWindow(); const int width = (dataW.max.x - dataW.min.x + 1); const int height = (dataW.max.y - dataW.min.y + 1); const PixelType pixType = (_depth == JPEGLS_8 ? UINT8 : _depth == JPEGLS_10 ? UINT10 : _depth == JPEGLS_12 ? UINT12 : _depth == JPEGLS_16 ? UINT16 : UINT8); const size_t pixSize = PixelSize(pixType); const unsigned int bitDepth = PixelBits(pixType); const int numChannels = (_channels == JPEGLS_RGBA ? 4 : 3); const size_t tempPixelSize = (numChannels * pixSize); const size_t tempRowbytes = (tempPixelSize * width); const size_t tempBufSize = (tempRowbytes * height); DataChunk dataChunk(tempBufSize); char *tempBuffer = (char *)dataChunk.Data; assert(dataW.min.x == 0 && dataW.min.y == 0); FrameBuffer tempFrameBuffer(dataW); tempFrameBuffer.insert("R", Slice(pixType, &tempBuffer[0 * pixSize], tempPixelSize, tempRowbytes)); tempFrameBuffer.insert("G", Slice(pixType, &tempBuffer[1 * pixSize], tempPixelSize, tempRowbytes)); tempFrameBuffer.insert("B", Slice(pixType, &tempBuffer[2 * pixSize], tempPixelSize, tempRowbytes)); if(_channels == JPEGLS_RGBA) tempFrameBuffer.insert("A", Slice(pixType, &tempBuffer[3 * pixSize], tempPixelSize, tempRowbytes)); tempFrameBuffer.copyFromFrame(frame); JlsParameters params = JlsParameters(); params.width = width; params.height = height; params.bitspersample = bitDepth; //params.bytesperline = (tempRowbytes / 3); params.components = numChannels; params.allowedlossyerror = 0; // always lossless params.ilv = ILV_SAMPLE; params.colorTransform = COLORXFORM_NONE; //params.outputBgr = 0; /* params.custom.MAXVAL = 255; params.custom.T1 = 0; params.custom.T2 = 0; params.custom.T3 = 0; params.custom.RESET = 1; params.jfif.Ver = 123; params.jfif.units = 0; params.jfif.XDensity = 72; params.jfif.YDensity = 72; params.jfif.Xthumb = 0; params.jfif.Ythumb = 0; params.jfif.pdataThumbnail = NULL; */ ByteStreamInfo inStream = FromByteArray(dataChunk.Data, dataChunk.Size); DataChunkPtr outDataChunk = new DataChunk(tempBufSize); size_t bytesWritten = 0; JLS_ERROR err = OK; do { ByteStreamInfo outStream = FromByteArray(outDataChunk->Data, outDataChunk->Size); err = JpegLsEncodeStream(outStream, &bytesWritten, inStream, ¶ms); if(err == CompressedBufferTooSmall) { outDataChunk->Resize(2 * outDataChunk->Size, false); } }while(err == CompressedBufferTooSmall); assert(err != TooMuchCompressedData); if(err == OK) { assert(bytesWritten > 0); outDataChunk->Resize(bytesWritten); storeData(outDataChunk); } else throw MoxMxf::ArgExc("JPEG-LS compression error"); }
void JPEGCodec::compress(const FrameBuffer &frame) { struct jpeg_error_mgr jerr; jerr.error_exit = my_error_exit; jerr.emit_message = my_emit_message; jerr.output_message = my_output_message; jerr.format_message = my_format_message; jerr.reset_error_mgr = my_reset_error_mgr; jerr.trace_level = 0; jerr.num_warnings = 0; jerr.msg_code = 0; jerr.jpeg_message_table = jpeg_std_message_table; jerr.last_jpeg_message = (int) JMSG_LASTMSGCODE - 1; jerr.addon_message_table = NULL; jerr.first_addon_message = 0; jerr.last_jpeg_message = 0; struct jpeg_compress_struct cinfo; cinfo.err = &jerr; jpeg_create_compress(&cinfo); my_destination_mgr mgr; mgr.outfile = new MemoryFile; mgr.buffer = NULL; mgr.bufferSize = 0; mgr.pub.init_destination = my_init_destination; mgr.pub.empty_output_buffer = my_empty_output_buffer; mgr.pub.term_destination = my_term_destination; cinfo.dest = (jpeg_destination_mgr *)&mgr; const Box2i dataW = dataWindow(); const int width = (dataW.max.x - dataW.min.x + 1); const int height = (dataW.max.y - dataW.min.y + 1); cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, _quality, TRUE); bool success = true; try { jpeg_start_compress(&cinfo, TRUE); const size_t tempPixelSize = (3 * PixelSize(UINT8)); const size_t tempRowbytes = (tempPixelSize * width); const size_t tempBufSize = (tempRowbytes * height); DataChunk dataChunk(tempBufSize); char *tempBuffer = (char *)dataChunk.Data; FrameBuffer tempFrameBuffer(dataW); tempFrameBuffer.insert("R", Slice(UINT8, &tempBuffer[0], tempPixelSize, tempRowbytes)); tempFrameBuffer.insert("G", Slice(UINT8, &tempBuffer[1], tempPixelSize, tempRowbytes)); tempFrameBuffer.insert("B", Slice(UINT8, &tempBuffer[2], tempPixelSize, tempRowbytes)); tempFrameBuffer.copyFromFrame(frame); JSAMPARRAY scanlines = (JSAMPARRAY)malloc(height * sizeof(JSAMPROW)); if(scanlines == NULL) throw MoxMxf::NullExc("out of memory"); for(int y=0; y < height; y++) { scanlines[y] = (JSAMPROW)(tempBuffer + (y * tempRowbytes)); } const JDIMENSION linesWrote = jpeg_write_scanlines(&cinfo, scanlines, height); assert(linesWrote == height); jpeg_finish_compress(&cinfo); assert(jerr.msg_code == 0); storeData( mgr.outfile->getDataChunk() ); free(scanlines); } catch(...) { success = false; } jpeg_destroy_compress(&cinfo); if(mgr.buffer != NULL) free(mgr.buffer); delete mgr.outfile; if(!success) throw MoxMxf::ArgExc("JPEG compression error"); }