Exemple #1
0
void* FastCMOS::getLastFrame() {

	/*
	imgSessionAcquire(session, 0, 0);
	void *r = buffers[curBuf];
	curBuf = (curBuf+1) % buffers.size();*/

	uInt32 bufnum;
	void* bufaddr;

	checkIMAQ(imgSessionExamineBuffer2(session, IMG_CURRENT_BUFFER, &bufnum, &bufaddr));

	dbgprintf("bufnum: %d\n",bufnum);

	return bufaddr;
}
static GstFlowReturn
gst_niimaqsrc_create (GstPushSrc * psrc, GstBuffer ** buffer)
{
  GstNiImaqSrc *src = GST_NIIMAQSRC (psrc);
  GstFlowReturn ret = GST_FLOW_OK;
  GstClockTime timestamp;
  GstClockTime duration;
  uInt32 copied_number;
  uInt32 copied_index;
  Int32 rval;
  uInt32 dropped;
  gboolean no_copy;
  GstMapInfo minfo;

  /* we can only do a no-copy if strides are property byte aligned */
  no_copy = src->avoid_copy && src->width == src->rowpixels;

  /* start the IMAQ acquisition session if we haven't done so yet */
  if (!src->session_started) {
    if (!gst_niimaqsrc_start_acquisition (src)) {
      GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
          ("Unable to start acquisition."), (NULL));
      return GST_FLOW_ERROR;
    }
  }

  if (no_copy) {
    GST_LOG_OBJECT (src,
        "Sending IMAQ buffer #%d along without copying", src->cumbufnum);
    *buffer = gst_buffer_new ();
    if (G_UNLIKELY (*buffer == NULL))
      goto error;
  } else {
    GST_LOG_OBJECT (src, "Copying IMAQ buffer #%d, size %d",
        src->cumbufnum, src->framesize);
    ret =
        GST_BASE_SRC_CLASS (gst_niimaqsrc_parent_class)->alloc (GST_BASE_SRC
        (src), 0, src->framesize, buffer);
    if (ret != GST_FLOW_OK) {
      GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
          ("Failed to allocate buffer"),
          ("Failed to get downstream pad to allocate buffer"));
      goto error;
    }
  }

  //{
  // guint32 *data;
  // int i;
  //    rval = imgSessionExamineBuffer2 (src->sid, src->cumbufnum, &copied_number, &data);
  // for (i=0; i<src->bufsize;i++)
  //  if (data == src->buflist[i])
  //        break;
  // timestamp = src->times[i];
  // memcpy (GST_BUFFER_DATA (*buffer), data, src->framesize);
  // src->times[i] = GST_CLOCK_TIME_NONE;
  // imgSessionReleaseBuffer (src->sid); //TODO: mutex here?
  //}
  if (no_copy) {
    /* FIXME: with callback change, is this broken now? mutex... */
    gpointer data;
    rval =
        imgSessionExamineBuffer2 (src->sid, src->cumbufnum,
        &copied_number, &data);
    gst_buffer_append_memory (*buffer,
        gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, data,
            src->framesize, 0, src->framesize, src,
            gst_niimaqsrc_release_buffer));
  } else if (src->width == src->rowpixels) {
    /* TODO: optionally use ExamineBuffer and byteswap in transfer (to offer BIG_ENDIAN) */
    gst_buffer_map (*buffer, &minfo, GST_MAP_WRITE);
    g_mutex_lock (&src->mutex);
    rval =
        imgSessionCopyBufferByNumber (src->sid, src->cumbufnum,
        minfo.data, IMG_OVERWRITE_GET_OLDEST, &copied_number, &copied_index);
    timestamp = src->times[copied_index];
    src->times[copied_index] = GST_CLOCK_TIME_NONE;
    g_mutex_unlock (&src->mutex);
    gst_buffer_unmap (*buffer, &minfo);
  } else {
    gst_buffer_map (*buffer, &minfo, GST_MAP_WRITE);
    g_mutex_lock (&src->mutex);
    rval =
        imgSessionCopyAreaByNumber (src->sid, src->cumbufnum, 0, 0,
        src->height, src->width, minfo.data, src->rowpixels,
        IMG_OVERWRITE_GET_OLDEST, &copied_number, &copied_index);
    timestamp = src->times[copied_index];
    src->times[copied_index] = GST_CLOCK_TIME_NONE;
    g_mutex_unlock (&src->mutex);
    gst_buffer_unmap (*buffer, &minfo);
  }

  /* TODO: do this above to reduce copying overhead */
  if (src->is_signed) {
    gint16 *src;
    guint16 *dst;
    guint i;
    gst_buffer_map (*buffer, &minfo, GST_MAP_READWRITE);
    src = minfo.data;
    dst = minfo.data;

    GST_DEBUG_OBJECT (src, "Shifting signed to unsigned");

    /* TODO: make this faster */
    for (i = 0; i < minfo.size / 2; i++)
      *dst++ = *src++ + 32768;

    gst_buffer_unmap (*buffer, &minfo);
  }

  if (rval) {
    gst_niimaqsrc_report_imaq_error (rval);
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
        ("failed to copy buffer %d", src->cumbufnum),
        ("failed to copy buffer %d", src->cumbufnum));
    goto error;
  }

  /* make guess of duration from timestamp and cumulative buffer number */
  if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
    duration = timestamp / (copied_number + 1);
  } else {
    duration = 33 * GST_MSECOND;
  }

  GST_BUFFER_OFFSET (*buffer) = copied_number;
  GST_BUFFER_OFFSET_END (*buffer) = copied_number + 1;
  GST_BUFFER_TIMESTAMP (*buffer) =
      timestamp - gst_element_get_base_time (GST_ELEMENT (src));
  GST_BUFFER_DURATION (*buffer) = duration;

  dropped = copied_number - src->cumbufnum;
  if (dropped > 0) {
    src->n_dropped_frames += dropped;
    GST_WARNING_OBJECT (src,
        "Asked to copy buffer #%d but was given #%d; just dropped %d frames (%d total)",
        src->cumbufnum, copied_number, dropped, src->n_dropped_frames);
  }

  /* set cumulative buffer number to get next frame */
  src->cumbufnum = copied_number + 1;
  src->n_frames++;

  if (G_UNLIKELY (src->start_time && !src->start_time_sent)) {
    GstTagList *tl =
        gst_tag_list_new (GST_TAG_DATE_TIME, src->start_time, NULL);
    GstEvent *e = gst_event_new_tag (tl);
    GST_DEBUG_OBJECT (src, "Sending start time event: %" GST_PTR_FORMAT, e);
    gst_pad_push_event (GST_BASE_SRC_PAD (src), e);
    src->start_time_sent = TRUE;
  }
  return ret;

