Beispiel #1
0
        private static string GetErrorMessage(JpegLSError result)
        {
            var message = Environment.Is64BitProcess ?
                          SafeNativeMethods.CharLSGetErrorMessageX64((int)result) :
                          SafeNativeMethods.CharLSGetErrorMessageX86((int)result);

            return(Marshal.PtrToStringAnsi(message));
        }
Beispiel #2
0
        private static void JpegLsReadHeaderThrowWhenError(byte[] source, int length, out JlsParameters info)
        {
            Contract.Requires(source != null);

            var         errorMessage = new StringBuilder(256);
            JpegLSError result       = Is64BitProcess ?
                                       SafeNativeMethods.JpegLsReadHeader64(source, length, out info, errorMessage) :
                                       SafeNativeMethods.JpegLsReadHeader(source, length, out info, errorMessage);

            HandleResult(result, errorMessage);
        }
Beispiel #3
0
        private static void HandleResult(JpegLSError result, StringBuilder errorMessage)
        {
            Exception exception;

            switch (result)
            {
            case JpegLSError.None:
                return;

            case JpegLSError.TooMuchCompressedData:
            case JpegLSError.InvalidJlsParameters:
            case JpegLSError.InvalidCompressedData:
            case JpegLSError.CompressedBufferTooSmall:
            case JpegLSError.ImageTypeNotSupported:
            case JpegLSError.UnsupportedBitDepthForTransform:
            case JpegLSError.UnsupportedColorTransform:
            case JpegLSError.UnsupportedEncoding:
            case JpegLSError.UnknownJpegMarker:
            case JpegLSError.MissingJpegMarkerStart:
            case JpegLSError.UnspecifiedFailure:
                exception = new InvalidDataException(errorMessage.ToString());
                break;

            case JpegLSError.UncompressedBufferTooSmall:
            case JpegLSError.ParameterValueNotSupported:
                exception = new ArgumentException(errorMessage.ToString());
                break;

            case JpegLSError.UnexpectedFailure:
                exception = new InvalidOperationException("Unexpected failure. The state of the implementation may be invalid.");
                break;

            default:
                exception = new NotImplementedException(string.Format(CultureInfo.InvariantCulture,
                                                                      "The native codec has returned an unexpected result value: {0}", result));
                break;
            }

            var data = exception.Data;

            Contract.Assume(data != null);

            // ReSharper disable once PossibleNullReferenceException
            data.Add("JpegLSError", result);
            throw exception;
        }
Beispiel #4
0
        /// <summary>
        /// Decompresses the JPEG-LS encoded data passed in the source byte array into the destination array.
        /// </summary>
        /// <param name="source">The byte array that contains the JPEG-LS encoded data to decompress.</param>
        /// <param name="count">The number of bytes of the array to decompress.</param>
        /// <param name="pixels">The byte array that will hold the pixels when the function returns.</param>
        /// <exception cref="ArgumentNullException">source -or- pixels is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">count contains an invalid value.</exception>
        /// <exception cref="ArgumentException">Thrown when the destination array is too small to hold the decompressed pixel data.</exception>
        /// <exception cref="InvalidDataException">Thrown when the source array contains an invalid encodeded JPEG-LS bit stream.</exception>
        public static void Decompress(byte[] source, int count, byte[] pixels)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (count < 0 || count > source.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count), "count < 0 || count > source.Length");
            }
            if (pixels == null)
            {
                throw new ArgumentNullException(nameof(pixels));
            }
            Contract.EndContractBlock();

            var         errorMessage = new StringBuilder(256);
            JpegLSError error        = Is64BitProcess ?
                                       SafeNativeMethods.JpegLsDecode64(pixels, pixels.Length, source, count, IntPtr.Zero, errorMessage) :
                                       SafeNativeMethods.JpegLsDecode(pixels, pixels.Length, source, count, IntPtr.Zero, errorMessage);

            HandleResult(error, errorMessage);
        }
        private static string GetErrorMessage(JpegLSError error)
        {
            IntPtr message = CharLSGetErrorMessage((int)error);

            return(Marshal.PtrToStringAnsi(message) ?? string.Empty);
        }
        internal static void HandleJpegLSError(JpegLSError error)
        {
            Exception exception;

            switch (error)
            {
            case JpegLSError.None:
                return;

            case JpegLSError.TooMuchEncodedData:
            case JpegLSError.ParameterValueNotSupported:
            case JpegLSError.InvalidEncodedData:
            case JpegLSError.SourceBufferTooSmall:
            case JpegLSError.BitDepthForTransformNotSupported:
            case JpegLSError.ColorTransformNotSupported:
            case JpegLSError.EncodingNotSupported:
            case JpegLSError.UnknownJpegMarkerFound:
            case JpegLSError.JpegMarkerStartByteNotFound:
            case JpegLSError.StartOfImageMarkerNotFound:
            case JpegLSError.StartOfFrameMarkerNotFound:
            case JpegLSError.InvalidMarkerSegmentSize:
            case JpegLSError.DuplicateStartOfImageMarker:
            case JpegLSError.DuplicateStartOfFrameMarker:
            case JpegLSError.DuplicateComponentIdInStartOfFrameSegment:
            case JpegLSError.UnexpectedEndOfImageMarker:
            case JpegLSError.InvalidJpegLSPresetParameterType:
            case JpegLSError.JpeglsPresetExtendedParameterTypeNotSupported:
            case JpegLSError.MissingEndOfSpiffDirectory:
            case JpegLSError.InvalidParameterWidth:
            case JpegLSError.InvalidParameterHeight:
            case JpegLSError.InvalidParameterComponentCount:
            case JpegLSError.InvalidParameterBitsPerSample:
            case JpegLSError.InvalidParameterInterleaveMode:
            case JpegLSError.UnexpectedFailure:
            case JpegLSError.NotEnoughMemory:
                exception = new InvalidDataException(GetErrorMessage(error));
                break;

            case JpegLSError.InvalidArgument:
            case JpegLSError.DestinationBufferTooSmall:
                exception = new ArgumentException(GetErrorMessage(error));
                break;

            case JpegLSError.InvalidArgumentWidth:
            case JpegLSError.InvalidArgumentHeight:
            case JpegLSError.InvalidArgumentComponentCount:
            case JpegLSError.InvalidArgumentBitsPerSample:
            case JpegLSError.InvalidArgumentInterleaveMode:
            case JpegLSError.InvalidArgumentNearLossless:
            case JpegLSError.InvalidArgumentPresetCodingParameters:
            case JpegLSError.InvalidArgumentSpiffEntrySize:
            case JpegLSError.InvalidArgumentColorTransformation:
                exception = new ArgumentOutOfRangeException(GetErrorMessage(error));
                break;

            case JpegLSError.InvalidOperation:
                exception = new InvalidOperationException(GetErrorMessage(error));
                break;

            default:
                Debug.Assert(false, "C# and native implementation mismatch");

                // ReSharper disable once HeuristicUnreachableCode
                exception = new InvalidOperationException(GetErrorMessage(error));
                break;
            }

            exception.Data.Add(nameof(JpegLSError), error);
            throw exception;
        }
