size_t wxChmInputStream::OnSysRead(void *buffer, size_t bufsize) { if ( m_pos >= m_size ) { m_lasterror = wxSTREAM_EOF; return 0; } m_lasterror = wxSTREAM_NO_ERROR; // If the rest to read from the stream is less // than the buffer size, then only read the rest if ( m_pos + bufsize > m_size ) bufsize = m_size - m_pos; if (m_contentStream->SeekI(m_pos) == wxInvalidOffset) { m_lasterror = wxSTREAM_EOF; return 0; } size_t read = m_contentStream->Read(buffer, bufsize).LastRead(); m_pos += read; if (m_contentStream->SeekI(m_pos) == wxInvalidOffset) { m_lasterror = wxSTREAM_READ_ERROR; return 0; } if (read != bufsize) m_lasterror = m_contentStream->GetLastError(); return read; }
bool wxSVGHandler::DoCanRead(wxInputStream& stream) { unsigned char hdr[5]; if (!stream.Read(hdr, WXSIZEOF(hdr))) // it's ok to modify the stream position here return false; return hdr[0] == '<' && hdr[1] == '?' && hdr[2] == 'x' && hdr[3] == 'm' && hdr[4] == 'l'; // <?xml }
bool wxPNGHandler::DoCanRead( wxInputStream& stream ) { unsigned char hdr[4]; if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) return false; return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0; }
bool wxTarHeaderBlock::Read(wxInputStream& in) { bool ok = true; for (int id = 0; id < TAR_NUMFIELDS && ok; id++) ok = in.Read(Get(id), Len(id)).LastRead() == Len(id); return ok; }
bool wxJPEGHandler::DoCanRead( wxInputStream& stream ) { unsigned char hdr[2]; if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) return false; return hdr[0] == 0xFF && hdr[1] == 0xD8; }
bool wxPNGHandler::DoCanRead( wxInputStream& stream ) { unsigned char hdr[4]; if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) // it's ok to modify the stream position here return false; return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0; }
bool wxJPEGHandler::DoCanRead( wxInputStream& stream ) { unsigned char hdr[2]; if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) // it's ok to modify the stream position here return false; return hdr[0] == 0xFF && hdr[1] == 0xD8; }
/** @brief Read an int from an input stream * * @param in wxInputStream& * @param out_Int int& * @return bool * */ static bool ReadInt( wxInputStream& in, int& out_Int ) { int val = 0; if (!in.CanRead()) return false; in.Read(&val, sizeof(val)); out_Int = val; return true; }
/** @brief Read a long long from an input stream * * @param in wxInputStream& * @param out_LongLong long long& * @return bool * */ static bool ReadLongLong( wxInputStream& in, long long& out_LongLong ) { long long val = 0; if (!in.CanRead()) return false; in.Read(&val, sizeof(val)); out_LongLong = val; return true; }
void MyPipeFrame::DoGetFromStream(wxTextCtrl *text, wxInputStream& in) { while ( in.CanRead() ) { char buffer[4096]; buffer[in.Read(buffer, WXSIZEOF(buffer) - 1).LastRead()] = '\0'; text->AppendText(buffer); } }
bool HexView::Load(wxInputStream& iStr) { wxFileOffset strSize = iStr.SeekI(0, wxFromEnd); iStr.SeekI(0); unsigned char* data = new unsigned char[strSize]; iStr.Read(data, strSize); Load(data, strSize); delete [] data; return true; }
// recognize the MXF JPEG 2000 starting box bool wxMXFHandler::DoCanRead( wxInputStream& stream ) { unsigned char hdr[4]; if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) return false; return (hdr[0] == 0x06 && hdr[1] == 0x0E && hdr[2] == 0x2B && hdr[3] == 0x34); }
wxString FbImportBook::CalcMd5(wxInputStream& stream) { const size_t BUFSIZE = 1024; unsigned char buf[BUFSIZE]; md5_context md5; md5_starts( &md5 ); bool eof; do { size_t len = stream.Read(buf, BUFSIZE).LastRead(); eof = (len < BUFSIZE); md5_update( &md5, buf, (int) len ); } while (!eof); return Md5(md5); }
// recognize the JPEG 2000 starting box bool wxJP2Handler::DoCanRead( wxInputStream& stream ) { unsigned char hdr[23]; if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) return false; return (hdr[0] == 0x00 && hdr[1] == 0x00 && hdr[2] == 0x00 && hdr[3] == 0x0C && hdr[4] == 0x6A && hdr[5] == 0x50 && hdr[6] == 0x20 && hdr[7] == 0x20 && hdr[20] == 0x6A && hdr[21] == 0x70 && hdr[22] == 0x32); }
/* static */ bool IStateStore::LoadString(wxInputStream& input, wxString& txt) { wxInt32 len = 0; bool ret = LoadSimpleType<wxInt32>(input, len); if (ret) { len *= sizeof(wchar_t); wchar_t* buf = (wchar_t*) malloc(len); input.Read(buf, len); if (CheckLastRead(input, len)) { txt = wxString(buf, len / sizeof(wchar_t)); ret = true; } free(buf); } return ret; }
size_t wxChmInputStream::OnSysRead(void *buffer, size_t bufsize) { if ( m_pos >= m_size ) { m_lasterror = wxSTREAM_EOF; return 0; } m_lasterror = wxSTREAM_NO_ERROR; // If the rest to read from the stream is less // than the buffer size, than only read the rest if ( m_pos + bufsize > m_size ) bufsize = m_size - m_pos; m_contentStream->SeekI(m_pos); m_contentStream->Read(buffer, bufsize); m_pos +=bufsize; m_contentStream->SeekI(m_pos); return bufsize; }
/** @brief Read a string from an input stream * * @param in wxInputStream& * @param out_String wxString& * @return bool * */ static bool ReadString( wxInputStream& in, wxString& out_String ) { int len; if (!ReadInt(in, len)) return false; if (len == 0) { out_String = out_String.Truncate(0); return true; } if (!in.CanRead()) return false; char buffer[len + 1]; in.Read( buffer, len ); buffer[len] = '\0'; out_String = wxString::FromUTF8(buffer); return true; }
void wxMemoryInputStream::InitFromStream(wxInputStream& stream, wxFileOffset lenFile) { if ( lenFile == wxInvalidOffset ) lenFile = stream.GetLength(); if ( lenFile == wxInvalidOffset ) { m_i_streambuf = NULL; m_lasterror = wxSTREAM_EOF; return; } const size_t len = wx_truncate_cast(size_t, lenFile); wxASSERT_MSG( (wxFileOffset)len == lenFile, wxT("huge files not supported") ); m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read); m_i_streambuf->SetBufferIO(len); // create buffer stream.Read(m_i_streambuf->GetBufferStart(), len); m_i_streambuf->SetIntPosition(0); // seek to start pos m_i_streambuf->Fixed(true); m_length = stream.LastRead(); }
bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height, int bpp, int ncolors, int comp, wxFileOffset bmpOffset, wxInputStream& stream, bool verbose, bool IsBmp, bool hasPalette) { wxInt32 aDword, rmask = 0, gmask = 0, bmask = 0, amask = 0; int rshift = 0, gshift = 0, bshift = 0, ashift = 0; int rbits = 0, gbits = 0, bbits = 0; wxInt32 dbuf[4]; wxInt8 bbuf[4]; wxUint8 aByte; wxUint16 aWord; // allocate space for palette if needed: BMPPalette *cmap; if ( bpp < 16 ) { cmap = new BMPPalette[ncolors]; if ( !cmap ) { if (verbose) { wxLogError(_("BMP: Couldn't allocate memory.")); } return false; } } else // no palette { cmap = NULL; } wxON_BLOCK_EXIT1(&BMPPalette::Free, cmap); // destroy existing here instead of: image->Destroy(); image->Create(width, height); unsigned char *ptr = image->GetData(); if ( !ptr ) { if ( verbose ) { wxLogError( _("BMP: Couldn't allocate memory.") ); } return false; } unsigned char *alpha; if ( bpp == 32 ) { // tell the image to allocate an alpha buffer image->SetAlpha(); alpha = image->GetAlpha(); if ( !alpha ) { if ( verbose ) { wxLogError(_("BMP: Couldn't allocate memory.")); } return false; } } else // no alpha { alpha = NULL; } // Reading the palette, if it exists: if ( bpp < 16 && ncolors != 0 ) { unsigned char* r = new unsigned char[ncolors]; unsigned char* g = new unsigned char[ncolors]; unsigned char* b = new unsigned char[ncolors]; for (int j = 0; j < ncolors; j++) { if (hasPalette) { stream.Read(bbuf, 4); cmap[j].b = bbuf[0]; cmap[j].g = bbuf[1]; cmap[j].r = bbuf[2]; r[j] = cmap[j].r; g[j] = cmap[j].g; b[j] = cmap[j].b; } else { //used in reading .ico file mask r[j] = cmap[j].r = g[j] = cmap[j].g = b[j] = cmap[j].b = ( j ? 255 : 0 ); } } #if wxUSE_PALETTE // Set the palette for the wxImage image->SetPalette(wxPalette(ncolors, r, g, b)); #endif // wxUSE_PALETTE delete[] r; delete[] g; delete[] b; } else if ( bpp == 16 || bpp == 32 ) { if ( comp == BI_BITFIELDS ) { int bit; stream.Read(dbuf, 4 * 3); rmask = wxINT32_SWAP_ON_BE(dbuf[0]); gmask = wxINT32_SWAP_ON_BE(dbuf[1]); bmask = wxINT32_SWAP_ON_BE(dbuf[2]); // find shift amount (Least significant bit of mask) for (bit = bpp-1; bit>=0; bit--) { if (bmask & (1 << bit)) bshift = bit; if (gmask & (1 << bit)) gshift = bit; if (rmask & (1 << bit)) rshift = bit; } // Find number of bits in mask (MSB-LSB+1) for (bit = 0; bit < bpp; bit++) { if (bmask & (1 << bit)) bbits = bit-bshift+1; if (gmask & (1 << bit)) gbits = bit-gshift+1; if (rmask & (1 << bit)) rbits = bit-rshift+1; } } else if ( bpp == 16 ) { rmask = 0x7C00; gmask = 0x03E0; bmask = 0x001F; rshift = 10; gshift = 5; bshift = 0; rbits = 5; gbits = 5; bbits = 5; } else if ( bpp == 32 ) { rmask = 0x00FF0000; gmask = 0x0000FF00; bmask = 0x000000FF; amask = 0xFF000000; ashift = 24; rshift = 16; gshift = 8; bshift = 0; rbits = 8; gbits = 8; bbits = 8; } } /* * Reading the image data */ if ( IsBmp ) { // NOTE: seeking a positive amount in wxFromCurrent mode allows us to // load even non-seekable streams (see wxInputStream::SeekI docs)! const wxFileOffset pos = stream.TellI(); if (pos != wxInvalidOffset && bmpOffset > pos) if (stream.SeekI(bmpOffset - pos, wxFromCurrent) == wxInvalidOffset) return false; //else: icon, just carry on } unsigned char *data = ptr; /* set the whole image to the background color */ if ( bpp < 16 && (comp == BI_RLE4 || comp == BI_RLE8) ) { for (int i = 0; i < width * height; i++) { *ptr++ = cmap[0].r; *ptr++ = cmap[0].g; *ptr++ = cmap[0].b; } ptr = data; } int linesize = ((width * bpp + 31) / 32) * 4; /* BMPs are stored upside down */ for ( int line = (height - 1); line >= 0; line-- ) { int linepos = 0; for ( int column = 0; column < width ; ) { if ( bpp < 16 ) { linepos++; aByte = stream.GetC(); if ( bpp == 1 ) { for (int bit = 0; bit < 8 && column < width; bit++) { int index = ((aByte & (0x80 >> bit)) ? 1 : 0); ptr[poffset] = cmap[index].r; ptr[poffset + 1] = cmap[index].g; ptr[poffset + 2] = cmap[index].b; column++; } } else if ( bpp == 4 ) { if ( comp == BI_RLE4 ) { wxUint8 first; first = aByte; aByte = stream.GetC(); if ( first == 0 ) { if ( aByte == 0 ) { if ( column > 0 ) column = width; } else if ( aByte == 1 ) { column = width; line = -1; } else if ( aByte == 2 ) { aByte = stream.GetC(); column += aByte; linepos = column * bpp / 4; aByte = stream.GetC(); line -= aByte; // upside down } else { int absolute = aByte; wxUint8 nibble[2] ; int readBytes = 0 ; for (int k = 0; k < absolute; k++) { if ( !(k % 2 ) ) { ++readBytes ; aByte = stream.GetC(); nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ; nibble[1] = (wxUint8)( aByte & 0x0F ) ; } ptr[poffset ] = cmap[nibble[k%2]].r; ptr[poffset + 1] = cmap[nibble[k%2]].g; ptr[poffset + 2] = cmap[nibble[k%2]].b; column++; if ( k % 2 ) linepos++; } if ( readBytes & 0x01 ) aByte = stream.GetC(); } } else { wxUint8 nibble[2] ; nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ; nibble[1] = (wxUint8)( aByte & 0x0F ) ; for ( int l = 0; l < first && column < width; l++ ) { ptr[poffset ] = cmap[nibble[l%2]].r; ptr[poffset + 1] = cmap[nibble[l%2]].g; ptr[poffset + 2] = cmap[nibble[l%2]].b; column++; if ( l % 2 ) linepos++; } } }
/** * Calculates the checksums from the given stream. * * @param in Input stream from which the data will be extracted to * compute the checksum. The data are extracted until the end * of the stream is reached. * @param checksums Array of checksums to calculate. * @param sumValues The calculated values of the checksums from the input * stream. The array is erased first before adding results. * On success <CODE>ArrayChecksum.GetCount() == sumValues.GetCount()</CODE>, * on failure, <CODE>sumValues</CODE> should be empty. * @return <UL> * <LI><CODE>Ok</CODE> if the checksum has been successfully calculated.</Li> * <LI><CODE>ReadError</CODE> if a read error has occured.</LI> * <LI><CODE>Canceled</CODE> if the user has canceled the calculation.</LI> * </UL> */ ChecksumCalculator::State ChecksumCalculator::calculate(wxInputStream& in, const ArrayChecksum& checksums, wxArrayString& sumValues) { // Check if the input stream is valid. if (!in.IsOk()) return ReadError; // Check if at least a checksum instance is OK. bool aInstanceOK = false; size_t i = 0; size_t s = checksums.GetCount(); while (!aInstanceOK && i < s) if (checksums[i] != NULL) aInstanceOK = true; else i++; if (!aInstanceOK) return ReadError; // Initializes the buffer. const size_t bufSize = getBufferSize(); wxByte* buff = new wxByte[bufSize]; // Calculating the checksum. ChecksumProgress* p = getChecksumProgress(); bool canceled = false; size_t read; wxStreamError lastError = wxSTREAM_NO_ERROR; s = checksums.GetCount(); for (i = 0; i < s; i++) if (checksums[i] != NULL) checksums[i]->reset(); while (!canceled && !in.Eof() && lastError == wxSTREAM_NO_ERROR) { in.Read(buff, bufSize); read = in.LastRead(); if (read > 0 && read <= bufSize) { for (i = 0; i < s; i++) if (checksums[i] != NULL) checksums[i]->update(buff, read); if (p != NULL) p->update(read, canceled); } lastError = in.GetLastError(); } // Cleans-up the memory delete[] buff; if (canceled) return CanceledByUser; if (lastError != wxSTREAM_NO_ERROR && lastError != wxSTREAM_EOF) return ReadError; sumValues.Empty(); for (i = 0; i < s; i++) if (checksums[i] != NULL) sumValues.Add(checksums[i]->getValue()); else sumValues.Add(wxEmptyString); return Ok; }
wxStreamError wxBackingFileImpl::ReadAt(wxFileOffset pos, void *buffer, size_t *size) { size_t reqestedSize = *size; *size = 0; // size1 is the number of bytes it will read directly from the backing // file. size2 is any remaining bytes not yet backed, these are returned // from the buffer or read from the parent stream. size_t size1, size2; if (pos + reqestedSize <= m_filelen + size_t(0)) { size1 = reqestedSize; size2 = 0; } else if (pos < m_filelen) { size1 = size_t(m_filelen - pos); size2 = reqestedSize - size1; } else { size1 = 0; size2 = reqestedSize; } if (pos < 0) return wxSTREAM_READ_ERROR; // read the backing file if (size1) { if (m_file.Seek(pos) == wxBadSeek) return wxSTREAM_READ_ERROR; ssize_t n = m_file.Read(buffer, size1); if (n > 0) { *size = n; pos += n; } if (*size < size1) return wxSTREAM_READ_ERROR; } // read from the buffer or parent stream if (size2) { while (*size < reqestedSize) { // if pos is further ahead than the parent has been read so far, // then read forward in the parent stream while (pos - m_filelen + size_t(0) >= m_buflen) { // if the parent is small enough, don't use a backing file // just the buffer memory if (!m_stream && m_filelen == 0) return m_parenterror; // before refilling the buffer write out the current buffer // to the backing file if there is anything in it if (m_buflen) { if (!m_file.IsOpened()) if (!wxCreateTempFile(m_prefix, &m_file, &m_filename)) return wxSTREAM_READ_ERROR; if (m_file.Seek(m_filelen) == wxBadSeek) return wxSTREAM_READ_ERROR; size_t count = m_file.Write(m_buf, m_buflen); m_filelen += count; if (count < m_buflen) { wxDELETE(m_stream); if (count > 0) { wxDELETEA(m_buf); m_buflen = 0; } m_parenterror = wxSTREAM_READ_ERROR; return m_parenterror; } m_buflen = 0; if (!m_stream) { wxDELETEA(m_buf); } } if (!m_stream) return m_parenterror; // refill buffer m_buflen = m_stream->Read(m_buf, m_bufsize).LastRead(); if (m_buflen < m_bufsize) { m_parenterror = m_stream->GetLastError(); if (m_parenterror == wxSTREAM_NO_ERROR) m_parenterror = wxSTREAM_EOF; wxDELETE(m_stream); } } // copy to the user's buffer size_t start = size_t(pos - m_filelen); size_t len = wxMin(m_buflen - start, reqestedSize - *size); memcpy((char*)buffer + *size, m_buf + start, len); *size += len; pos += len; } } return wxSTREAM_NO_ERROR; }
bool wxSHA384::Hash(wxInputStream &input, wxSHA384Hash &hash) { // Get and verify the length of the stream m_length = input.GetLength(); if((m_length == wxInvalidOffset) || (m_length == 0)) return false; // Initialize H[0,..,7] hash.H[0] = SHA384_INIT_0; hash.H[1] = SHA384_INIT_1; hash.H[2] = SHA384_INIT_2; hash.H[3] = SHA384_INIT_3; hash.H[4] = SHA384_INIT_4; hash.H[5] = SHA384_INIT_5; H6 = SHA384_INIT_6; H7 = SHA384_INIT_7; // Initialize data variables m_readed = 0; m_N = (m_length / SHA384_DATA_PAD_SPACE_1) + 1; for(i = 0; i < m_N; i++) { // Initialize temporary HASH value a = hash.H[0]; b = hash.H[1]; c = hash.H[2]; d = hash.H[3]; e = hash.H[4]; f = hash.H[5]; g = H6; h = H7; // First step SHA384 [0,..,15] size_t t; for(t = 0; t < 16; t++) { // Read data to W[t] if((input.Read(&W[t], GetWSize()).LastRead() != GetWSize()) && ((m_length - m_readed) != input.LastRead())) return false; W[t] = wxUINT64_SWAP_ON_LE(W[t].GetValue()); // Wt = Mt(i) M(t, input.LastRead()); m_readed += input.LastRead(); ComputeTempHash(t); } // Second step SHA384 [16,..,79] for(; t < 80; t++) { W16_79(t); ComputeTempHash(t); } // Update HASH hash.H[0] += a; hash.H[1] += b; hash.H[2] += c; hash.H[3] += d; hash.H[4] += e; hash.H[5] += f; H6 += g; H7 += h; } return true; }
bool wxAnimation::Load(wxInputStream &stream, wxAnimationType type) { UnRef(); char anim_type[12]; switch (type) { case wxANIMATION_TYPE_GIF: strcpy(anim_type, "gif"); break; case wxANIMATION_TYPE_ANI: strcpy(anim_type, "ani"); break; default: anim_type[0] = '\0'; break; } // create a GdkPixbufLoader GError *error = NULL; GdkPixbufLoader *loader; if (type != wxANIMATION_TYPE_INVALID && type != wxANIMATION_TYPE_ANY) loader = gdk_pixbuf_loader_new_with_type(anim_type, &error); else loader = gdk_pixbuf_loader_new(); if (!loader || error != NULL) // even if the loader was allocated, an error could have happened { wxLogDebug(wxT("Could not create the loader for '%s' animation type: %s"), anim_type, error->message); return false; } // connect to loader signals g_signal_connect(loader, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated), this); guchar buf[2048]; bool data_written = false; while (stream.IsOk()) { // read a chunk of data if (!stream.Read(buf, sizeof(buf)) && stream.GetLastError() != wxSTREAM_EOF) // EOF is OK for now { // gdk_pixbuf_loader_close wants the GError == NULL gdk_pixbuf_loader_close(loader, NULL); return false; } // fetch all data into the loader if (!gdk_pixbuf_loader_write(loader, buf, stream.LastRead(), &error)) { wxLogDebug(wxT("Could not write to the loader: %s"), error->message); // gdk_pixbuf_loader_close wants the GError == NULL gdk_pixbuf_loader_close(loader, NULL); return false; } data_written = true; } if (!data_written) { wxLogDebug("Could not read data from the stream..."); return false; } // load complete: gdk_pixbuf_loader_close will now check if the data we // wrote inside the pixbuf loader does make sense and will give an error // if it doesn't (because of a truncated file, corrupted data or whatelse) if (!gdk_pixbuf_loader_close(loader, &error)) { wxLogDebug(wxT("Could not close the loader: %s"), error->message); return false; } // wait until we get the last area_updated signal return data_written; }
/* internal mini-search for a box signature */ int jpeg2000_file_parse(wxInputStream& stream, unsigned long int filepoint, unsigned long int filelimit, int level, char *scansign, unsigned long int *scanpoint) { unsigned long int LBox = 0x00000000; char TBox[5] = "\0\0\0\0"; int8byte XLBox = 0x0000000000000000; unsigned long int box_length = 0; int last_box = 0, box_num = 0; int box_type = ANY_BOX; unsigned char fourbytes[4]; int box_number = 0; /* cycle all over the file */ box_num = 0; last_box = 0; while (!last_box) { /* do not exceed file limit */ if (filepoint >= filelimit) return (0); /* seek on file */ if (stream.SeekI(filepoint, wxFromStart) == wxInvalidOffset) return (-1); /* read the mandatory LBox, 4 bytes */ if (!stream.Read(fourbytes, 4)) { wxLogError(wxT("Problem reading LBox from the file (file ended?)")); return -1; }; LBox = STREAM_TO_UINT32(fourbytes, 0); /* read the mandatory TBox, 4 bytes */ if (!stream.Read(TBox, 4)) { wxLogError(wxT("Problem reading TBox from the file (file ended?)")); return -1; }; /* look if scansign is got */ if ((scansign != NULL) && (memcmp(TBox, scansign, 4) == 0)) { /* hack/exploit */ // stop as soon as you find the level-th codebox if (box_number == level) { memcpy(scansign, " ", 4); *scanpoint = filepoint; return (0); } else box_number++; }; /* determine the box type */ for (box_type = JP_BOX; box_type < UNK_BOX; box_type++) if (memcmp(TBox, jpeg2000box[box_type].value, 4) == 0) break; /* read the optional XLBox, 8 bytes */ if (LBox == 1) { if (!stream.Read(&XLBox, 8)) { wxLogError(wxT("Problem reading XLBox from the file (file ended?)")); return -1; }; box_length = (unsigned long int) BYTE_SWAP8(XLBox); } else if (LBox == 0x00000000) { /* last box in file */ last_box = 1; box_length = filelimit - filepoint; } else box_length = LBox; /* go deep in the box */ jpeg2000_box_handler_function((jpeg2000boxtype) box_type, stream, (LBox == 1) ? (filepoint + 16) : (filepoint + 8), filepoint + box_length, level, scansign, scanpoint); /* if it's a superbox go inside it */ if (jpeg2000box[box_type].sbox) jpeg2000_file_parse(stream, (LBox == 1) ? (filepoint + 16) : (filepoint + 8), filepoint + box_length, level, scansign, scanpoint); /* increment box number and filepoint*/ box_num++; filepoint += box_length; }; /* all good */ return (0); }
// load the jpeg2000 file format bool wxJPEG2000Handler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose, int index) { opj_dparameters_t parameters; /* decompression parameters */ opj_event_mgr_t event_mgr; /* event manager */ opj_image_t *opjimage = NULL; unsigned char *src = NULL; unsigned char *ptr; int file_length, jp2c_point, jp2h_point; unsigned long int jp2hboxlen, jp2cboxlen; opj_codestream_info_t cstr_info; /* Codestream information structure */ unsigned char hdr[24]; int jpfamform; // destroy the image image->Destroy(); /* read the beginning of the file to check the type */ if (!stream.Read(hdr, WXSIZEOF(hdr))) return false; if ((jpfamform = jpeg2000familytype(hdr, WXSIZEOF(hdr))) < 0) return false; stream.SeekI(0, wxFromStart); /* handle to a decompressor */ opj_dinfo_t* dinfo = NULL; opj_cio_t *cio = NULL; /* configure the event callbacks */ memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); event_mgr.error_handler = jpeg2000_error_callback; event_mgr.warning_handler = jpeg2000_warning_callback; event_mgr.info_handler = jpeg2000_info_callback; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* prepare parameters */ strncpy(parameters.infile, "", sizeof(parameters.infile) - 1); strncpy(parameters.outfile, "", sizeof(parameters.outfile) - 1); parameters.decod_format = jpfamform; parameters.cod_format = BMP_DFMT; if (m_reducefactor) parameters.cp_reduce = m_reducefactor; if (m_qualitylayers) parameters.cp_layer = m_qualitylayers; /*if (n_components) parameters. = n_components;*/ /* JPWL only */ #ifdef USE_JPWL parameters.jpwl_exp_comps = m_expcomps; parameters.jpwl_max_tiles = m_maxtiles; parameters.jpwl_correct = m_enablejpwl; #endif /* USE_JPWL */ /* get a decoder handle */ if (jpfamform == JP2_CFMT || jpfamform == MJ2_CFMT) dinfo = opj_create_decompress(CODEC_JP2); else if (jpfamform == J2K_CFMT) dinfo = opj_create_decompress(CODEC_J2K); else return false; /* find length of the stream */ stream.SeekI(0, wxFromEnd); file_length = (int) stream.TellI(); /* it's a movie */ if (jpfamform == MJ2_CFMT) { /* search for the first codestream box and the movie header box */ jp2c_point = searchjpeg2000c(stream, file_length, m_framenum); jp2h_point = searchjpeg2000headerbox(stream, file_length); // read the jp2h box and store it stream.SeekI(jp2h_point, wxFromStart); stream.Read(&jp2hboxlen, sizeof(unsigned long int)); jp2hboxlen = BYTE_SWAP4(jp2hboxlen); // read the jp2c box and store it stream.SeekI(jp2c_point, wxFromStart); stream.Read(&jp2cboxlen, sizeof(unsigned long int)); jp2cboxlen = BYTE_SWAP4(jp2cboxlen); // malloc memory source src = (unsigned char *) malloc(jpeg2000headSIZE + jp2hboxlen + jp2cboxlen); // copy the jP and ftyp memcpy(src, jpeg2000head, jpeg2000headSIZE); // copy the jp2h stream.SeekI(jp2h_point, wxFromStart); stream.Read(&src[jpeg2000headSIZE], jp2hboxlen); // copy the jp2c stream.SeekI(jp2c_point, wxFromStart); stream.Read(&src[jpeg2000headSIZE + jp2hboxlen], jp2cboxlen); } else if (jpfamform == JP2_CFMT || jpfamform == J2K_CFMT) { /* It's a plain image */ /* get data */ stream.SeekI(0, wxFromStart); src = (unsigned char *) malloc(file_length); stream.Read(src, file_length); } else return false; /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ if (jpfamform == MJ2_CFMT) cio = opj_cio_open((opj_common_ptr)dinfo, src, jpeg2000headSIZE + jp2hboxlen + jp2cboxlen); else if (jpfamform == JP2_CFMT || jpfamform == J2K_CFMT) cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length); else { free(src); return false; } /* decode the stream and fill the image structure */ opjimage = opj_decode_with_info(dinfo, cio, &cstr_info); if (!opjimage) { wxMutexGuiEnter(); wxLogError(wxT("JPEG 2000 failed to decode image!")); wxMutexGuiLeave(); opj_destroy_decompress(dinfo); opj_cio_close(cio); free(src); return false; } /* close the byte stream */ opj_cio_close(cio); /* - At this point, we have the structure "opjimage" that is filled with decompressed data, as processed by the OpenJPEG decompression engine - We need to fill the class "image" with the proper pixel sample values */ { int shiftbpp; int c, tempcomps; // check components number if (m_components > opjimage->numcomps) m_components = opjimage->numcomps; // check image depth (only on the first one, for now) if (m_components) shiftbpp = opjimage->comps[m_components - 1].prec - 8; else shiftbpp = opjimage->comps[0].prec - 8; // prepare image size if (m_components) image->Create(opjimage->comps[m_components - 1].w, opjimage->comps[m_components - 1].h, true); else image->Create(opjimage->comps[0].w, opjimage->comps[0].h, true); // access image raw data image->SetMask(false); ptr = image->GetData(); // workaround for components different from 1 or 3 if ((opjimage->numcomps != 1) && (opjimage->numcomps != 3)) { #ifndef __WXGTK__ wxMutexGuiEnter(); #endif /* __WXGTK__ */ wxLogMessage(wxT("JPEG2000: weird number of components")); #ifndef __WXGTK__ wxMutexGuiLeave(); #endif /* __WXGTK__ */ tempcomps = 1; } else tempcomps = opjimage->numcomps; // workaround for subsampled components for (c = 1; c < tempcomps; c++) { if ((opjimage->comps[c].w != opjimage->comps[c - 1].w) || (opjimage->comps[c].h != opjimage->comps[c - 1].h)) { tempcomps = 1; break; } } // workaround for different precision components for (c = 1; c < tempcomps; c++) { if (opjimage->comps[c].bpp != opjimage->comps[c - 1].bpp) { tempcomps = 1; break; } } // only one component selected if (m_components) tempcomps = 1; // RGB color picture if (tempcomps == 3) { int row, col; int *r = opjimage->comps[0].data; int *g = opjimage->comps[1].data; int *b = opjimage->comps[2].data; if (shiftbpp > 0) { for (row = 0; row < opjimage->comps[0].h; row++) { for (col = 0; col < opjimage->comps[0].w; col++) { *(ptr++) = (*(r++)) >> shiftbpp; *(ptr++) = (*(g++)) >> shiftbpp; *(ptr++) = (*(b++)) >> shiftbpp; } } } else if (shiftbpp < 0) { for (row = 0; row < opjimage->comps[0].h; row++) { for (col = 0; col < opjimage->comps[0].w; col++) { *(ptr++) = (*(r++)) << -shiftbpp; *(ptr++) = (*(g++)) << -shiftbpp; *(ptr++) = (*(b++)) << -shiftbpp; } } } else { for (row = 0; row < opjimage->comps[0].h; row++) { for (col = 0; col < opjimage->comps[0].w; col++) { *(ptr++) = *(r++); *(ptr++) = *(g++); *(ptr++) = *(b++); } } } }
bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding, int flags) { #if wxUSE_UNICODE (void)encoding; #else m_encoding = encoding; #endif const size_t BUFSIZE = 1024; char buf[BUFSIZE]; wxXmlParsingContext ctx; bool done; XML_Parser parser = XML_ParserCreate(NULL); ctx.root = ctx.node = NULL; ctx.encoding = wxT("UTF-8"); // default in absence of encoding="" ctx.conv = NULL; #if !wxUSE_UNICODE if ( encoding.CmpNoCase(wxT("UTF-8")) != 0 ) ctx.conv = new wxCSConv(encoding); #endif ctx.removeWhiteOnlyNodes = (flags & wxXMLDOC_KEEP_WHITESPACE_NODES) == 0; XML_SetUserData(parser, (void*)&ctx); XML_SetElementHandler(parser, StartElementHnd, EndElementHnd); XML_SetCharacterDataHandler(parser, TextHnd); XML_SetStartCdataSectionHandler(parser, StartCdataHnd); XML_SetCommentHandler(parser, CommentHnd); XML_SetDefaultHandler(parser, DefaultHnd); XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL); bool ok = true; do { size_t len = stream.Read(buf, BUFSIZE).LastRead(); done = (len < BUFSIZE); if (!XML_Parse(parser, buf, len, done)) { wxString error(XML_ErrorString(XML_GetErrorCode(parser)), *wxConvCurrent); wxLogError(_("XML parsing error: '%s' at line %d"), error.c_str(), XML_GetCurrentLineNumber(parser)); ok = false; break; } } while (!done); if (ok) { if (!ctx.version.empty()) SetVersion(ctx.version); if (!ctx.encoding.empty()) SetFileEncoding(ctx.encoding); SetRoot(ctx.root); } else { delete ctx.root; } XML_ParserFree(parser); #if !wxUSE_UNICODE if ( ctx.conv ) delete ctx.conv; #endif return ok; }
/* static */ bool IStateStore::LoadData(wxInputStream& input, void* dest, size_t size) { input.Read(dest, size); return CheckLastRead(input, size); }
// load the mxf file format bool wxMXFHandler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose, int index) { opj_dparameters_t parameters; /* decompression parameters */ opj_event_mgr_t event_mgr; /* event manager */ opj_image_t *opjimage = NULL; unsigned char *src = NULL; unsigned char *ptr; int file_length, j2k_point, j2k_len; opj_codestream_info_t cstr_info; /* Codestream information structure */ // destroy the image image->Destroy(); /* handle to a decompressor */ opj_dinfo_t* dinfo = NULL; opj_cio_t *cio = NULL; /* configure the event callbacks (not required) */ memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); event_mgr.error_handler = mxf_error_callback; event_mgr.warning_handler = mxf_warning_callback; event_mgr.info_handler = mxf_info_callback; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* prepare parameters */ strncpy(parameters.infile, "", sizeof(parameters.infile)-1); strncpy(parameters.outfile, "", sizeof(parameters.outfile)-1); parameters.decod_format = J2K_CFMT; parameters.cod_format = BMP_DFMT; if (m_reducefactor) parameters.cp_reduce = m_reducefactor; if (m_qualitylayers) parameters.cp_layer = m_qualitylayers; /*if (n_components) parameters. = n_components;*/ /* JPWL only */ #ifdef USE_JPWL parameters.jpwl_exp_comps = m_expcomps; parameters.jpwl_max_tiles = m_maxtiles; parameters.jpwl_correct = m_enablejpwl; #endif /* USE_JPWL */ /* get a decoder handle */ dinfo = opj_create_decompress(CODEC_J2K); /* find length of the stream */ stream.SeekI(0, wxFromEnd); file_length = (int) stream.TellI(); /* search for the m_framenum codestream position and length */ //jp2c_point = searchjp2c(stream, file_length, m_framenum); //jp2c_len = searchjp2c(stream, file_length, m_framenum); j2k_point = 0; j2k_len = 10; // malloc memory source src = (unsigned char *) malloc(j2k_len); // copy the jp2c stream.SeekI(j2k_point, wxFromStart); stream.Read(src, j2k_len); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ cio = opj_cio_open((opj_common_ptr)dinfo, src, j2k_len); /* decode the stream and fill the image structure */ opjimage = opj_decode_with_info(dinfo, cio, &cstr_info); if (!opjimage) { wxMutexGuiEnter(); wxLogError(wxT("MXF: failed to decode image!")); wxMutexGuiLeave(); opj_destroy_decompress(dinfo); opj_cio_close(cio); free(src); return false; } /* close the byte stream */ opj_cio_close(cio); /* common rendering method */ #include "imagjpeg2000.cpp" wxMutexGuiEnter(); wxLogMessage(wxT("MXF: image loaded.")); wxMutexGuiLeave(); /* close openjpeg structs */ opj_destroy_decompress(dinfo); opj_image_destroy(opjimage); free(src); if (!image->Ok()) return false; else return true; }
size_t OnSysRead(void *buffer, size_t bufsize) { wxASSERT(m_pStream); size_t ret = m_pStream->Read(buffer, bufsize).LastRead(); Synch(); return ret; }
bool wxAnimation::Load(wxInputStream &stream, wxAnimationType type) { UnRef(); char anim_type[12]; switch (type) { case wxANIMATION_TYPE_GIF: strcpy(anim_type, "gif"); break; case wxANIMATION_TYPE_ANI: strcpy(anim_type, "ani"); break; default: anim_type[0] = '\0'; break; } // create a GdkPixbufLoader GError *error = NULL; GdkPixbufLoader *loader; if (type != wxANIMATION_TYPE_INVALID && type != wxANIMATION_TYPE_ANY) loader = gdk_pixbuf_loader_new_with_type(anim_type, &error); else loader = gdk_pixbuf_loader_new(); if (!loader) { wxLogDebug(wxT("Could not create the loader for '%s' animation type"), anim_type); return false; } // connect to loader signals g_signal_connect(loader, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated), this); guchar buf[2048]; while (stream.IsOk()) { // read a chunk of data stream.Read(buf, sizeof(buf)); // fetch all data into the loader if (!gdk_pixbuf_loader_write(loader, buf, stream.LastRead(), &error)) { gdk_pixbuf_loader_close(loader, &error); wxLogDebug(wxT("Could not write to the loader")); return false; } } // load complete if (!gdk_pixbuf_loader_close(loader, &error)) { wxLogDebug(wxT("Could not close the loader")); return false; } // wait until we get the last area_updated signal return true; }