public T GetMappedObject <T>(byte[] bytes, uint initialOffset) where T : class, new() { if (typeof(T) == typeof(ImageLayoutInfo)) { var offset = initialOffset; var layoutInfo = new ImageLayoutInfo { newImageOffset = BitConverter.ToInt32( new[] { bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3] }, 0), //sometimes it is FF FF FF FF, sometimes - not offsetY = BitConverter.ToInt16(new[] { bytes[offset + 6], bytes[offset + 7] }, 0), offsetX = BitConverter.ToInt16(new[] { bytes[offset + 4], bytes[offset + 5] }, 0), Width = BitConverter.ToInt16(new[] { bytes[offset + 8], bytes[offset + 9] }, 0), Height = BitConverter.ToInt16(new[] { bytes[offset + 10], bytes[offset + 11] }, 0), EndOfHeader = BitConverter.ToInt32(new[] { bytes[offset + 12], bytes[offset + 13], bytes[offset + 14], bytes[offset + 15] }, 0), // should always be FF FF FF FF NumberOfRows = BitConverter.ToInt16(new[] { bytes[offset + 21], bytes[offset + 22] }, 0), }; return(layoutInfo as T); } var emptyObject = Activator.CreateInstance <T>(); return(emptyObject); }
private void CheckItem(ImageLayoutInfo layoutInfo) { if (layoutInfo.EndOfHeader != -1) { throw new Exception("EndOfHeader should be FF FF FF FF"); } }
public void SavePhotoAndTags() { var checksum = "abc123"; var fileNameOriginal = "bill-birthday.jpg"; var fileNameCache = "983767D2-96B3-4372-89D1-74C190EE23B0.jpg"; var cacheSubfolder = "c1"; var cacheFilePath = Path.Combine(cacheSubfolder, fileNameCache); var imageFilePath = Path.Combine("home", "homePhotos", "parties", "birthdays", fileNameOriginal); var tags = new List <string> { "Tag1", "Tag2", "parties", "birthdays" }; var exifData = new List <ExifDirectoryBase> { { new ExifSubIfdDirectory() }, { new ExifIfd0Directory() } }; var imageLayoutInfo = new ImageLayoutInfo { Height = 1000, Width = 3000, LayoutType = ImageLayoutType.Landscape, Ratio = 0.33m }; _fileSystemService.Setup(m => m.GetDirectoryTags(imageFilePath)).Returns(tags); _photoService.Setup(m => m.SavePhoto(It.IsAny <Model.Photo>())) .Callback <Model.Photo>((photo) => { Assert.Equal(checksum, photo.Checksum); Assert.Equal(fileNameOriginal, photo.Name); Assert.Equal(fileNameCache, photo.FileName); Assert.Equal(cacheSubfolder, photo.CacheFolder); }); _photoService.Setup(m => m.AssociateTags(It.IsAny <Model.Photo>(), It.IsAny <string[]>())) .Callback <Model.Photo, string[]>((photo, tags) => { Assert.Equal(checksum, photo.Checksum); Assert.Equal(fileNameOriginal, photo.Name); Assert.Equal(fileNameCache, photo.FileName); Assert.Equal(cacheSubfolder, photo.CacheFolder); Assert.NotNull(tags); Assert.True(tags.Length == 4); Assert.Contains("Tag1", tags); Assert.Contains("Tag2", tags); Assert.Contains("parties", tags); Assert.Contains("birthdays", tags); }); //_imageTransformer.Setup(m => m.GetImageLayoutInfo(It.IsAny<string>())).Returns(imageLayoutInfo); _imageService.SavePhotoAndTags(null, imageFilePath, cacheFilePath, checksum, imageLayoutInfo, exifData); _fileSystemService.Verify(m => m.GetDirectoryTags(imageFilePath), Times.Once); _photoService.Verify(m => m.SavePhoto(It.IsAny <Model.Photo>()), Times.Once); _photoService.Verify(m => m.AssociateTags(It.IsAny <Model.Photo>(), It.IsAny <string[]>()), Times.Once); }
public void RenderShapeBlocks(ImageView imageView, List <AbsoluteBlock> piactureElements, ImageLayoutInfo layout) { foreach (var block in piactureElements) { var slice = Enumerable.Range(1, block.Length).Select(it => _shadowColor).ToArray(); imageView.DrawHorizontalColorLine(slice, layout.offsetX + block.OffsetX, layout.offsetY + block.OffsetY); } }
public Bitmap Run(IList <ImageLayout> extractResult, NationColorOffset nationColorOffset, int imageNumber, RawParser rawParser, IRenderer renderer, Color[] imagePaletteArray, Color[] generalPaletteColors) { var imageGenerator = new ImageGenerator(); var partialLayouts = extractResult[imageNumber].PartialLayouts; var largestWidth = partialLayouts .Select(it => it.Width + it.offsetX) .OrderByDescending(it => it) .First(); var largestHeight = partialLayouts .Select(it => it.Height + it.offsetY) .OrderByDescending(it => it) .First(); var bitMap = new Bitmap(largestWidth, largestHeight); renderer.SetupCanvas(bitMap); foreach (var layout in partialLayouts) { ImageLayoutInfo layout1 = layout; var imageView = new ImageView(layout1.Width + layout1.offsetX, layout1.Height + layout1.offsetY); var firstPartBlocks = Helper.WithMeasurement(() => { var rawFirstPartBlocks = rawParser.ParseRawBlockGroups(layout1.Bytes, layout1.NumberOfRows); return(rawFirstPartBlocks.ConvertToAbsoluteCoordinatesBlocks()); }, "firstPartBlocks"); var secondPartBlocks = Helper.WithMeasurement(() => rawParser.GetRawColorBlocks(layout1.Bytes, layout1.ColorBlocksSectionOffset, (int)layout1.GlobalByteOffsetStart + layout1.ColorBlocksSectionOffset + layout1.HeaderBytes.Length, layout1.IsNationColorImage), "secondPartBlocks"); Helper.WithMeasurement(() => { imageGenerator.RenderShapeBlocks(imageView, firstPartBlocks, layout1); imageGenerator.RenderCounterBlocksOnBitmap(imageView, nationColorOffset, firstPartBlocks, secondPartBlocks, layout1, imagePaletteArray, generalPaletteColors); }, "RenderCounterBlocksOnBitmap"); Helper.WithMeasurement(() => renderer.RenderImage(bitMap, imageView), "Render on bitmap"); } return(bitMap); }
public Photo SavePhotoAndTags(Photo existingPhoto, string imageFilePath, string cacheFilePath, string checksum, ImageLayoutInfo imageLayoutInfo, IEnumerable <ExifDirectoryBase> exifDataList, params string[] tags) { _logger.LogInformation("Saving photo with checksum {Checksum}.", checksum); var dirTags = _fileSystemService.GetDirectoryTags(imageFilePath); var imageInfo = GetImageInfo(exifDataList); var photo = existingPhoto ?? new Photo(); photo.Name = Path.GetFileName(imageFilePath); photo.FileName = Path.GetFileName(cacheFilePath); photo.Checksum = checksum; photo.CacheFolder = Path.GetDirectoryName(cacheFilePath); photo.DateFileCreated = File.GetCreationTime(imageFilePath); photo.DateTaken = imageInfo.DateTaken; photo.ImageHeight = imageLayoutInfo.Height; photo.ImageWidth = imageLayoutInfo.Width; photo.ReprocessCache = false; _photoService.SavePhoto(photo); if (existingPhoto == null) { var photoTags = dirTags.ToList(); if (tags != null && tags.Length > 0) { photoTags.AddRange(tags); } _photoService.AssociateTags(photo, photoTags.ToArray()); } _logger.LogInformation("Saved photo to database."); return(photo); }
public void RenderCounterBlocksOnBitmap(ImageView imageView, NationColorOffset nationColorOffset, List <AbsoluteBlock> piactureElements, Collection <RawColorBlock> secondPartBlocks, ImageLayoutInfo layout, Color[] imagePaletteArray, Color[] generalPaletteColors) { var blocksDistributor = new BlocksDistributor(); var blockContainerCollection = Helper.WithMeasurement(() => blocksDistributor.GetDistributedCounterPartBlocks(piactureElements, secondPartBlocks), "GetDistributedCounterPartBlocks"); foreach (var blockContainer in blockContainerCollection) { foreach (var counterBlockContainer in blockContainer.CounterBlockContainers) { var sourceOffset = counterBlockContainer.RawColorBlock.Offset + counterBlockContainer.StripePadding; if (counterBlockContainer.RawColorBlock.type == RawColorBlockType.MultiPixel) { var offsetX = layout.offsetX + blockContainer.Block.OffsetX + counterBlockContainer.Offset; var offsetY = layout.offsetY + blockContainer.Block.OffsetY; var destinationOffset = offsetY * imageView.Width + offsetX; Array.Copy( sourceArray: imagePaletteArray, sourceIndex: sourceOffset, destinationArray: imageView.Pixels, destinationIndex: destinationOffset, length: counterBlockContainer.Width); } else if (counterBlockContainer.RawColorBlock.type == RawColorBlockType.FourPixel) { var offsetX = layout.offsetX + blockContainer.Block.OffsetX + counterBlockContainer.Offset; var offsetY = layout.offsetY + blockContainer.Block.OffsetY; var destinationOffset = offsetY * imageView.Width + offsetX; var colorByte = counterBlockContainer.RawColorBlock.One; // Codes 4 pixels: 2 bits for pixel var _1 = colorByte & 0x03; var _2 = (colorByte >> 2) & 0x03; var _3 = (colorByte >> 4) & 0x03; var _4 = (colorByte >> 6) & 0x03; var slice = new[] { generalPaletteColors[nationColorOffset.Offset + _1], generalPaletteColors[nationColorOffset.Offset + _2], generalPaletteColors[nationColorOffset.Offset + _3], generalPaletteColors[nationColorOffset.Offset + _4] }; Array.Copy( sourceArray: slice, sourceIndex: sourceOffset, destinationArray: imageView.Pixels, destinationIndex: destinationOffset, length: counterBlockContainer.Width); } else { var colorIndex = counterBlockContainer.RawColorBlock.One; var color = generalPaletteColors[colorIndex]; imageView.DrawColorPixel(color, layout.offsetX + blockContainer.Block.OffsetX + counterBlockContainer.Offset, layout.offsetY + blockContainer.Block.OffsetY); } } } }
public void RenderShapeBlocks(ImageView imageView, List<AbsoluteBlock> piactureElements, ImageLayoutInfo layout) { foreach (var block in piactureElements) { var slice = Enumerable.Range(1, block.Length).Select(it => _shadowColor).ToArray(); imageView.DrawHorizontalColorLine(slice, layout.offsetX + block.OffsetX, layout.offsetY + block.OffsetY); } }
public void RenderCounterBlocksOnBitmap(ImageView imageView, NationColorOffset nationColorOffset, List<AbsoluteBlock> piactureElements, Collection<RawColorBlock> secondPartBlocks, ImageLayoutInfo layout, Color[] imagePaletteArray, Color[] generalPaletteColors) { var blocksDistributor = new BlocksDistributor(); var blockContainerCollection = Helper.WithMeasurement(() => blocksDistributor.GetDistributedCounterPartBlocks(piactureElements, secondPartBlocks), "GetDistributedCounterPartBlocks"); foreach (var blockContainer in blockContainerCollection) { foreach (var counterBlockContainer in blockContainer.CounterBlockContainers) { var sourceOffset = counterBlockContainer.RawColorBlock.Offset + counterBlockContainer.StripePadding; if (counterBlockContainer.RawColorBlock.type == RawColorBlockType.MultiPixel) { var offsetX = layout.offsetX + blockContainer.Block.OffsetX + counterBlockContainer.Offset; var offsetY = layout.offsetY + blockContainer.Block.OffsetY; var destinationOffset = offsetY * imageView.Width + offsetX; Array.Copy( sourceArray: imagePaletteArray, sourceIndex: sourceOffset, destinationArray: imageView.Pixels, destinationIndex: destinationOffset, length: counterBlockContainer.Width); } else if (counterBlockContainer.RawColorBlock.type == RawColorBlockType.FourPixel) { var offsetX = layout.offsetX + blockContainer.Block.OffsetX + counterBlockContainer.Offset; var offsetY = layout.offsetY + blockContainer.Block.OffsetY; var destinationOffset = offsetY * imageView.Width + offsetX; var colorByte = counterBlockContainer.RawColorBlock.One; // Codes 4 pixels: 2 bits for pixel var _1 = colorByte & 0x03; var _2 = (colorByte >> 2) & 0x03; var _3 = (colorByte >> 4) & 0x03; var _4 = (colorByte >> 6) & 0x03; var slice = new[] { generalPaletteColors[nationColorOffset.Offset + _1], generalPaletteColors[nationColorOffset.Offset + _2], generalPaletteColors[nationColorOffset.Offset + _3], generalPaletteColors[nationColorOffset.Offset + _4] }; Array.Copy( sourceArray: slice, sourceIndex: sourceOffset, destinationArray: imageView.Pixels, destinationIndex: destinationOffset, length: counterBlockContainer.Width); } else { var colorIndex = counterBlockContainer.RawColorBlock.One; var color = generalPaletteColors[colorIndex]; imageView.DrawColorPixel(color, layout.offsetX + blockContainer.Block.OffsetX + counterBlockContainer.Offset, layout.offsetY + blockContainer.Block.OffsetY); } } } }