예제 #1
0
        public static byte[] Decode(int bitsPerChar, DecodeCallback decodeCallback,
                                    IEnumerable <char> value, int bitsToDecode)
        {
            Helper.CheckRange("bitsPerChar", bitsPerChar, 1, 8);
            Helper.CheckNull("decodeCallback", decodeCallback);
            Helper.CheckNull("value", value);
            Helper.CheckRange("bitsToDecode", bitsToDecode, 0, int.MaxValue - 7);
            using (IEnumerator <char> e = value.GetEnumerator()) {
                byte[] bytes = new byte[(bitsToDecode + 7) / 8];

                int j = 0, bits = 0, buffer = 0;
                while (bitsToDecode > 0)
                {
                    while (bits < 8)
                    {
                        if (!e.MoveNext())
                        {
                            break;
                        }
                        bits   += bitsPerChar; buffer <<= bitsPerChar;
                        buffer |= decodeCallback(e.Current) & ((1 << bitsPerChar) - 1);
                    }

                    int chunk = Math.Min(8, Math.Min(bits, bitsToDecode));
                    bits -= chunk; bitsToDecode -= 8;

                    int eightBit = buffer >> bits; buffer ^= eightBit << bits;
                    bytes[j++] = (byte)(eightBit << (8 - chunk));
                }

                Debug.Assert(j == bytes.Length);
                return(bytes);
            }
        }
예제 #2
0
        public void Decode(byte[] buf, DecodeCallback callback)
        {
            buffer = buffer.Concat(buf);
            while (buffer.Count() > 0)
            {
                if (packetLength == 0)
                {
                    if (buffer.Count() < 4)
                    {
                        return;
                    }
                    packetLength = BitConverter.ToInt32(buffer.Take(4).ToArray());
                    buffer       = buffer.Skip(4);
                }

                if (buffer.Count() < packetLength)
                {
                    return;
                }

                string message = Encoding.UTF8.GetString(buffer.Skip(8).Take(packetLength - 9).ToArray());
                buffer       = buffer.Skip(packetLength);
                packetLength = 0;
                callback(message);
            }
        }
예제 #3
0
 public static byte[] Decode(int bitsPerChar, DecodeCallback decodeCallback,
                             char[] value)
 {
     Helper.CheckRange("bitsPerChar", bitsPerChar, 1, 8);
     Helper.CheckNull("value", value);
     Helper.CheckRange("value", value, 0, int.MaxValue / bitsPerChar);
     return(Decode(bitsPerChar, decodeCallback, value, value.Length * bitsPerChar));
 }
예제 #4
0
        public static byte[] Decode(int bitsPerChar, DecodeCallback decodeCallback,
		                            char[] value)
        {
            Helper.CheckRange("bitsPerChar", bitsPerChar, 1, 8);
            Helper.CheckNull("value", value);
            Helper.CheckRange("value", value, 0, int.MaxValue/bitsPerChar);
            return Decode(bitsPerChar, decodeCallback, value, value.Length*bitsPerChar);
        }
예제 #5
0
        public static byte[] Decode(int bitsPerChar, DecodeCallback decodeCallback,
		                            IEnumerable<char> value, int bitsToDecode)
        {
            Helper.CheckRange("bitsPerChar", bitsPerChar, 1, 8);
            Helper.CheckNull("decodeCallback", decodeCallback);
            Helper.CheckNull("value", value);
            Helper.CheckRange("bitsToDecode", bitsToDecode, 0, int.MaxValue - 7);
            using (IEnumerator<char> e = value.GetEnumerator())
            {
                byte[] bytes = new byte[(bitsToDecode + 7)/8];

                int j = 0, bits = 0, buffer = 0;
                while (bitsToDecode > 0)
                {
                    while (bits < 8)
                    {
                        if (!e.MoveNext())
                        {
                            break;
                        }
                        bits += bitsPerChar;
                        buffer <<= bitsPerChar;
                        buffer |= decodeCallback(e.Current) & ((1 << bitsPerChar) - 1);
                    }

                    int chunk = Math.Min(8, Math.Min(bits, bitsToDecode));
                    bits -= chunk;
                    bitsToDecode -= 8;

                    int eightBit = buffer >> bits;
                    buffer ^= eightBit << bits;
                    bytes[j++] = (byte) (eightBit << (8 - chunk));
                }

                Debug.Assert(j == bytes.Length);
                return bytes;
            }
        }
