int main(int argc, char *argv[]){ Camera *camera; GPContext *context = gp_context_new(); int retval; CameraEventType evttype; void *evtdata; CameraFile *file; gp_camera_new(&camera); retval = gp_camera_init(camera, context); if(retval != GP_OK) { printf("Error: %s\n", gp_result_as_string(retval)); return 1; } do { retval = gp_camera_wait_for_event (camera, 10, &evttype, &evtdata, context); } while ((retval == GP_OK) && (evttype != GP_EVENT_TIMEOUT)); retval = gp_file_new(&file); if (retval != GP_OK) { printf("gp_file_new: %d\n", retval); return 1; } /*retval = gp_camera_capture_preview(camera, file, context); */ if (retval != GP_OK) { printf("gp_camera_capture_preview: %d\n", retval); return 1; } gp_file_free (file); if(argc == 1) { retval = camera_auto_focus(camera, context); if(retval != GP_OK) { printf("Error: %s\n", gp_result_as_string(retval)); return 1; } } else if(argc == 2) { int value = atoi(argv[1]); retval = camera_manual_focus(camera, value, context); if(retval != GP_OK) { printf("Error: %s\n", gp_result_as_string(retval)); return 1; } } do { retval = gp_camera_wait_for_event (camera, 10, &evttype, &evtdata, context); } while ((retval == GP_OK) && (evttype != GP_EVENT_TIMEOUT)); gp_camera_exit(camera, context); return 0; }
void GPCamera::printGphotoErrorDescription(int errorCode) { #ifdef HAVE_GPHOTO2 qCDebug(DIGIKAM_IMPORTUI_LOG) << "Libgphoto2 error: " << gp_result_as_string(errorCode) << " (" << errorCode << ")"; #else Q_UNUSED(errorCode); #endif /* HAVE_GPHOTO2 */ }
/** * @brief QTLCamera::initCamera * @return */ QTLError QTLCamera::initCamera() { QTLError result; result.rc = GP_OK; result.errorText = "Camera initialized."; // Set aside memory for camera result.rc = gp_camera_new(¶ms->camera); if (result.rc != GP_OK) { result.errorText = gp_result_as_string(result.rc); return result; } // Initialise camera qDebug() << "Detecting Camera."; result.rc = gp_camera_init(params->camera, params->context); if (result.rc != GP_OK) { result.errorText = gp_result_as_string(result.rc); if (result.rc == -105) { result.errorText = "Failed to initialise camera. Please check the camera " \ "is turned on, then re-initialise or restart the program to enable" \ " camera paramaters."; } else if (result.rc == -60 || result.rc == -53) { result.errorText = "Failed to initialise camera. Please check the camera " \ "is unmounted and that no other applications are using it, then " \ "re-initialise or restart the program to enable camera paramaters."; } } else { qDebug() << "Camera detected"; qDebug() << "Detecting widgets" << endl; CameraWidget *rootConfig; result.rc = gp_camera_get_config(params->camera, &rootConfig, params->context); if (result.rc == GP_OK) { char prefix[] = ""; _getWidgets(params->widgetList, rootConfig, prefix); gp_widget_free(rootConfig); } qDebug() << "Widgets detected"; } return result; }
/** * @brief CameraHandler::_deleteImage */ QTLError QTLCamera::_deleteImage(CameraFilePath *cameraFilePath, CameraFile *cameraFile) { QTLError result; result.rc = gp_camera_file_delete(params->camera, cameraFilePath->folder, cameraFilePath->name, params->context); if (result.rc != GP_OK) { result.errorText = gp_result_as_string(result.rc); qDebug() << "Problem deleting file from camera." << result.errorText; } else { qDebug() << "File" << cameraFilePath->folder << cameraFilePath->name << "deleted from camera."; gp_file_unref(cameraFile); } return result; }
static int set_config (CmdConfig *cmd_config) { int result, selection; char *msg[10]; char *buttons[] = {N_("</B/24>Continue"), N_("</B16>Cancel")}; CDKDIALOG *question = NULL; result = gp_camera_set_config (cmd_config->camera, cmd_config->window, cmd_config->context); if (result < 0) { msg[0] = N_("<C></5>Error"); msg[1] = ""; msg[2] = N_("Could not set configuration:"); msg[3] = (char*) gp_result_as_string (result); question = newCDKDialog (cmd_config->screen, CENTER, CENTER, msg, 4, buttons, 2, COLOR_PAIR (2) | A_REVERSE, TRUE, TRUE, FALSE); if (!question) return (GP_ERROR); selection = activateCDKDialog (question, 0); if (question->exitType == vNORMAL) { switch (selection) { case 0: /* Continue */ destroyCDKDialog (question); return (GP_OK); default: destroyCDKDialog (question); return (result); } } else { destroyCDKDialog (question); return (result); } } return (GP_OK); }
bool GPhotoCCD::grabImage() { //char ext[16]; uint8_t *memptr = PrimaryCCD.getFrameBuffer(); size_t memsize; int fd, naxis=2, w, h, bpp=8; if (sim) { w= PrimaryCCD.getXRes(); h= PrimaryCCD.getYRes(); size_t fullbuf_size = w*h + 512; uint8_t * fullbuf = (uint8_t *) malloc(fullbuf_size); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) fullbuf[i * w + j] = rand() % 255; // Starting address if subframing memptr = fullbuf + (PrimaryCCD.getSubY() * PrimaryCCD.getXRes()) + PrimaryCCD.getSubX(); memsize = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * PrimaryCCD.getBPP()/8; PrimaryCCD.setFrameBuffer(memptr); PrimaryCCD.setFrameBufferSize(memsize, false); //PrimaryCCD.setResolution(w, h); //PrimaryCCD.setFrame(0, 0, w, h); PrimaryCCD.setNAxis(naxis); PrimaryCCD.setBPP(bpp); ExposureComplete(&PrimaryCCD); return true; } // If only save to SD Card, let's not upload back to client /*if (UploadS[GP_UPLOAD_SDCARD].s == ISS_ON) { DEBUG(INDI::Logger::DBG_SESSION, "Exposure complete. Image saved to SD Card."); ExposureComplete(&PrimaryCCD); return true; }*/ if (transferFormatS[0].s == ISS_ON) { char tmpfile[] = "/tmp/indi_XXXXXX"; //dcraw can't read from stdin, so we need to write to disk then read it back fd = mkstemp(tmpfile); int ret = gphoto_read_exposure_fd(gphotodrv, fd); if (ret != GP_OK || fd == -1) { if (fd == -1) DEBUGF(INDI::Logger::DBG_ERROR, "Exposure failed to save image. Cannot create temp file %s", tmpfile); else DEBUGF(INDI::Logger::DBG_ERROR, "Exposure failed to save image... %s", gp_result_as_string(ret)); unlink(tmpfile); return false; } if (!strcmp(gphoto_get_file_extension(gphotodrv), "unknown")) { DEBUG(INDI::Logger::DBG_ERROR, "Exposure failed."); return false; } /* We're done exposing */ DEBUG(INDI::Logger::DBG_SESSION, "Exposure done, downloading image..."); if(strcasecmp(gphoto_get_file_extension(gphotodrv), "jpg") == 0 || strcasecmp(gphoto_get_file_extension(gphotodrv), "jpeg") == 0) { if (read_jpeg(tmpfile, &memptr, &memsize, &naxis, &w, &h)) { DEBUG(INDI::Logger::DBG_ERROR, "Exposure failed to parse jpeg."); unlink(tmpfile); return false; } DEBUGF(INDI::Logger::DBG_DEBUG, "read_jpeg: memsize (%d) naxis (%d) w (%d) h (%d) bpp (%d)", memsize, naxis, w, h, bpp); } else { if (read_dcraw(tmpfile, &memptr, &memsize, &naxis, &w, &h, &bpp)) { DEBUG(INDI::Logger::DBG_ERROR, "Exposure failed to parse raw image."); unlink(tmpfile); return false; } DEBUGF(INDI::Logger::DBG_DEBUG, "read_dcraw: memsize (%d) naxis (%d) w (%d) h (%d) bpp (%d)", memsize, naxis, w, h, bpp); unlink(tmpfile); } PrimaryCCD.setImageExtension("fits"); // If subframing is requested if (frameInitialized && (PrimaryCCD.getSubH() < PrimaryCCD.getYRes() || PrimaryCCD.getSubW() < PrimaryCCD.getXRes())) { int subFrameSize = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * bpp/8 * ((naxis == 3) ? 3 : 1); int oneFrameSize = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * bpp/8; uint8_t *subframeBuf = (uint8_t *) malloc(subFrameSize); int startY= PrimaryCCD.getSubY(); int endY = startY + PrimaryCCD.getSubH(); int lineW = PrimaryCCD.getSubW() * bpp/8; int subX = PrimaryCCD.getSubX(); if (naxis == 2) { for (int i=startY ; i < endY; i++) memcpy(subframeBuf + (i - startY) * lineW, memptr + (i * w + subX) * bpp/8 , lineW); } else { uint8_t *subR = subframeBuf; uint8_t *subG = subframeBuf + oneFrameSize; uint8_t *subB = subframeBuf + oneFrameSize * 2; uint8_t *startR = memptr; uint8_t *startG = memptr + (w * h * bpp/8); uint8_t *startB = memptr + (w * h * bpp/8 * 2); for (int i=startY; i < endY; i++) { memcpy(subR + (i-startY) * lineW, startR + (i * w + subX) * bpp/8 , lineW ); memcpy(subG + (i-startY) * lineW, startG + (i * w + subX) * bpp/8 , lineW ); memcpy(subB + (i-startY) * lineW, startB + (i * w + subX) * bpp/8 , lineW ); } } PrimaryCCD.setFrameBuffer(subframeBuf); PrimaryCCD.setFrameBufferSize(subFrameSize, false); PrimaryCCD.setResolution(w, h); PrimaryCCD.setNAxis(naxis); PrimaryCCD.setBPP(bpp); ExposureComplete(&PrimaryCCD); // Restore old pointer and release memory PrimaryCCD.setFrameBuffer(memptr); PrimaryCCD.setFrameBufferSize(memsize, false); free(subframeBuf); } else { // We need to initially set the frame dimensions for the first time since it is unknown at the time of connection. frameInitialized = true; PrimaryCCD.setFrame(0, 0, w, h); PrimaryCCD.setFrameBuffer(memptr); PrimaryCCD.setFrameBufferSize(memsize, false); PrimaryCCD.setResolution(w, h); PrimaryCCD.setNAxis(naxis); PrimaryCCD.setBPP(bpp); ExposureComplete(&PrimaryCCD); } } else { int rc = gphoto_read_exposure(gphotodrv); if (rc != 0) { DEBUG(INDI::Logger::DBG_ERROR, "Failed to expose."); return rc; } /* We're done exposing */ DEBUG(INDI::Logger::DBG_DEBUG, "Exposure done, downloading image..."); uint8_t *newMemptr = NULL; gphoto_get_buffer(gphotodrv, (const char **)&newMemptr, &memsize); memptr = (uint8_t *)realloc(memptr, memsize); // We copy the obtained memory pointer to avoid freeing some gphoto memory memcpy(memptr, newMemptr, memsize); // gphoto_get_dimensions(gphotodrv, &w, &h); PrimaryCCD.setImageExtension(gphoto_get_file_extension(gphotodrv)); if (w > 0 && h > 0) PrimaryCCD.setFrame(0, 0, w, h); PrimaryCCD.setFrameBuffer(memptr); PrimaryCCD.setFrameBufferSize(memsize, false); if (w > 0 && h > 0) PrimaryCCD.setResolution(w, h); PrimaryCCD.setNAxis(naxis); PrimaryCCD.setBPP(bpp); ExposureComplete(&PrimaryCCD); } return true; }
bool GPhotoCCD::capturePreview() { if (sim) return false; int rc = GP_OK; char errMsg[MAXRBUF]; const char* previewData; unsigned long int previewSize; CameraFile* previewFile = NULL; rc = gp_file_new(&previewFile); if (rc != GP_OK) { DEBUGF(INDI::Logger::DBG_ERROR, "Error creating gphoto file: %s", gp_result_as_string(rc)); return false; } for (int i=0; i < MAX_RETRIES; i++) { rc = gphoto_capture_preview(gphotodrv, previewFile, errMsg); if (rc == true) break; } if (rc != GP_OK) { DEBUGF(INDI::Logger::DBG_ERROR, "%s", errMsg); return false; } if (rc >= GP_OK) { rc = gp_file_get_data_and_size(previewFile, &previewData, &previewSize); if (rc != GP_OK) { DEBUGF(INDI::Logger::DBG_ERROR, "Error getting preview image data and size: %s", gp_result_as_string(rc)); return false; } } //DEBUGF(INDI::Logger::DBG_DEBUG, "Preview capture size %d bytes.", previewSize); char *previewBlob = (char *) previewData; imageB->blob = previewBlob; imageB->bloblen = previewSize; imageB->size = previewSize; strncpy(imageB->format, "stream_jpeg", MAXINDIBLOBFMT); IDSetBLOB (imageBP, NULL); if (previewFile) { gp_file_unref(previewFile); previewFile = NULL; } return true; }
static void _camera_process_job(const dt_camctl_t *c,const dt_camera_t *camera, gpointer job) { dt_camera_t *cam=(dt_camera_t *)camera; _camctl_camera_job_t *j = (_camctl_camera_job_t *)job; switch( j->type ) { case _JOB_TYPE_EXECUTE_CAPTURE: { dt_print (DT_DEBUG_CAMCTL,"[camera_control] executing remote camera capture job\n"); CameraFilePath fp; int res=GP_OK; if( (res = gp_camera_capture (camera->gpcam, GP_CAPTURE_IMAGE,&fp, c->gpcontext)) == GP_OK ) { CameraFile *destination; const char *output_path = _dispatch_request_image_path(c,camera); if( !output_path ) output_path="/tmp"; const char *fname = _dispatch_request_image_filename(c,fp.name,cam); if( !fname ) fname=fp.name; char *output = g_build_filename (output_path,fname,(char *)NULL); int handle = open (output, O_CREAT | O_WRONLY,0666); gp_file_new_from_fd (&destination , handle); gp_camera_file_get (camera->gpcam, fp.folder , fp.name, GP_FILE_TYPE_NORMAL, destination, c->gpcontext); close (handle); // Notify listerners of captured image _dispatch_camera_image_downloaded (c,camera,output); g_free (output); } else dt_print (DT_DEBUG_CAMCTL,"[camera_control] capture job failed to capture image: %s\n",gp_result_as_string(res)); } break; case _JOB_TYPE_EXECUTE_LIVE_VIEW: { CameraFile *fp = NULL; int res = GP_OK; const gchar* data = NULL; unsigned long int data_size = 0; gp_file_new(&fp); if( (res = gp_camera_capture_preview (cam->gpcam, fp, c->gpcontext)) != GP_OK ) { dt_print (DT_DEBUG_CAMCTL,"[camera_control] live view failed to capture preview: %s\n", gp_result_as_string(res)); } else if( (res = gp_file_get_data_and_size(fp, &data, &data_size)) != GP_OK ) { dt_print (DT_DEBUG_CAMCTL,"[camera_control] live view failed to get preview data: %s\n", gp_result_as_string(res)); } else { // everything worked GdkPixbufLoader *loader = gdk_pixbuf_loader_new(); if(gdk_pixbuf_loader_write(loader, (guchar*)data, data_size, NULL) == TRUE) { dt_pthread_mutex_lock(&cam->live_view_pixbuf_mutex); if(cam->live_view_pixbuf != NULL) g_object_unref(cam->live_view_pixbuf); cam->live_view_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); dt_pthread_mutex_unlock(&cam->live_view_pixbuf_mutex); } gdk_pixbuf_loader_close(loader, NULL); } if(fp) gp_file_free(fp); dt_pthread_mutex_unlock(&cam->live_view_synch); dt_control_queue_redraw_center(); } break; case _JOB_TYPE_SET_PROPERTY: { _camctl_camera_set_property_job_t *spj=(_camctl_camera_set_property_job_t *)job; dt_print(DT_DEBUG_CAMCTL,"[camera_control] executing set camera config job %s=%s\n",spj->name,spj->value); CameraWidget *config; // Copy of camera configuration CameraWidget *widget; gp_camera_get_config( cam->gpcam, &config, c->gpcontext ); if( gp_widget_get_child_by_name ( config, spj->name, &widget) == GP_OK) { gp_widget_set_value ( widget , spj->value); gp_camera_set_config( cam->gpcam, config, c->gpcontext ); } /* dt_pthread_mutex_lock( &cam->config_lock ); CameraWidget *widget; if( gp_widget_get_child_by_name ( camera->configuration, spj->name, &widget) == GP_OK) { gp_widget_set_value ( widget , spj->value); //gp_widget_set_changed( widget, 1 ); cam->config_changed=TRUE; } dt_pthread_mutex_unlock( &cam->config_lock);*/ } break; default: dt_print(DT_DEBUG_CAMCTL,"[camera_control] process of unknown job type %lx\n",(unsigned long int)j->type); break; } g_free(j); }
int input_run(int id) { int res, i; global->in[id].buf = malloc(256 * 1024); if(global->in[id].buf == NULL) { IPRINT(INPUT_PLUGIN_NAME " - could not allocate memory\n"); exit(EXIT_FAILURE); } plugin_id = id; // auto-detect algorithm CameraAbilitiesList* al; GPPortInfoList* il; CameraList* list; const char* model; const char* port; context = gp_context_new(); gp_abilities_list_new(&al); gp_abilities_list_load(al, context); gp_port_info_list_new(&il); gp_port_info_list_load(il); gp_list_new(&list); gp_abilities_list_detect(al, il, list, context); int count = gp_list_count(list); IPRINT(INPUT_PLUGIN_NAME " - Detected %d camera(s)\n", count); if(count == 0) { IPRINT(INPUT_PLUGIN_NAME " - No cameras detected.\n"); return 0; } GPPortInfo info; CameraAbilities a; int m, p; camera = NULL; for(i = 0; i < count; i++) { res = gp_list_get_name(list, i, &model); CAMERA_CHECK_GP(res, "gp_list_get_name"); m = gp_abilities_list_lookup_model(al, model); if(m < 0) { IPRINT(INPUT_PLUGIN_NAME " - Gphoto abilities_list_lookup_model Code: %d - %s\n", m, gp_result_as_string(m)); return 0; } res = gp_abilities_list_get_abilities(al, m, &a); CAMERA_CHECK_GP(res, "gp_abilities_list_get_abilities"); res = gp_list_get_value(list, i, &port); CAMERA_CHECK_GP(res, "gp_list_get_value"); DBG("Model: %s; port: %s.\n", model, port); if(selected_port != NULL && strcmp(selected_port, port) != 0) continue; p = gp_port_info_list_lookup_path(il, port); if(p < 0) { IPRINT(INPUT_PLUGIN_NAME " - Gphoto port_info_list_lookup_path Code: %d - %s\n", m, gp_result_as_string(m)); return 0; } res = gp_port_info_list_get_info(il, p, &info); CAMERA_CHECK_GP(res, "gp_port_info_list_get_info"); res = gp_camera_new(&camera); CAMERA_CHECK_GP(res, "gp_camera_new"); res = gp_camera_set_abilities(camera, a); CAMERA_CHECK_GP(res, "gp_camera_set_abilities"); res = gp_camera_set_port_info(camera, info); CAMERA_CHECK_GP(res, "gp_camera_set_port_info"); } if(camera == NULL) { IPRINT("Camera %s not found, exiting.\n", selected_port); exit(EXIT_FAILURE); } // cleanup gp_list_unref(list); gp_port_info_list_free(il); gp_abilities_list_free(al); // open camera and set capture on int value = 1; res = gp_camera_init(camera, context); CAMERA_CHECK_GP(res, "gp_camera_init"); camera_set("capture", &value); // starting thread if(pthread_create(&thread, 0, capture, NULL) != 0) { free(global->in[id].buf); IPRINT("could not start worker thread\n"); exit(EXIT_FAILURE); } pthread_detach(thread); return 0; }
static int camera_cam_desc_get_widget (Camera *camera, CameraRegisterType *reg_p, CameraWidget *section, GPContext *context) { int ind, vind, ret, value; int mask; char buff[1024]; CameraWidget *child; RegisterDescriptorType *reg_desc_p; GP_DEBUG ("register %d", reg_p->reg_number); if (reg_p->reg_len == 0) { /* * This is 0 for GP_WIDGET_BUTTON (callbacks), since is no * register for call backs. Frontends "get" the value, and * call the function directly. */ ret = GP_OK; } else if (reg_p->reg_len == 4) { int rval; ret = sierra_get_int_register (camera, reg_p->reg_number, &rval, context); reg_p->reg_value = rval; } else if (reg_p->reg_len == 8) { /* * reg_value is 8 bytes maximum. If you need a bigger * value, change the reg_value size, or allocate space on * the fly and make a union with reg_value and a void*. */ ret = sierra_get_string_register (camera, reg_p->reg_number, -1, NULL, (unsigned char *)buff, (unsigned int *)&value, context); if ((ret == GP_OK) && value != reg_p->reg_len) { GP_DEBUG ("Bad length result %d", value); return (GP_ERROR); } memcpy (®_p->reg_value, buff, reg_p->reg_len); } else { GP_DEBUG ("Bad register length %d", reg_p->reg_number); return (GP_ERROR); } GP_DEBUG ("... '%s'.", gp_result_as_string (ret)); if (ret < 0) { return (ret); } for (ind = 0; ind < reg_p->reg_desc_cnt; ind++) { reg_desc_p = ®_p->reg_desc[ind]; mask = reg_desc_p->regs_mask; GP_DEBUG ("window name is %s", reg_desc_p->regs_long_name); gp_widget_new (reg_desc_p->reg_widget_type, _(reg_desc_p->regs_long_name), &child); gp_widget_set_name (child, reg_desc_p->regs_short_name); /* * Setting the info for the preference settings does not * make sense like it does for an icon button. This is * used as the tool-tip field (mouse over hint that pops * up after a second in gtkam). We don't want this used * at all; setting it to space doesn't work well, so just * set it to the same as regs_long_name. */ gp_widget_set_info (child, _(reg_desc_p->regs_long_name)); GP_DEBUG ("reg_value 0x%016llx", (long long unsigned)reg_p->reg_value); for (vind = 0; vind < reg_desc_p->reg_val_name_cnt; vind++) { camera_cam_desc_get_value (®_desc_p->regs_value_names[vind], reg_desc_p->reg_widget_type, reg_p->reg_len, (char*) ®_p->reg_value, mask, child); } /* * For radio and menu values: if there has been no change, it * means the value was not set, and so it is unknown. */ if (((reg_desc_p->reg_widget_type == GP_WIDGET_RADIO) || (reg_desc_p->reg_widget_type == GP_WIDGET_MENU)) && !gp_widget_changed (child)) { sprintf (buff, _("%lld (unknown)"), (long long)reg_p->reg_value); gp_widget_add_choice (child, buff); gp_widget_set_value (child, buff); } gp_widget_append (section, child); } return (GP_OK); }
// Use this only for gphoto2 errors, not internal wrapper errors Exception::Exception(const std::string& msg, int gpnum) : std::runtime_error("gphoto2 error " + msg + ":" + std::to_string(gpnum) + " (" + gp_result_as_string(gpnum) + ")") {}