예제 #1
0
        private unsafe PinnedByteArray GetGreyBytes(Bitmap bitmap)
        {
            var pixels = new PinnedByteArray(bitmap.Width * bitmap.Height);

            var data = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly,
                bitmap.PixelFormat);

            var srcComponents = bitmap.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4;

            var dstLine = (byte *)pixels.Pointer;
            var srcLine = (byte *)data.Scan0.ToPointer();

            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    var pixel = srcLine + j * srcComponents;
                    int grey  = (int)(pixel[0] * 0.3 + pixel[1] * 0.59 + pixel[2] * 0.11);
                    dstLine[j] = (byte)grey;
                }

                srcLine += data.Stride;
                dstLine += data.Width;
            }
            bitmap.UnlockBits(data);

            return(pixels);
        }
예제 #2
0
        private unsafe PinnedByteArray GetColorbytes(Bitmap bitmap)
        {
            var pixels = new PinnedByteArray(bitmap.Width * bitmap.Height * 3);

            var data = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly,
                bitmap.PixelFormat);

            var srcComponents = bitmap.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4;

            var dstLine = (byte *)pixels.Pointer;
            var srcLine = (byte *)data.Scan0.ToPointer();

            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    var srcPixel = srcLine + j * srcComponents;
                    var dstPixel = dstLine + j * 3;

                    //convert from bgr to rgb
                    dstPixel[0] = srcPixel[2];
                    dstPixel[1] = srcPixel[1];
                    dstPixel[2] = srcPixel[0];
                }

                srcLine += data.Stride;
                dstLine += data.Width * 3;
            }
            bitmap.UnlockBits(data);

            return(pixels);
        }
예제 #3
0
        MarshalStringArrayToIntPtrArray(String[] args,
                                        int argc)
        {
            Encoding encode = Encoding.Unicode;

            switch (CurrentEncodeing)
            {
            case P4Encoding.utf8:
                encode = Encoding.UTF8;
                break;

            case P4Encoding.utf16:
                encode = Encoding.Unicode;
                break;
            }

            PinnedByteArray[] args_b = new PinnedByteArray[argc];

            for (int i = 0; i < argc; i++)
            {
                //null terminate the strings
                byte[] arg_b = encode.GetBytes(args[i] + '\0');
                args_b[i] = new PinnedByteArray(arg_b);
            }
            return(new PinnedByteArrays(args_b));
        }
예제 #4
0
        private unsafe PinnedByteArray GetGreyBytes(Bitmap bitmap)
        {
            var pixels = new PinnedByteArray(bitmap.Width * bitmap.Height);

            var data = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly,
                bitmap.PixelFormat);

            var srcComponents = bitmap.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4;

            var dstLine = (byte *)pixels.Pointer;
            var srcLine = (byte *)data.Scan0.ToPointer();

            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    var pixel = srcLine + j * srcComponents;
                    // int grey = (int)(pixel[0] * 0.3 + pixel[1] * 0.59 + pixel[2] * 0.11);
                    // dstLine[j] = (byte)grey;
                    // see https://www.cnblogs.com/carekee/articles/3629964.html
                    dstLine[j] = (byte)((pixel[0] * 38 + pixel[1] * 75 + pixel[2] * 15) >> 7);
                }

                srcLine += data.Stride;
                dstLine += data.Width;
            }
            bitmap.UnlockBits(data);

            return(pixels);
        }