Beispiel #7
0
        private static void HandleResult(JpegLSError result, StringBuilder errorMessage)
        {
            Exception exception;

            switch (result)
            {
                case JpegLSError.None:
                    return;

                case JpegLSError.TooMuchCompressedData:
                case JpegLSError.InvalidJlsParameters:
                case JpegLSError.InvalidCompressedData:
                case JpegLSError.CompressedBufferTooSmall:
                case JpegLSError.ImageTypeNotSupported:
                case JpegLSError.UnsupportedBitDepthForTransform:
                case JpegLSError.UnsupportedColorTransform:
                case JpegLSError.UnsupportedEncoding:
                case JpegLSError.UnknownJpegMarker:
                case JpegLSError.MissingJpegMarkerStart:
                case JpegLSError.UnspecifiedFailure:
                    exception = new InvalidDataException(errorMessage.ToString());
                    break;

                case JpegLSError.UncompressedBufferTooSmall:
                case JpegLSError.ParameterValueNotSupported:
                    exception = new ArgumentException(errorMessage.ToString());
                    break;

                case JpegLSError.UnexpectedFailure:
                    exception = new InvalidOperationException("Unexpected failure. The state of the implementation may be invalid.");
                    break;

                default:
                    exception = new NotImplementedException(string.Format(CultureInfo.InvariantCulture,
                        "The native codec has returned an unexpected result value: {0}", result));
                    break;
            }

            var data = exception.Data;
            Contract.Assume(data != null);
            data.Add("JpegLSError", result);
            throw exception;
        }
Beispiel #8
0
        private static void HandleResult(JpegLSError result)
        {
            Exception exception;

            switch (result)
            {
            case JpegLSError.None:
                return;

            case JpegLSError.TooMuchEncodedData:
            case JpegLSError.ParameterValueNotSupported:
            case JpegLSError.InvalidEncodedData:
            case JpegLSError.SourceBufferTooSmall:
            case JpegLSError.BitDepthForTransformNotSupported:
            case JpegLSError.ColorTransformNotSupported:
            case JpegLSError.EncodingNotSupported:
            case JpegLSError.UnknownJpegMarkerFound:
            case JpegLSError.JpegMarkerStartByteNotFound:
            case JpegLSError.StartOfImageMarkerNotFound:
            case JpegLSError.StartOfFrameMarkerNotFound:
            case JpegLSError.InvalidMarkerSegmentSize:
            case JpegLSError.DuplicateStartOfImageMarker:
            case JpegLSError.DuplicateStartOfFrameMarker:
            case JpegLSError.DuplicateComponentIdInStartOfFrameSegment:
            case JpegLSError.UnexpectedEndOfImageMarker:
            case JpegLSError.InvalidJpeglsPresetParameterType:
            case JpegLSError.JpeglsPresetExtendedParameterTypeNotSupported:
            case JpegLSError.InvalidParameterWidth:
            case JpegLSError.InvalidParameterHeight:
            case JpegLSError.InvalidParameterComponentCount:
            case JpegLSError.InvalidParameterBitsPerSample:
            case JpegLSError.InvalidParameterInterleaveMode:
                exception = new InvalidDataException(GetErrorMessage(result));
                break;

            case JpegLSError.InvalidArgument:
            case JpegLSError.DestinationBufferTooSmall:
            case JpegLSError.InvalidArgumentWidth:
            case JpegLSError.InvalidArgumentHeight:
            case JpegLSError.InvalidArgumentComponentCount:
            case JpegLSError.InvalidArgumentBitsPerSample:
            case JpegLSError.InvalidArgumentInterleaveMode:
            case JpegLSError.InvalidArgumentDestination:
            case JpegLSError.InvalidArgumentSource:
            case JpegLSError.InvalidArgumentThumbnail:
                exception = new ArgumentException(GetErrorMessage(result));
                break;

            case JpegLSError.UnexpectedFailure:
                exception = new InvalidOperationException(GetErrorMessage(result));
                break;

            default:
                Debug.Assert(false, "C# and native implementation mismatch");

                // ReSharper disable once HeuristicUnreachableCode
                exception = new InvalidOperationException(GetErrorMessage(result));
                break;
            }

            var data = exception.Data;

            // ReSharper disable once PossibleNullReferenceException
            data.Add(nameof(JpegLSError), result);
            throw exception;
        }