internal void Read(BinaryReader _reader, bool _decompressImage) { m_contentRectangle = new Rectangle(_reader.ReadInt32(), _reader.ReadInt32(), _reader.ReadInt32(), _reader.ReadInt32()); m_compressedImageFormat = (ImageFile.FILE_FORMAT)_reader.ReadInt32(); // Read compressed image int compressedImageLength = _reader.ReadInt32(); m_compressedImage = new byte[compressedImageLength]; _reader.Read(m_compressedImage, 0, compressedImageLength); if (!_decompressImage) { return; } // Attempt to read the image file try { using (NativeByteArray imageContent = new NativeByteArray(m_compressedImage)) { m_image = new ImageFile(imageContent, m_compressedImageFormat); } } catch (Exception _e) { throw new Exception("An error occurred while attempting to decompress image from byte[] (length = " + compressedImageLength + " format = " + m_compressedImageFormat + ")", _e); } }
internal WebPageImagePart(Rectangle _contentRectangle, ImageFile _image, ImageFile.FILE_FORMAT _compressedImageFormat) { if (_image == null) { throw new Exception("Invalid image part!"); } m_contentRectangle = _contentRectangle; m_image = _image; m_compressedImageFormat = _compressedImageFormat; // Compress the image try { NativeByteArray content = null; switch (m_compressedImageFormat) { case ImageFile.FILE_FORMAT.PNG: content = m_image.Save(ImageFile.FILE_FORMAT.PNG, ImageFile.SAVE_FLAGS.SF_PNG_Z_BEST_COMPRESSION); break; case ImageFile.FILE_FORMAT.JPEG: content = m_image.Save(ImageFile.FILE_FORMAT.JPEG, ImageFile.SAVE_FLAGS.SF_JPEG_QUALITYNORMAL); break; default: content = m_image.Save(m_compressedImageFormat, ImageFile.SAVE_FLAGS.NONE); break; } if (content == null) { throw new Exception("Failed to save image using \"" + m_compressedImageFormat + "\" format! Image library returned null..."); } m_compressedImage = content.AsByteArray; content.Dispose(); } catch (Exception _e) { throw new Exception("An error occurred during image compression!", _e); } }
public void UpdateImages(uint _imageStartIndex, Rectangle[] _contentRectangles, ImageFile[] _images, ImageFile.FILE_FORMAT _compressedFormat) { int newSize = Mathf.Max(m_images.Length, (int)_imageStartIndex + _images.Length); WebPageImagePart[] newImages = new WebPageImagePart[newSize]; // Copy existing images for (uint imageIndex = 0; imageIndex < m_images.Length; imageIndex++) { newImages[imageIndex] = m_images[imageIndex]; } // Replace with new images for (uint imageIndex = 0; imageIndex < _images.Length; imageIndex++) { if (newImages[_imageStartIndex + imageIndex] != null) { newImages[_imageStartIndex + imageIndex].Dispose(); // We're replacing this image with a new one... } if (_images[imageIndex] == null) { throw new Exception("Invalid image! Fiche web images must not be null!"); } try { newImages[_imageStartIndex + imageIndex] = new WebPageImagePart(_contentRectangles[imageIndex], _images[imageIndex], _compressedFormat); } catch (Exception _e) { // Something went wrong! m_owner.Database.AsyncMain_ReportFicheStatus(m_owner, FichesDB.FICHE_REPORT_STATUS.ERROR, "An error occurred while saving a part of the web page image! " + _e.Message); } } // Replace old array m_images = newImages; // Notify? m_owner.NotifyWebPageImageChanged(this); }
public ChunkWebPageSnapshot(Fiche _owner, uint _imageStartIndex, Rectangle[] _contentRectangles, ImageFile[] _images, ImageFile.FILE_FORMAT _targetFormat) : base(_owner, ~0UL, 0) { UpdateImages(_imageStartIndex, _contentRectangles, _images, _targetFormat); }
/// <summary> /// Create the image chunk and feed it our image /// </summary> /// <param name="_imagesWebPage"></param> internal void CreateImageChunk(uint _imageStartIndex, Rectangle[] _contentRectangles, ImageFile[] _imagesWebPage, ImageFile.FILE_FORMAT _targetFormat) { ChunkWebPageSnapshot chunk = FindChunkByType <ChunkWebPageSnapshot>(); if (chunk == null) { chunk = new ChunkWebPageSnapshot(this, _imageStartIndex, _contentRectangles, _imagesWebPage, _targetFormat); } else { chunk.UpdateImages(_imageStartIndex, _contentRectangles, _imagesWebPage, _targetFormat); } }