none
how to create compressed dcm file from jpg? using Clearcanvas.Dicom.Codec.Jpeg.dll RRS feed

  • General discussion

  • Hi all, 
    I have mentioned followed code and tool to develop jpg to compressed DCM.
    Using IDE: VS2008 
    Clearcanvas library 
    1.JPEG import from local folder binding Patient metadata 
    Step 1: Transfer Syntax
     /// <summary>String representing
            /// <para>JPEG Baseline (Process 1): Default Transfer Syntax for Lossy JPEG 8 Bit Image Compression</para>
            /// <para>UID: 1.2.840.10008.1.2.4.50</para>
            /// </summary>
            public static readonly String JpegBaselineProcess1Uid = "1.2.840.10008.1.2.4.50";
            /// <summary>TransferSyntax object representing
            /// <para>JPEG Baseline (Process 1): Default Transfer Syntax for Lossy JPEG 8 Bit Image Compression</para>
            /// <para>UID: 1.2.840.10008.1.2.4.50</para>
            /// </summary>
            public static readonly TransferSyntax JpegBaselineProcess1 =
                        new TransferSyntax("JPEG Baseline (Process 1): Default Transfer Syntax for Lossy JPEG 8 Bit Image Compression",
                                     JpegBaselineProcess1Uid,
                                     true, // Little Endian?
                                     true, // Encapsulated?
                                     true, // Explicit VR?
                                     false, // Deflated?
                                     true, // lossy?
                                     false // lossless?
                                     ); 
    Step 2 JPG to compressed DCM 

          i).JPG image converts to bitmap. 
          ii).Bitmap convert to a byte array.
          iii).Byte array values passed to DicomCompressedPixelData
    public bool SetPixelData(Image <g class="gr_ gr_39 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="39" id="39">currimage</g>)
            {
                this._modulestring = "SetPixelData";
                Bitmap bmp = (Bitmap)currimage;
                try
                {
                    if (bmp == null)
                    {
                        this.LogErrorString("invalid image sent to the function");
                        return false;
                    }
                    byte[] rawbytesfrombmp = BitmapToByteArray(bmp); 
                    //Bitmap bmpfds = ByteArrayToBitmap(rawbytesfrombmp, 4288, 2848);
                    //bmpfds.Save(@"C:\Users\Rajkumar\Desktop\Mockup\sakthi\newonetw.jpg", ImageFormat.Bmp);
                    if (rawbytesfrombmp == null)
                    {
                        this.LogErrorString("unable to get raw bytes from BMP image");
                        return false;
                    }
                    ImagePixelModuleInfo imagePixelModuleInformation = new ImagePixelModuleInfo();
                    this._modulestring = "ImagePixelModuleWrite";
                    if (((bmp.PixelFormat == PixelFormat.Format24bppRgb) || (bmp.PixelFormat == PixelFormat.Format32bppArgb)) || (bmp.PixelFormat == PixelFormat.Format32bppRgb))
                    {
                        ushort height = (ushort)bmp.Height;
                        ushort width = (ushort)bmp.Width;
                        if (((height % 2) != 0) && ((width % 2) != 0))
                        {
                            width = (ushort)(width - 1);
                        }                   
                        if ((bmp.PixelFormat == PixelFormat.Format24bppRgb) || (bmp.PixelFormat == PixelFormat.Format32bppArgb) || (bmp.PixelFormat == PixelFormat.Format32bppRgb))
                        {
                                            
                            /////////////////////////////////////end///////////////////////////////////////
                            DicomCompressedPixelData fragments = new DicomCompressedPixelData(_dcmfile, rawbytesfrombmp);  
                            fragments.ImageWidth = width;//(ushort)height;
                            fragments.ImageHeight = height; //(ushort)image.Height;
                            fragments.BitsStored = 8;//(ushort)bitsPerPixel;
                            fragments.BitsAllocated = 8;//(ushort)bitsPerPixel;
                            fragments.HighBit = 7;
                            fragments.SamplesPerPixel = 3;
                            fragments.PlanarConfiguration = 0;
                            fragments.PhotometricInterpretation = "YBR_FULL_422";                        
                            fragments.UpdateMessage(_dcmfile);                                    ///
                            
                        }
    }
    }
    }
                catch (Exception exception)
                {
                    this.LogErrorString(exception.Message);
                    return false;
                }
                return true;
    }
     public byte[] BitmapToByteArray(Bitmap img)      //img is the input image. Image width and height in pixels. PixelFormat is 24 bit per pixel in this case
            {
                try
                {
                    BitmapData bmpData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, img.PixelFormat);    //define and lock the area of the picture with rectangle
                    int pixelbytes = Image.GetPixelFormatSize(img.PixelFormat) / 8;     //for 24 bpp the value of pixelbytes is 3, for 32 bpp is 4, for 8 bpp is 1
                    IntPtr ptr = bmpData.Scan0;      //this is a memory address, where the bitmap starts
                    Int32 psize = bmpData.Stride * bmpData.Height;      // picture size in bytes
                    byte[] byOut = new byte[psize];     //create the output byte array, which size is obviously the same as the input one
                    System.Runtime.InteropServices.Marshal.Copy(ptr, byOut, 0, psize);      //this is a very fast method, which copies the memory content to byteOut array, but implemented for 24 bpp pictures only
                    img.UnlockBits(bmpData);      //release the locked memory
                    return byOut;//  e finita la commedia
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
     Create Dicom successfully but open in DCM dcmtk open sources
    1. Open DCM file using dcmdump open successfully 
    C:\Windows\system32>dcmdump D:\Temp\DICOM\20190304\1.3.6.1.4.1.25403.70975893642847.8012.20190304030543.1\1.3.6.1.4.1.25403.70975893642847.8012.20190304030543.1.0.dcm
    mentioned pixel dataset using DicomCompressedPixelData

    Pixeldataset.PNG

    2. Error get
    C:\Windows\system32>dcmdjpeg D:\Temp\DICOM\20190304\1.3.6.1.4.1.25403.70975893642847.8012.20190304030543.1\1.3.6.1.4.1.25403.70975893642847.8012.20190304030543.1.0.dcm D:\out\ku.dcm
    E: found invalid marker in JPEG stream while scanning for bit depth: 0x0000
    F: Pixel representation cannot be changed: decompressing file: D:\Projects\Fourus\PellucidFourusDicomApi\PellucidForusApi\bin\Debug\Temp\DICOM\20190304\1.3.6.1.4.1.25403.70975893642847.8012.20190304030543.1\1.3.6.1.4.1.25403.70975893642847.8012.20190304030543.1.0.dcm
    F: Input transfer syntax JPEG Baseline not supported

    so help me if have any idea just share, how to compressed DCM creation from jpg 

    Rajkumar P

    Wednesday, March 6, 2019 5:50 AM