! Writes out a single image to destination */
| 5119 | |
| 5120 | /*! Writes out a single image to destination */ |
| 5121 | GMT_LOCAL int gmtapi_export_image (struct GMTAPI_CTRL *API, int object_ID, unsigned int mode, struct GMT_IMAGE *I_obj) { |
| 5122 | int item, error; |
| 5123 | bool done = true; |
| 5124 | struct GMTAPI_DATA_OBJECT *S_obj = NULL; |
| 5125 | struct GMT_IMAGE *I_copy = NULL; |
| 5126 | struct GMT_IMAGE_HIDDEN *IH = gmt_get_I_hidden (I_obj); |
| 5127 | |
| 5128 | GMT_Report (API, GMT_MSG_DEBUG, "gmtapi_export_image: Passed ID = %d and mode = %d\n", object_ID, mode); |
| 5129 | |
| 5130 | if (object_ID == GMT_NOTSET) return (gmtlib_report_error (API, GMT_OUTPUT_NOT_SET)); |
| 5131 | if (I_obj->data == NULL && !(mode & GMT_CONTAINER_ONLY)) return (gmtlib_report_error (API, GMT_PTR_IS_NULL)); |
| 5132 | if ((item = gmtlib_validate_id (API, GMT_IS_IMAGE, object_ID, GMT_OUT, GMT_NOTSET)) == GMT_NOTSET) return (gmtlib_report_error (API, API->error)); |
| 5133 | |
| 5134 | S_obj = API->object[item]; /* The current object whose data we will export */ |
| 5135 | if (S_obj->status != GMT_IS_UNUSED && !(mode & GMT_IO_RESET)) |
| 5136 | return (gmtlib_report_error (API, GMT_WRITTEN_ONCE)); /* Only allow writing of a data set once, unless overridden by mode */ |
| 5137 | if (mode & GMT_IO_RESET) mode -= GMT_IO_RESET; |
| 5138 | switch (S_obj->method) { |
| 5139 | case GMT_IS_FILE: /* Name of an image file on disk */ |
| 5140 | GMT_Report (API, GMT_MSG_INFORMATION, "Writing image to file %s\n", S_obj->filename); |
| 5141 | if ((error = gmtapi_export_ppm (API->GMT, S_obj->filename, I_obj)) == GMT_NOERROR) |
| 5142 | break; /* OK, wrote a PPM and we are done */ |
| 5143 | else if (error == GMT_ERROR_ON_FOPEN) { /* Failed to open file */ |
| 5144 | GMT_Report (API, GMT_MSG_ERROR, "Unable to export image\n"); |
| 5145 | return (gmtlib_report_error (API, GMT_ERROR_ON_FOPEN)); |
| 5146 | } |
| 5147 | else if (gmt_M_err_pass (API->GMT, gmt_export_image (API->GMT, S_obj->filename, I_obj), S_obj->filename)) |
| 5148 | return (gmtlib_report_error (API, GMT_IMAGE_WRITE_ERROR)); |
| 5149 | break; |
| 5150 | |
| 5151 | case GMT_IS_DUPLICATE: /* Duplicate GMT image to a new GMT_IMAGE container object */ |
| 5152 | if (S_obj->resource) return (gmtlib_report_error (API, GMT_PTR_NOT_NULL)); /* The output resource pointer must be NULL */ |
| 5153 | if (mode & GMT_CONTAINER_ONLY) return (gmtlib_report_error (API, GMT_NOT_A_VALID_MODE)); |
| 5154 | GMT_Report (API, GMT_MSG_INFORMATION, "Duplicating image data to GMT_IMAGE memory location\n"); |
| 5155 | if ((I_copy = GMT_Duplicate_Data (API, GMT_IS_IMAGE, mode, I_obj)) == NULL) |
| 5156 | return (gmtlib_report_error (API, GMT_MEMORY_ERROR)); |
| 5157 | S_obj->resource = I_copy; /* Set resource pointer to the image */ |
| 5158 | break; /* Done with this image */ |
| 5159 | |
| 5160 | case GMT_IS_REFERENCE: /* GMT image and header in a GMT_IMAGE container object - just pass the reference */ |
| 5161 | if (S_obj->region) return (gmtlib_report_error (API, GMT_SUBSET_NOT_ALLOWED)); |
| 5162 | if (mode & GMT_CONTAINER_ONLY) return (gmtlib_report_error (API, GMT_NOT_A_VALID_MODE)); |
| 5163 | GMT_Report (API, GMT_MSG_INFORMATION, "Referencing image data to GMT_IMAGE memory location\n"); |
| 5164 | S_obj->resource = I_obj; /* Set resource pointer to the image */ |
| 5165 | IH->alloc_level = S_obj->alloc_level; /* Since we are passing it up to the caller */ |
| 5166 | break; |
| 5167 | |
| 5168 | default: |
| 5169 | GMT_Report (API, GMT_MSG_ERROR, "Wrong method used to export image\n"); |
| 5170 | return (gmtlib_report_error (API, GMT_NOT_A_VALID_METHOD)); |
| 5171 | break; |
| 5172 | } |
| 5173 | |
| 5174 | if (done) S_obj->status = GMT_IS_USED; /* Mark as written (unless we only updated header) */ |
| 5175 | |
| 5176 | return (GMT_NOERROR); |
| 5177 | } |
| 5178 |
no test coverage detected