예제 #5
0
        public override void Decode(DicomPixelData oldPixelData, DicomPixelData newPixelData, DicomCodecParams parameters)
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                throw new InvalidOperationException("Unsupported OS Platform");
            }

            for (int frame = 0; frame < oldPixelData.NumberOfFrames; frame++)
            {
                IByteBuffer jpegData = oldPixelData.GetFrame(frame);

                //Converting photmetricinterpretation YbrFull or YbrFull422 to RGB
                if (oldPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrFull)
                {
                    jpegData = PixelDataConverter.YbrFullToRgb(jpegData);
                    oldPixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb;
                }
                else if (oldPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrFull422)
                {
                    jpegData = PixelDataConverter.YbrFull422ToRgb(jpegData, oldPixelData.Width);
                    oldPixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb;
                }

                PinnedByteArray jpegArray = new PinnedByteArray(jpegData.Data);

                byte[] frameData = new byte[newPixelData.UncompressedFrameSize];

                PinnedByteArray frameArray = new PinnedByteArray(frameData);

                JlsParameters jls = new JlsParameters();

                char[] errorMessage = new char[256];

                // IMPORT JpegLsDecode
                unsafe
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        CharlsApiResultType err = JpegLSDecode_Linux64((void *)frameArray.Pointer, frameData.Length, (void *)jpegArray.Pointer, Convert.ToUInt32(jpegData.Size), ref jls, errorMessage);
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        CharlsApiResultType err = JpegLSDecode_Windows64((void *)frameArray.Pointer, frameData.Length, (void *)jpegArray.Pointer, Convert.ToUInt32(jpegData.Size), ref jls, errorMessage);
                    }

                    IByteBuffer buffer;
                    if (frameData.Length >= (1 * 1024 * 1024) || oldPixelData.NumberOfFrames > 1)
                    {
                        buffer = new TempFileBuffer(frameData);
                    }
                    else
                    {
                        buffer = new MemoryByteBuffer(frameData);
                    }
                    buffer = EvenLengthBuffer.Create(buffer);

                    newPixelData.AddFrame(buffer);
                }
            }
        }
예제 #6
0
 /// <summary>
 /// Adds a new entry in the map
 /// </summary>
 /// <param name="lr">String representing both the the left and right sides of
 /// the new entry</param>
 /// <param name="t">Type of the new entry</param>
 public void Insert(String lr, P4MapApi.Type t)
 {
     if (UseUnicode)
     {
         using (PinnedByteArray plr = MarshalStringToIntPtr(lr))
         {
             InsertW(pMapApi, plr, t);
         }
     }
     else
     {
         InsertA(pMapApi, lr, t);
     }
 }
예제 #7
0
 /// <summary>
 /// Adds a new entry in the map
 /// </summary>
 /// <param name="left">String representing the the left side of the new entry</param>
 /// <param name="right">String representing the the right side of the new entry</param>
 /// <param name="type">Type of the new entry</param>
 public void Insert(String left, String right, P4MapApi.Type type)
 {
     if (UseUnicode)
     {
         using (PinnedByteArray pLeft = MarshalStringToIntPtr(left),
                pRight = MarshalStringToIntPtr(right))
         {
             InsertW(pMapApi, pLeft, pRight, type);
         }
     }
     else
     {
         InsertA(pMapApi, left, right, type);
     }
 }
예제 #8
0
        /// <summary>
        ///  Translate a file path from on side of the mapping to the other
        /// </summary>
        /// <param name="path">The path to translate</param>
        /// <param name="direction">The direction to perform the translation L->R or R->L</param>
        /// <returns>Translated path, Null if no translation (path is not mapped)</returns>
        public String Translate(String path, Direction direction)
        {
            IntPtr pStr = IntPtr.Zero;

            if (UseUnicode)
            {
                using (PinnedByteArray pPath = MarshalStringToIntPtr(path))
                {
                    pStr = TranslateW(pMapApi, pPath, direction);
                }
            }
            else
            {
                pStr = TranslateA(pMapApi, path, direction);
            }

            if (pStr != IntPtr.Zero)
            {
                String translation = MarshalPtrToString(pStr);
                P4Bridge.ReleaseString(pStr);
                return(translation);
            }
            return(null);
        }
예제 #9
0
        private unsafe PinnedByteArray GetColorbytes(Bitmap bitmap)
        {
            var pixels = new PinnedByteArray(bitmap.Width * bitmap.Height * 3);

            var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);

            var srcComponents = bitmap.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4;

            var dstLine = (byte*)pixels.Pointer;
            var srcLine = (byte*)data.Scan0.ToPointer();

            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    var srcPixel = srcLine + j * srcComponents;
                    var dstPixel = dstLine + j * 3;

                    //convert from bgr to rgb
                    dstPixel[0] = srcPixel[2];
                    dstPixel[1] = srcPixel[1];
                    dstPixel[2] = srcPixel[0];
                }

                srcLine += data.Stride;
                dstLine += data.Width * 3;
            }
            bitmap.UnlockBits(data);

            return pixels;
        }