error:
  {
    return ret;
  }
}
void AcquireThreadBasler::BaslerAcquire (void)
{
	sprintf_s(intfName,  "img0");//img0 Basler///img3 Atmel
	imgInterfaceOpen (intfName, &Iid);
	imgSessionOpen (Iid, &Sid);
	AcqWinWidth =  globalOptions->IMAGEWIDTH;
	AcqWinHeight = globalOptions->IMAGEHEIGHT;
	imgSetAttribute2 (Sid, IMG_ATTR_ROI_WIDTH, AcqWinWidth);
	imgSetAttribute2 (Sid, IMG_ATTR_ROI_HEIGHT, AcqWinHeight);
    imgSessionTriggerConfigure2(Sid, IMG_SIGNAL_EXTERNAL, 0, 
                                        IMG_TRIG_POLAR_ACTIVEH, 5000, 
                                        IMG_TRIG_ACTION_BUFFER);

	for(int i=0; i<NUM_RING_BUFFERS; i++)
		ImaqBuffers[i] = NULL; 
	// Setup and launch the ring acquisition
	imgRingSetup (Sid, NUM_RING_BUFFERS, (void**)ImaqBuffers, 0, TRUE);
	uInt32  currBufNum;
	
    //Infinite Loop
	//Temporarily we have incorporated an infinite loop. Stop the program execution by closing the program. 
	//Threads can also be suspended and resumed via the start/stop buttons
	while (true)
	{
		DataFramePos = RawData; //Reset the DataFramePos pointer to the beginning, pointed to by RawData

		if (globalOptions->volumeReady < 1)
		{
			for (int framenum=0; framenum < globalOptions->NumFramesPerVol; framenum++)
			{
				if (breakLoop)
					break;

				imgSessionExamineBuffer2 (Sid, IMG_CURRENT_BUFFER , &currBufNum, (void**)&ImaqBuffer);

				if (!ImaqBuffer) { //Check if Basler Camera is on and properly connected
					printf("ERROR: Basler Camera is either off, or miscommunication with framegrabber.\n Exiting Program...\n");
					Sleep(2000);
					exit(1);
				}

				memcpy(DataFramePos, ImaqBuffer, AcqWinHeight*AcqWinWidth*sizeof(unsigned short));
				DataFramePos = &DataFramePos[AcqWinHeight*AcqWinWidth];
				imgSessionReleaseBuffer (Sid);
				
				if(!globalOptions->bVolumeScan)
					DataFramePos = RawData;
			}

			if (breakLoop) {
				breakLoop = false; //Reset the breakLoop flag after each time
			} else {
				globalOptions->volumeReady++;
			}
		}	
		else
		{
			if(globalOptions->saveVolumeflag == true)
			{		    
				fwrite(DataFramePos,2,AcqWinWidth*AcqWinHeight*globalOptions->NumFramesPerVol,globalOptions->fid);
				globalOptions->saveVolumeflag =false;
				fclose(globalOptions->fid);
				
				ScanThrPtr->InitializeSyncAndScan();	
			}
			globalOptions->volumeReady = 0;
		}
	}
}