예제 #6
0
        private static object ToneMap(ref FIBITMAP b, bool mayUnloadOriginal, ResizeSettings settings, DecodeCallback callback)
        {
            return(callback(ref b, mayUnloadOriginal));//Tone mapping is disabled, not yet functional

            FIBITMAP m = FIBITMAP.Zero;

            try {
                var alg = settings.Get <ToneMappingAlgorithm>("fi.tonemap", ToneMappingAlgorithm.None);
                if (alg == ToneMappingAlgorithm.Drago)
                {
                    m = FreeImage.TmoDrago03(b, 2.2, 0);
                }
                else if (alg == ToneMappingAlgorithm.Reinhard)
                {
                    m = FreeImage.TmoReinhard05(b, 0, 0);
                }
                else if (alg == ToneMappingAlgorithm.Fattal)
                {
                    m = FreeImage.TmoFattal02(b, 0.5, 0.85);
                }
                else
                {
                    return(callback(ref b, mayUnloadOriginal));
                }
                if (mayUnloadOriginal)
                {
                    FreeImage.UnloadEx(ref b);
                }

                return(callback(ref m, true));
            } finally {
                if (!m.IsNull)
                {
                    FreeImage.UnloadEx(ref m);
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Decodes the given stream, selects the correct page or frame, rotates it correctly (if autorotate=true), then executes the callback, then cleans up.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="settings"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static object DecodeAndCall(Stream s, ResizeSettings settings, DecodeCallback callback)
        {
            if (!FreeImageAPI.FreeImage.IsAvailable())
            {
                return(null);
            }

            FREE_IMAGE_LOAD_FLAGS flags = FREE_IMAGE_LOAD_FLAGS.DEFAULT;

            //If we're not tone-mapping the raw file, convert it for display
            if (!HasToneMappingCommands(settings))
            {
                flags |= FREE_IMAGE_LOAD_FLAGS.RAW_DISPLAY;
            }

            bool usethumb = ("true".Equals(settings["usepreview"], StringComparison.OrdinalIgnoreCase));

            if (usethumb)
            {
                flags |= FREE_IMAGE_LOAD_FLAGS.RAW_PREVIEW;
            }

            bool autorotate = ("true".Equals(settings["autorotate"], StringComparison.OrdinalIgnoreCase));

            if (autorotate)
            {
                flags |= FREE_IMAGE_LOAD_FLAGS.JPEG_EXIFROTATE;
            }

            int page = 0;

            if (!string.IsNullOrEmpty(settings["page"]) && !int.TryParse(settings["page"], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out page))
            {
                page = 0;
            }

            int frame = 0;

            if (!string.IsNullOrEmpty(settings["frame"]) && !int.TryParse(settings["frame"], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out frame))
            {
                frame = 0;
            }

            if (page == 0 && frame != 0)
            {
                page = frame;
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            if (page > 1)
            {
                FREE_IMAGE_FORMAT fmt = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
                FIMULTIBITMAP     mb  = FreeImage.OpenMultiBitmapFromStream(s, ref fmt, flags);
                //Prevent asking for a non-existent page
                int pages = FreeImage.GetPageCount(mb);
                if (page > pages)
                {
                    page = pages;
                }
                try {
                    if (mb.IsNull)
                    {
                        return(null);
                    }
                    FIBITMAP bPage = FreeImage.LockPage(mb, page - 1);
                    if (bPage.IsNull)
                    {
                        return(null);
                    }
                    try {
                        sw.Stop();
                        return(ToneMap(ref bPage, false, settings, callback));
                    } finally {
                        FreeImage.UnlockPage(mb, bPage, false);
                    }
                } finally {
                    if (!mb.IsNull)
                    {
                        FreeImage.CloseMultiBitmapEx(ref mb, FREE_IMAGE_SAVE_FLAGS.DEFAULT);
                    }
                }
            }
            else
            {
                FIBITMAP original = FIBITMAP.Zero;
                try {
                    original = FreeImage.LoadFromStream(s, flags);
                    sw.Stop();
                    if (original.IsNull)
                    {
                        return(null);
                    }
                    return(ToneMap(ref original, true, settings, callback));
                } finally {
                    if (!original.IsNull)
                    {
                        FreeImage.UnloadEx(ref original);
                    }
                }
            }
        }
예제 #8
0
 public static void Decode(Bitmap b, DecodeOptions options, DecodeCallback Callback)
 {
     Decode(b, options, Callback, 0, null);
 }
예제 #9
0
        public static void Decode(
            Bitmap b,
            DecodeOptions options,
            DecodeCallback Callback,
            DiagnosticImageStyles diagnosticImageStyle, DecodeDiagnosticImageCallback DiagnosticImageCallback)
        {
            Exception decodeException = null;
            byte      status;

            try {
                int    bitmapStride;
                byte[] pxl = BitmapToByteArray(b, out bitmapStride);

                DmtxDiagnosticImageCallback diagnosticImageCallbackParam = null;
                if (DiagnosticImageCallback != null)
                {
                    diagnosticImageCallbackParam = delegate(IntPtr data, uint totalBytes, uint headerBytes) {
                        try {
                            byte[] pnmData = new byte[totalBytes];
                            Marshal.Copy(data, pnmData, 0, pnmData.Length);
                            using (MemoryStream pnmInputStream = new MemoryStream(pnmData)) {
                                Bitmap bm = PnmToBitmap(pnmInputStream);
                                DiagnosticImageCallback(bm);
                            }
                        } catch (Exception ex) {
                            decodeException = ex;
                        }
                    };
                }

                status = DmtxDecode(
                    pxl,
                    (UInt32)b.Width,
                    (UInt32)b.Height,
                    (UInt32)bitmapStride,
                    options,
                    diagnosticImageCallbackParam, diagnosticImageStyle,
                    delegate(DecodedInternal dmtxDecodeResult) {
                    DmtxDecoded result;
                    try {
                        result            = new DmtxDecoded();
                        result.Corners    = dmtxDecodeResult.Corners;
                        result.SymbolInfo = dmtxDecodeResult.SymbolInfo;
                        result.Data       = new byte[dmtxDecodeResult.DataSize];
                        for (int dataIdx = 0; dataIdx < dmtxDecodeResult.DataSize; dataIdx++)
                        {
                            result.Data[dataIdx] = Marshal.ReadByte(dmtxDecodeResult.Data, dataIdx);
                        }
                        Callback(result);
                        return(true);
                    } catch (Exception ex) {
                        decodeException = ex;
                        return(false);
                    }
                });
            } catch (Exception ex) {
                throw new DmtxException("Error calling native function.", ex);
            }
            if (decodeException != null)
            {
                throw decodeException;
            }
            if (status == RETURN_NO_MEMORY)
            {
                throw new DmtxOutOfMemoryException("Not enough memory.");
            }
            else if (status == RETURN_INVALID_ARGUMENT)
            {
                throw new DmtxInvalidArgumentException("Invalid options configuration.");
            }
            else if (status > 0)
            {
                throw new DmtxException("Unknown error.");
            }
        }
예제 #10
0
        /// <summary>
        /// Decodes the given stream, selects the correct page or frame, rotates it correctly (if autorotate=true), then executes the callback, then cleans up.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="settings"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static object DecodeAndCall(Stream s, ResizeSettings settings, DecodeCallback callback)
        {
            if (!FreeImageAPI.FreeImage.IsAvailable()) return null;

            FREE_IMAGE_LOAD_FLAGS flags = FREE_IMAGE_LOAD_FLAGS.DEFAULT;

            //If we're not tone-mapping the raw file, convert it for display
            if (!HasToneMappingCommands(settings)) flags |= FREE_IMAGE_LOAD_FLAGS.RAW_DISPLAY;

            bool usethumb = ("true".Equals(settings["usepreview"], StringComparison.OrdinalIgnoreCase));
            if (usethumb) flags |= FREE_IMAGE_LOAD_FLAGS.RAW_PREVIEW;

            bool autorotate = ("true".Equals(settings["autorotate"], StringComparison.OrdinalIgnoreCase));
            if (autorotate) flags |= FREE_IMAGE_LOAD_FLAGS.JPEG_EXIFROTATE;

            int page = 0;
            if (!string.IsNullOrEmpty(settings["page"]) && !int.TryParse(settings["page"], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out page))
                page = 0;

            int frame = 0;
            if (!string.IsNullOrEmpty(settings["frame"]) && !int.TryParse(settings["frame"], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out frame))
                frame = 0;

            if (page == 0 && frame != 0) page = frame;

            Stopwatch sw = new Stopwatch();
            sw.Start();

            if (page > 1) {
                FREE_IMAGE_FORMAT fmt = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
                FIMULTIBITMAP mb = FreeImage.OpenMultiBitmapFromStream(s, ref fmt, flags);
                //Prevent asking for a non-existent page
                int pages = FreeImage.GetPageCount(mb);
                if (page > pages) page = pages;
                try {
                    if (mb.IsNull) return null;
                    FIBITMAP bPage = FreeImage.LockPage(mb, page - 1);
                    if (bPage.IsNull) return null;
                    try {
                        sw.Stop();
                        return ToneMap(ref bPage, false, settings, callback);
                    } finally {
                        FreeImage.UnlockPage(mb, bPage, false);
                    }

                } finally {
                    if (!mb.IsNull) FreeImage.CloseMultiBitmapEx(ref mb, FREE_IMAGE_SAVE_FLAGS.DEFAULT);
                }

            } else {
                FIBITMAP original = FIBITMAP.Zero;
                try {
                    original = FreeImage.LoadFromStream(s, flags);
                    sw.Stop();
                    if (original.IsNull) return null;
                    return ToneMap(ref original, true, settings, callback);
                } finally {
                    if (!original.IsNull) FreeImage.UnloadEx(ref original);
                }
            }
        }
예제 #11
0
        private static object ToneMap(ref FIBITMAP b, bool mayUnloadOriginal, ResizeSettings settings, DecodeCallback callback)
        {
            return callback(ref b, mayUnloadOriginal);//Tone mapping is disabled, not yet functional

            FIBITMAP m = FIBITMAP.Zero;
            try {
                var alg = NameValueCollectionExtensions.Get<ToneMappingAlgorithm>(settings, "fi.tonemap", ToneMappingAlgorithm.None);
                if (alg == ToneMappingAlgorithm.Drago){
                    m = FreeImage.TmoDrago03(b, 2.2, 0);
                }else if (alg == ToneMappingAlgorithm.Reinhard){
                    m = FreeImage.TmoReinhard05(b, 0, 0);
                }else if (alg == ToneMappingAlgorithm.Fattal){
                    m = FreeImage.TmoFattal02(b, 0.5, 0.85);
                }else{
                    return callback(ref b, mayUnloadOriginal);
                }
                if (mayUnloadOriginal) FreeImage.UnloadEx(ref b);

                return callback(ref m, true);
            } finally {
                if (!m.IsNull) FreeImage.UnloadEx(ref m);
            }
        }
예제 #12
0
 public static extern IntPtr GetRtspPlayer(string rtspUri, DecodeCallback fDecodeCallback, MediaEndedCallback fMediaEndedCallback, ErrorCallback fErrorCallback, int timeout, IntPtr windowHandle);
예제 #13
0
 public static extern IntPtr GetLivePlayer(UInt16 wVendorType, DecodeCallback fDecodeCallback, EndInitCallback fEndInitCallback, String rtspPath, uint mode, IntPtr windowHandle);
예제 #14
0
 public static extern IntPtr GetPlayer(String pchFileName, UInt16 wVendorType, DecodeCallback fDecodeCallback, MediaEndedCallback fMediaEndedCallback, IntPtr windowHandle);
예제 #15
0
        public static void Decode(
            Bitmap b,
            DecodeOptions options,
            DecodeCallback Callback,
            DiagnosticImageStyles diagnosticImageStyle, DecodeDiagnosticImageCallback DiagnosticImageCallback)
        {
            Exception decodeException = null;
            byte status;
            try {
                int bitmapStride;
                byte[] pxl = BitmapToByteArray(b, out bitmapStride);

                DmtxDiagnosticImageCallback diagnosticImageCallbackParam = null;
                if (DiagnosticImageCallback != null) {
                    diagnosticImageCallbackParam = delegate(IntPtr data, uint totalBytes, uint headerBytes) {
                        try {
                            byte[] pnmData = new byte[totalBytes];
                            Marshal.Copy(data, pnmData, 0, pnmData.Length);
                            using (MemoryStream pnmInputStream = new MemoryStream(pnmData)) {
                                Bitmap bm = PnmToBitmap(pnmInputStream);
                                DiagnosticImageCallback(bm);
                            }
                        } catch (Exception ex) {
                            decodeException = ex;
                        }
                    };
                }

                status = DmtxDecode(
                    pxl,
                    (UInt32)b.Width,
                    (UInt32)b.Height,
                    (UInt32)bitmapStride,
                    options,
                    diagnosticImageCallbackParam, diagnosticImageStyle,
                    delegate(DecodedInternal dmtxDecodeResult) {
                        DmtxDecoded result;
                        try {
                            result = new DmtxDecoded();
                            result.Corners = dmtxDecodeResult.Corners;
                            result.SymbolInfo = dmtxDecodeResult.SymbolInfo;
                            result.Data = new byte[dmtxDecodeResult.DataSize];
                            for (int dataIdx = 0; dataIdx < dmtxDecodeResult.DataSize; dataIdx++) {
                                result.Data[dataIdx] = Marshal.ReadByte(dmtxDecodeResult.Data, dataIdx);
                            }
                            Callback(result);
                            return true;
                        } catch (Exception ex) {
                            decodeException = ex;
                            return false;
                        }
                    });
            } catch (Exception ex) {
                throw new DmtxException("Error calling native function.", ex);
            }
            if (decodeException != null) {
                throw decodeException;
            }
            if (status == RETURN_NO_MEMORY) {
                throw new DmtxOutOfMemoryException("Not enough memory.");
            } else if (status == RETURN_INVALID_ARGUMENT) {
                throw new DmtxInvalidArgumentException("Invalid options configuration.");
            } else if (status > 0) {
                throw new DmtxException("Unknown error.");
            }
        }
예제 #16
0
 public static void Decode(Bitmap b, DecodeOptions options, DecodeCallback Callback)
 {
     Decode(b, options, Callback, 0, null);
 }