예제 #10
0
        private unsafe PinnedByteArray GetGreyBytes(Bitmap bitmap)
        {
            var pixels = new PinnedByteArray(bitmap.Width * bitmap.Height);

            var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);

            var srcComponents = bitmap.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4;

            var dstLine = (byte*)pixels.Pointer;
            var srcLine = (byte*)data.Scan0.ToPointer();

            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    var pixel = srcLine + j * srcComponents;
                    int grey = (int)(pixel[0] * 0.3 + pixel[1] * 0.59 + pixel[2] * 0.11);
                    dstLine[j] = (byte)grey;
                }

                srcLine += data.Stride;
                dstLine += data.Width;
            }
            bitmap.UnlockBits(data);

            return pixels;
        }
예제 #11
0
        public override unsafe void Encode(DicomPixelData oldPixelData, DicomPixelData newPixelData, DicomCodecParams parameters)
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                throw new InvalidOperationException("Unsupported OS Platform");
            }

            if ((oldPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrPartial422) ||
                (oldPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrPartial420))
            {
                throw new DicomCodecException("Photometric Interpretation '{0}' not supported by JPEG-LS encoder", oldPixelData.PhotometricInterpretation);
            }
            DicomJpegLsParams jparams = (DicomJpegLsParams)parameters;

            if (jparams == null)
            {
                jparams = (DicomJpegLsParams)GetDefaultParameters();
            }

            //IMPORT JLSPARAMETERS (DLLIMPORT)

            JlsParameters jls = new JlsParameters
            {
                width          = oldPixelData.Width,
                height         = oldPixelData.Height,
                bitsPerSample  = oldPixelData.BitsStored,
                stride         = oldPixelData.BytesAllocated * oldPixelData.Width * oldPixelData.SamplesPerPixel,
                components     = oldPixelData.SamplesPerPixel,
                interleaveMode = oldPixelData.SamplesPerPixel == 1
                    ? CharlsInterleaveModeType.None
                    : oldPixelData.PlanarConfiguration == PlanarConfiguration.Interleaved
                        ? CharlsInterleaveModeType.Sample
                        : CharlsInterleaveModeType.Line,
                colorTransformation = CharlsColorTransformationType.None
            };

            if (TransferSyntax == DicomTransferSyntax.JPEGLSNearLossless)
            {
                jls.allowedLossyError = jparams.AllowedError;
            }

            for (int frame = 0; frame < oldPixelData.NumberOfFrames; frame++)
            {
                IByteBuffer frameData = oldPixelData.GetFrame(frame);

                //Converting photmetricinterpretation YbrFull or YbrFull422 to RGB
                if (oldPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrFull)
                {
                    frameData = PixelDataConverter.YbrFullToRgb(frameData);
                    oldPixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb;
                }
                else if (oldPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrFull422)
                {
                    frameData = PixelDataConverter.YbrFull422ToRgb(frameData, oldPixelData.Width);
                    oldPixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb;
                }

                PinnedByteArray frameArray = new PinnedByteArray(frameData.Data);

                byte[] jpegData = new byte[frameData.Size];

                PinnedByteArray jpegArray    = new PinnedByteArray(jpegData);
                uint            jpegDataSize = 0;

                char[] errorMessage = new char[256];

                // IMPORT JpegLsEncode
                unsafe {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        CharlsApiResultType err = JpegLSEncode_Linux64((void *)jpegArray.Pointer, checked ((uint)jpegArray.Count), &jpegDataSize, (void *)frameArray.Pointer, checked ((uint)frameArray.Count), ref jls, errorMessage);
                    }

                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        CharlsApiResultType err = JpegLSEncode_Windows64((void *)jpegArray.Pointer, checked ((uint)jpegArray.Count), &jpegDataSize, (void *)frameArray.Pointer, checked ((uint)frameArray.Count), ref jls, errorMessage);
                    }

                    Array.Resize(ref jpegData, (int)jpegDataSize);

                    IByteBuffer buffer;
                    if (jpegDataSize >= (1 * 1024 * 1024) || oldPixelData.NumberOfFrames > 1)
                    {
                        buffer = new TempFileBuffer(jpegData);
                    }
                    else
                    {
                        buffer = new MemoryByteBuffer(jpegData);
                    }
                    buffer = EvenLengthBuffer.Create(buffer);
                    newPixelData.AddFrame(buffer);
                }
            }
        }