public void TestYCbCr8ToRgba32(byte y, byte cb, byte cr) { var converter = TiffYCbCrConverter8.CreateDefault(); Span <byte> ycbcr = stackalloc byte[9]; ycbcr[0] = y; ycbcr[1] = cb; ycbcr[2] = cr; ycbcr[3] = y; ycbcr[4] = cb; ycbcr[5] = cr; ycbcr[6] = y; ycbcr[7] = cb; ycbcr[8] = cr; Span <TiffRgba32> rgba = stackalloc TiffRgba32[3]; Span <byte> ycbcrBack = stackalloc byte[9]; converter.ConvertToRgba32(ycbcr, rgba, 3); for (int i = 0; i < 3; i++) { converter.ConvertFromRgb24(new TiffRgb24(rgba[i].R, rgba[i].G, rgba[i].B), out ycbcrBack[i * 3], out ycbcrBack[i * 3 + 1], out ycbcrBack[i * 3 + 2]); } Assert.True(ycbcr.SequenceEqual(ycbcrBack)); }
public void TestYCbCr8ToRgb24(byte y, byte cb, byte cr) { var converter = TiffYCbCrConverter8.CreateDefault(); TiffRgb24 pixel = converter.ConvertToRgb24(y, cb, cr); converter.ConvertFromRgb24(pixel, out byte oy, out byte ocb, out byte ocr); Assert.Equal(y, oy); Assert.Equal(cb, ocb); Assert.Equal(cr, ocr); Span <byte> ycbcr = stackalloc byte[9]; ycbcr[0] = y; ycbcr[1] = cb; ycbcr[2] = cr; ycbcr[3] = y; ycbcr[4] = cb; ycbcr[5] = cr; ycbcr[6] = y; ycbcr[7] = cb; ycbcr[8] = cr; Span <TiffRgb24> rgb = stackalloc TiffRgb24[3]; Span <byte> ycbcrBack = stackalloc byte[9]; converter.ConvertToRgb24(ycbcr, rgb, 3); converter.ConvertFromRgb24(rgb, ycbcrBack, 3); Assert.True(ycbcr.SequenceEqual(ycbcrBack)); }
public async ValueTask InvokeAsync(TiffImageEncoderContext <TPixel> context, ITiffImageEncoderPipelineNode <TPixel> next) { MemoryPool <byte> memoryPool = context.MemoryPool ?? MemoryPool <byte> .Shared; TiffSize imageSize = context.ImageSize; int arraySize = 3 * imageSize.Width * imageSize.Height; using (IMemoryOwner <byte> pixelData = memoryPool.Rent(arraySize)) { Memory <byte> pixelDataMemory = pixelData.Memory; using (var writer = new TiffMemoryPixelBufferWriter <TiffRgb24>(memoryPool, pixelDataMemory, imageSize.Width, imageSize.Height)) using (TiffPixelBufferWriter <TPixel> convertedWriter = context.ConvertWriter(writer.AsPixelBufferWriter())) { await context.GetReader().ReadAsync(convertedWriter, context.CancellationToken).ConfigureAwait(false); } TiffYCbCrConverter8.CreateDefault().ConvertFromRgb24(MemoryMarshal.Cast <byte, TiffRgb24>(pixelDataMemory.Span), pixelDataMemory.Span, imageSize.Width * imageSize.Height); context.PhotometricInterpretation = TiffPhotometricInterpretation.YCbCr; context.BitsPerSample = TiffValueCollection.UnsafeWrap(s_bitsPerSample); context.UncompressedData = pixelDataMemory.Slice(0, arraySize); await next.RunAsync(context).ConfigureAwait(false); context.UncompressedData = default; } TiffImageFileDirectoryWriter?ifdWriter = context.IfdWriter; if (!(ifdWriter is null)) { using (await context.LockAsync().ConfigureAwait(false)) { await ifdWriter.WriteTagAsync(TiffTag.PhotometricInterpretation, TiffValueCollection.Single((ushort)context.PhotometricInterpretation)).ConfigureAwait(false); await ifdWriter.WriteTagAsync(TiffTag.BitsPerSample, context.BitsPerSample).ConfigureAwait(false); await ifdWriter.WriteTagAsync(TiffTag.SamplesPerPixel, TiffValueCollection.Single <ushort>(3)).ConfigureAwait(false); await ifdWriter.WriteTagAsync(TiffTag.YCbCrCoefficients, TiffYCbCrConverter8.DefaultLuma).ConfigureAwait(false); await ifdWriter.WriteTagAsync(TiffTag.ReferenceBlackWhite, TiffYCbCrConverter8.DefaultReferenceBlackWhite).ConfigureAwait(false); } } }