// FIXME: we can't rely on darktable to avoid file overwriting -- it doesn't know the filename (extension). int write_image(dt_imageio_module_data_t *data, const char *filename, const void *in, dt_colorspaces_color_profile_type_t over_type, const char *over_filename, void *exif, int exif_len, int imgid, int num, int total, struct dt_dev_pixelpipe_t *pipe) { int status = 1; gboolean from_cache = TRUE; char sourcefile[PATH_MAX]; char *targetfile = NULL; char *xmpfile = NULL; char *content = NULL; FILE *fin = NULL; FILE *fout = NULL; dt_image_full_path(imgid, sourcefile, sizeof(sourcefile), &from_cache); char *extension = g_strrstr(sourcefile, "."); if(extension == NULL) goto END; targetfile = g_strconcat(filename, ++extension, NULL); if(!strcmp(sourcefile, targetfile)) goto END; fin = g_fopen(sourcefile, "rb"); fout = g_fopen(targetfile, "wb"); if(fin == NULL || fout == NULL) goto END; fseek(fin, 0, SEEK_END); size_t end = ftell(fin); rewind(fin); content = (char *)g_malloc_n(end, sizeof(char)); if(content == NULL) goto END; if(fread(content, sizeof(char), end, fin) != end) goto END; if(fwrite(content, sizeof(char), end, fout) != end) goto END; // we got a copy of the file, now write the xmp data xmpfile = g_strconcat(targetfile, ".xmp", NULL); if(dt_exif_xmp_write(imgid, xmpfile) != 0) { // something went wrong, unlink the copied image. g_unlink(targetfile); goto END; } status = 0; END: g_free(targetfile); g_free(xmpfile); g_free(content); if(fin) fclose(fin); if(fout) fclose(fout); return status; }
void dt_image_write_sidecar_file(int imgid) { // TODO: compute hash and don't write if not needed! // write .xmp file if(imgid > 0 && dt_conf_get_bool("write_sidecar_files")) { char filename[DT_MAX_PATH_LEN+8]; dt_image_full_path(imgid, filename, DT_MAX_PATH_LEN); dt_image_path_append_version(imgid, filename, DT_MAX_PATH_LEN); char *c = filename + strlen(filename); sprintf(c, ".xmp"); dt_exif_xmp_write(imgid, filename); } }
int32_t dt_control_write_sidecar_files_job_run(dt_job_t *job) { long int imgid = -1; dt_control_image_enumerator_t *t1 = (dt_control_image_enumerator_t *)job->param; GList *t = t1->index; while(t) { imgid = (long int)t->data; const dt_image_t *img = dt_image_cache_read_get(darktable.image_cache, (int32_t)imgid); char dtfilename[DT_MAX_PATH_LEN+4]; dt_image_full_path(img->id, dtfilename, DT_MAX_PATH_LEN); char *c = dtfilename + strlen(dtfilename); sprintf(c, ".xmp"); dt_exif_xmp_write(imgid, dtfilename); dt_image_cache_read_release(darktable.image_cache, img); t = g_list_delete_link(t, t); } return 0; }
// FIXME: we can't rely on darktable to avoid file overwriting -- it doesn't know the filename (extension). int write_image (dt_imageio_module_data_t *ppm, const char *filename, const void *in, void *exif, int exif_len, int imgid) { int status = 1; char *sourcefile = NULL; char *targetfile = NULL; char *xmpfile = NULL; char *content = NULL; FILE *fin = NULL; FILE *fout = NULL; sqlite3_stmt *stmt; DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select folder, filename from images, film_rolls where images.id = ?1 and film_id = film_rolls.id;", -1, &stmt, NULL); DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid); if(sqlite3_step(stmt) != SQLITE_ROW) goto END; char *sfolder = (char*)sqlite3_column_text(stmt, 0); char *sfilename = (char*)sqlite3_column_text(stmt, 1); sourcefile = g_build_filename(sfolder, sfilename, NULL); char *extension = g_strrstr(sourcefile, "."); if(extension == NULL) goto END; targetfile = g_strconcat(filename, ++extension, NULL); if(!strcmp(sourcefile, targetfile)) goto END; fin = fopen(sourcefile, "rb"); fout = fopen(targetfile, "wb"); if(fin == NULL || fout == NULL) goto END; fseek(fin,0,SEEK_END); size_t end = ftell(fin); rewind(fin); content = (char *)g_malloc_n(end, sizeof(char)); if(content == NULL) goto END; if(fread(content,sizeof(char),end,fin) != end) goto END; if(fwrite(content,sizeof(char),end,fout) != end) goto END; // we got a copy of the file, now write the xmp data xmpfile = g_strconcat(targetfile, ".xmp", NULL); if(dt_exif_xmp_write (imgid, xmpfile) != 0) { // something went wrong, unlink the copied image. g_unlink(targetfile); goto END; } status = 0; END: g_free(sourcefile); g_free(targetfile); g_free(xmpfile); g_free(content); if(fin) fclose(fin); if(fout) fclose(fout); return status; }