Ejemplo n.º 1
0
        public static void InterpolateA(LdrColorA c0, LdrColorA c1, int wa, int waprec, LdrColorA outt)
        {
            DebugGuard.MustBeBetweenOrEqualTo(waprec, 2, 4, nameof(waprec));

            int[] aWeights;
            switch (waprec)
            {
            case 2:
            {
                aWeights = Constants.Weights2;
                Debug.Assert(wa < 4, "wc is expected to be smaller then 4");
                break;
            }

            case 3:
            {
                aWeights = Constants.Weights3;
                Debug.Assert(wa < 8, "wc is expected to be smaller then 8");
                break;
            }

            case 4:
            {
                aWeights = Constants.Weights4;
                Debug.Assert(wa < 16, "wc is expected to be smaller then 16");
                break;
            }

            default:
                outt.A = 0;
                return;
            }

            outt.A = (byte)(((c0.A * (uint)(Constants.BC67_WEIGHT_MAX - aWeights[wa])) + (c1.A * (uint)aWeights[wa]) + Constants.BC67_WEIGHT_ROUND) >> Constants.BC67_WEIGHT_SHIFT);
        }
        public TiffPaletteWriter(
            ImageFrame <TPixel> image,
            IQuantizer quantizer,
            MemoryAllocator memoryAllocator,
            Configuration configuration,
            TiffEncoderEntriesCollector entriesCollector,
            int bitsPerPixel)
            : base(image, memoryAllocator, configuration, entriesCollector)
        {
            DebugGuard.NotNull(quantizer, nameof(quantizer));
            DebugGuard.NotNull(configuration, nameof(configuration));
            DebugGuard.NotNull(entriesCollector, nameof(entriesCollector));
            DebugGuard.MustBeBetweenOrEqualTo(bitsPerPixel, 4, 8, nameof(bitsPerPixel));

            this.BitsPerPixel      = bitsPerPixel;
            this.maxColors         = this.BitsPerPixel == 4 ? 16 : 256;
            this.colorPaletteSize  = this.maxColors * 3;
            this.colorPaletteBytes = this.colorPaletteSize * 2;
            using IQuantizer <TPixel> frameQuantizer = quantizer.CreatePixelSpecificQuantizer <TPixel>(this.Configuration, new QuantizerOptions()
            {
                MaxColors = this.maxColors
            });
            this.quantizedImage = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds());

            this.AddColorMapTag();
        }
Ejemplo n.º 3
0
#pragma warning restore SA1600 // ElementsMustBeDocumented

        /// <summary>
        /// Get/Set scalar elements at a given index
        /// </summary>
        /// <param name="idx">The index</param>
        /// <returns>The float value at the specified index</returns>
        public float this[int idx]
        {
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            get
            {
                DebugGuard.MustBeBetweenOrEqualTo(idx, 0, Size - 1, nameof(idx));
                ref float selfRef = ref Unsafe.As <Block8x8F, float>(ref this);
        public static void LoadAndStretchEdges(RowOctet <TPixel> source, Span <TPixel> dest, Point start, Size sampleSize, Size totalSize)
        {
            DebugGuard.MustBeBetweenOrEqualTo(start.X, 0, totalSize.Width - 1, nameof(start.X));
            DebugGuard.MustBeBetweenOrEqualTo(start.Y, 0, totalSize.Height - 1, nameof(start.Y));

            int width  = Math.Min(sampleSize.Width, totalSize.Width - start.X);
            int height = Math.Min(sampleSize.Height, totalSize.Height - start.Y);

            uint byteWidth       = (uint)(width * Unsafe.SizeOf <TPixel>());
            int  remainderXCount = sampleSize.Width - width;

            ref byte blockStart     = ref MemoryMarshal.GetReference(MemoryMarshal.Cast <TPixel, byte>(dest));
Ejemplo n.º 5
0
        /// <summary>
        /// Sets the specified bits within the packed fields to the supplied
        /// value.
        /// </summary>
        /// <param name="startIndex">The zero-based index within the packed fields of the first bit to  set.</param>
        /// <param name="length">The number of bits to set.</param>
        /// <param name="valueToSet">The value to set the bits to.</param>
        public void SetBits(int startIndex, int length, int valueToSet)
        {
            DebugGuard.MustBeBetweenOrEqualTo(startIndex, 0, 7, nameof(startIndex));
            DebugCheckLength(startIndex, length);

            int bitShift = length - 1;

            for (int i = startIndex; i < startIndex + length; i++)
            {
                int bitValueIfSet = 1 << bitShift;
                int bitValue      = valueToSet & bitValueIfSet;
                int bitIsSet      = bitValue >> bitShift;
                Bits[i] = bitIsSet == 1;
                bitShift--;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets the value of the specified bits within the byte.
        /// </summary>
        /// <param name="startIndex">The zero-based index of the first bit to get.</param>
        /// <param name="length">The number of bits to get.</param>
        /// <returns>
        /// The value of the specified bits within the byte.
        /// </returns>
        public int GetBits(int startIndex, int length)
        {
            DebugGuard.MustBeBetweenOrEqualTo(startIndex, 1, 8, nameof(startIndex));
            DebugCheckLength(startIndex, length);

            int returnValue = 0;
            int bitShift    = length - 1;

            for (int i = startIndex; i < startIndex + length; i++)
            {
                int bitValue = (Bits[i] ? 1 : 0) << bitShift;
                returnValue += bitValue;
                bitShift--;
            }

            return(returnValue);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes quantization table.
        /// </summary>
        /// <param name="i">The quantization index.</param>
        /// <param name="scale">The scaling factor.</param>
        /// <param name="quant">The quantization table.</param>
        private static void InitQuantizationTable(int i, int scale, ref Block8x8F quant)
        {
            DebugGuard.MustBeBetweenOrEqualTo(i, 0, 1, nameof(i));
            ReadOnlySpan <byte> unscaledQuant = (i == 0) ? UnscaledQuant_Luminance : UnscaledQuant_Chrominance;

            for (int j = 0; j < Block8x8F.Size; j++)
            {
                int x = unscaledQuant[j];
                x = ((x * scale) + 50) / 100;
                if (x < 1)
                {
                    x = 1;
                }

                if (x > 255)
                {
                    x = 255;
                }

                quant[j] = x;
            }
        }
Ejemplo n.º 8
0
        private static int QualityToScale(int quality)
        {
            DebugGuard.MustBeBetweenOrEqualTo(quality, MinQualityFactor, MaxQualityFactor, nameof(quality));

            return(quality < 50 ? (5000 / quality) : (200 - (quality * 2)));
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Sets the specified bit within the packed fields to the supplied
 /// value.
 /// </summary>
 /// <param name="index">
 /// The zero-based index within the packed fields of the bit to set.
 /// </param>
 /// <param name="valueToSet">
 /// The value to set the bit to.
 /// </param>
 public void SetBit(int index, bool valueToSet)
 {
     DebugGuard.MustBeBetweenOrEqualTo(index, 0, 7, nameof(index));
     Bits[index] = valueToSet;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Gets the value of the specified bit within the byte.
 /// </summary>
 /// <param name="index">The zero-based index of the bit to get.</param>
 /// <returns>
 /// The value of the specified bit within the byte.
 /// </returns>
 public bool GetBit(int index)
 {
     DebugGuard.MustBeBetweenOrEqualTo(index, 0, 7, nameof(index));
     return(Bits[index]);
 }