Пример #1
0
            private HRESULT SetShift(Int16 item)
            {
                try
                {
                    UInt16[] buf_total = new UInt16[itemWidth * height_frame_bmp];
                    int      currentX  = item * coefficient * width_frame_bmp;

                    FDataModification dataTransform = new FDataModification();
                    dataTransform.SetMode(MODE.Shift);
                    dataTransform.SetBufTotalToBufForBitmap(ModeEdit.ShiftValue, height_frame_bmp * currentX, height_frame_bmp * itemWidth, 0, ref buf_total);

                    if (bUseAdjustment)
                    {
                        dataTransform.SetAdjustmentParameters(bUseAdjustment, item);
                    }
                    int itemFramesCount = itemWidth / FData.width_frame_bmp;

                    bmp = new Bitmap(itemWidth, height_frame_bmp, PixelFormat.Format48bppRgb);;
                    dataTransform.SetBitmap(itemFramesCount, ref buf_total, ref bmp);
                }
                catch (Exception e)
                {
                    if (FData.Result == HRESULT.S_OK)
                    {
                        GC.Collect();
                        MessageBox.Show("Ошибка при изменении разрядности. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    return(HRESULT.S_ERROR);
                }

                return(HRESULT.S_OK);
            }
        // This function is used to transform bite buffer in int and to bitmap by reading data from file
        private HRESULT FDataConvertion_BitmapConversion(ref Byte[] bufP)
        {
            buf_total_int = new UInt32[height_frame_bmp * width_frame_bmp * framesCount];

            nSumBufTotalInt = 0;

            Result = FDataConvertion_BufferConversion(HEADER_SIZE, ref bufP, ref buf_total_int);
            if (Result != HRESULT.S_OK)
            {
                return(Result);
            }

            /*    test                     UInt32 temp_data = 0;
             *                     temp_data = buf_total_int[i] >> stSerOb.DataShift;
             *                     if (temp_data > 0xFF)
             *                         buf_total[i] = (byte)(0xFF);
             *                     else
             *                         buf_total[i] = (byte)temp_data;
             *                     if (stSerOb.DataPositive)
             *                         buf_total[i] = (byte)(buf_total[i] ^ 0xFF);
             *                 }
             *                 buf_total_bitmap = new Byte[buf_total.Length * 3];
             *
             *                   if (bit != null) { bit.Dispose(); }
             *                 bit = new Bitmap(buf_total.Length / num_diods, num_diods, PixelFormat.Format24bppRgb);
             *                 Cursor.Current = Cursors.WaitCursor;
             *               DrawScreen_FromFile_Short(num_diods, ref buf_total, ref buf_total_bitmap, ref bit);
             * //                    FWait formWait = new FWait(num_diods, ref buf_total, ref buf_total_bitmap, ref bit);
             * //                   formWait.ShowDialog();
             * //                    bit.SetPixel(0, bit.Height - 5, Color.Red);
             * //                    bit.SetPixel(0, bit.Height - 4, Color.Red);
             * //                    bit.SetPixel(0, bit.Height - 3, Color.Red);
             * //                    bit.SetPixel(0, bit.Height - 2, Color.Red);
             * //                    bit.SetPixel(0, bit.Height - 1, Color.Red);
             *                 im = bit;
             * test
             */
            bmp = null;
            // Number of items in all image
            int rem;

            itemsCount = (Int16)(Math.DivRem(framesCount, coefficient, out rem));
            bmp        = new Bitmap[itemsCount + (rem > 0 ? 1 : 0)];

            FDataModification dataModification = new FDataModification();

            dataModification.SetMode(Mode);

            if (bmp != null)
            {
                int itemWidthCurrent = itemWidth;
                itemWidthLast = (Int16)(rem * width_frame_bmp);
                int itemFramesCountCurrent = coefficient;
                try
                {
                    for (int item = 0; item < bmp.Length; item++)
                    {
                        if (item == itemsCount)
                        {
                            itemWidthCurrent       = itemWidthLast;
                            itemFramesCountCurrent = rem;
                        }

                        UInt16[] buf_total_item = new UInt16[itemWidthCurrent * height_frame_bmp];
                        int      currentX       = item * coefficient * width_frame_bmp;

                        Result = dataModification.SetBufTotalToBufForBitmap((int)nShift.Value, height_frame_bmp * currentX, height_frame_bmp * itemWidth, 0, ref buf_total_item);
                        if (Result != HRESULT.S_OK)
                        {
                            MessageBox.Show("Error by set the bitmap buffer.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return(Result);
                        }

                        bmp[item] = new Bitmap(itemWidthCurrent, height_frame_bmp, PixelFormat.Format48bppRgb);

                        if (stSerOb.UseAdjustment)
                        {
                            dataModification.SetAdjustmentParameters(stSerOb.UseAdjustment, item);
                        }

                        // Set frames in bitmap array item
                        Result = dataModification.SetBitmap(itemFramesCountCurrent, ref buf_total_item, ref bmp[item]);
                        if (Result != HRESULT.S_OK)
                        {
                            DisposeBitmapArray(ref bmp);
                            MessageBox.Show("Error by set the bitmap.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return(Result);
                        }
                    }
                }
                catch (Exception e)
                {
                    DisposeBitmapArray(ref bmp);
                    MessageBox.Show("Error by set the bitmap.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(HRESULT.S_ERROR);
                }
            }
            return(HRESULT.S_OK);
        }
        private HRESULT Scan()
        {
            StringBuilder prefixScanFileName = new StringBuilder(stSerOb.ServerScanDataFolderName + "\\rmu1_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"));
            StringBuilder serverUri          = new StringBuilder(stSerOb.ServerUri);

            IAsyncResult initDeviceRes = null;

            ScanResultError = 0;
            lock (progressBar)
            {
                progressBar.Value = 0;
            }
            nSumBufTotalInt = 0;
            UInt16[] buf_frame     = null;
            UInt32[] buf_frame_int = null;
            //Int32 overlap = (290 * (45 - stSerOb.OverlapZone)) / 45;//110417
            Int32 overlap = 390 - stSerOb.OverlapZone;     //step motor steps 370 - 720

            try
            {
                // XML-RPC initialization RMU1 device on server
                initDeviceRes = delegateInitDevice.BeginInvoke(serverUri, framesCount, stSerOb.VolltageSource, stSerOb.CurrentSource, stSerOb.ExpositionTimeFrame, stSerOb.TargetTimeFrame, overlap, Convert.ToInt32(stSerOb.Reverse), prefixScanFileName, callbackInitDevice, null);

                buf_frame     = new UInt16[width_frame_bmp * height_frame_bmp];
                buf_frame_int = new UInt32[(width_frame_bmp + stSerOb.CropZone * 2) * height_frame_bmp];
            }
            catch (Exception e)
            {
                MessageBox.Show("Error by array initialization or XML-RPC initialization RMU1 device on server. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Result = HRESULT.S_ERROR;
            }

            if (arAvgFrame.Count != 0)
            {
                arAvgFrame.Clear();
            }

            current_frame = 0;
            int   rem;
            Int16 itemsCount = (Int16)Math.DivRem(framesCount, coefficient, out rem);

            if (rem == 0)
            {
                rem = coefficient;
            }
            itemWidthLast = (Int16)(rem * width_frame_bmp);

            if (im != null)
            {
                lock (im)
                {
                    im.Dispose();
                    im = null;
                }
            }
            DisposeBitmapArray(ref bmp);

            if (buf_total_int != null)
            {
                buf_total_int = null;
            }

            // Set timeout in the case of connection check call for prevent server fail
            if (connectionCheckProcessing)
            {
                Debug.WriteLine("connection device end wait");
                Thread.Sleep(timerConnection.Interval / 2);
            }

            stateTimer.Elapsed += OnTimeoutExceptionEvent;

            // Цикл покадрового считывания
            while ((ScanResultError == 0) && (current_frame < framesCount) && (Result == HRESULT.S_OK) && !backgroundWorker.CancellationPending)
            {
                Boolean bIsDownload = false;
                Byte[]  bufP        = null;
                stateTimer.Enabled = true;

                // Download the file with frame data from server
                while ((ScanResultError == 0) && !backgroundWorker.CancellationPending && !bIsDownload)
                {
                    //                 try
                    {
                        String scanFileName = stSerOb.ServerPath + "\\" + prefixScanFileName + "_" + current_frame.ToString();
                        if (File.Exists(@scanFileName))
                        {
                            FDataSave_ReadFile(scanFileName, ref bufP);
                            stateTimer.Stop();
                            bIsDownload = true;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    //                 catch
                    {
                        // Timeout if the file is not ready (until frame is not scanning or error is occured)
                        //                     Thread.Sleep(2000);
                    }
                }     // while

                if ((bufP != null) && (ScanResultError == 0) && !backgroundWorker.CancellationPending)
                {
                    Result = FDataConvertion_BufferConversion(0, ref bufP, ref buf_frame_int);

                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }
                    buf_frame.Initialize();
                    // Заполнение кадра
                    // Create the buffer more size
                    Result = TotalBufCreate();
                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }
                    try
                    {
                        // Добавление кадра в общий буфер
                        if (buf_total_int.Length > 0)
                        {
                            for (int j = 0; j < height_frame_bmp; j++)
                            {
                                Array.Copy(buf_frame_int, ((width_frame_bmp + stSerOb.CropZone + stSerOb.CropZone) * j) + stSerOb.CropZone, buf_total_int, width_frame_bmp * height_frame_bmp * current_frame + width_frame_bmp * j, width_frame_bmp);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error by copy array. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        Result = HRESULT.S_ERROR;
                    }
                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }

                    if (stSerOb.UseAdjustment)
                    {
                        arAvgFrame.Add(new long[2]);
                    }

                    if (bmp == null)
                    {
                        int r;
                        int itemsBmpCount = (Int16)(Math.DivRem(framesCount, coefficient, out r) + (r > 0 ? 1 : 0));
                        bmp = new Bitmap[itemsBmpCount];
                    }

                    int item = Math.DivRem(current_frame, coefficient, out rem);

                    FDataModification dataTransform = new FDataModification();
                    dataTransform.SetMode(Mode);
                    dataTransform.SetAdjustmentParameters(stSerOb.UseAdjustment, item);

                    // Преобразование кадра в 16-битовое число
                    Result = dataTransform.SetBufTotalToBufForBitmap((int)nShift.Value, width_frame_bmp * height_frame_bmp * current_frame, width_frame_bmp * height_frame_bmp, current_frame, ref buf_frame);
                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }

                    // Установка ширины и создание текущей bitmap
                    Int16 itemWidthCurrent = itemWidth;
                    if (rem == 0)
                    {
                        if (item == (bmp.Length - 1))
                        {
                            itemWidthCurrent = itemWidthLast;
                        }

                        bmp[item] = new Bitmap(itemWidthCurrent, height_frame_bmp);
                    }

                    // Добавление кадра в bitmap
                    Result = dataTransform.SetBitmap(current_frame, ref buf_frame, ref bmp[item]);

                    if (Result != HRESULT.S_OK)
                    {
                        break;
                    }

                    // Отображение bitmap с новым кадром на image
                    FDataScan_DrawImage(ref item, ref itemWidthCurrent);

                    // Отображение image на экран
                    SetRectShowSize();
                    lock (progressBar)
                    {
                        progressBar.Increment(1);
                    }

                    current_frame++;
                } // if buf != null
            }     // while

            stateTimer.Elapsed -= OnTimeoutExceptionEvent;
            if ((ScanResultError == 0) && (Result == HRESULT.S_OK))
            {
                if (!backgroundWorker.CancellationPending)
                {
                    FDataSave_AutoSaveData();
                    for (int i = 0; i < framesCount; i++)
                    {
                        String scanFileName = stSerOb.ServerPath + prefixScanFileName + "_" + i.ToString();
                        if (File.Exists(scanFileName))
                        {
                            File.Delete(scanFileName);
                        }
                    }
                }
            }

            return(Result);
        }