static void _enesim_image_file_cb(Enesim_Buffer *b, void *user_data, int error) { Enesim_Image_File_Data *fdata = user_data; fdata->cb(b, fdata->user_data, error); enesim_stream_unref(fdata->data); }
/*============================================================================* * Global * *============================================================================*/ Efl_Egueb_IO_Request * efl_egueb_io_request_new(Egueb_Dom_String *location, const Efl_Egueb_IO_Request_Descriptor *descriptor, void *data) { Efl_Egueb_IO_Request *thiz; const char *filename; if (!location) return NULL; filename = egueb_dom_string_chars_get(location); thiz = calloc(1, sizeof(Efl_Egueb_IO_Request)); thiz->descriptor = descriptor; thiz->data = data; if (!strncmp(filename, "file://", 7)) { Enesim_Stream *s; s = enesim_stream_file_new(filename + 7, "r"); if (s) { DBG("Data '%s' loaded correctly", filename); thiz->in_event = EINA_TRUE; if (thiz->descriptor->completion) thiz->descriptor->completion(thiz, s); enesim_stream_unref(s); thiz->in_event = EINA_FALSE; } } else if (!strncmp(filename, "http://", 7)) { thiz->conn = ecore_con_url_new(filename); thiz->binbuf = eina_binbuf_new(); ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _efl_egueb_io_request_url_completion_cb, thiz); ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA, _efl_egueb_io_request_url_data_cb, thiz); ecore_con_url_get(thiz->conn); } else { WRN("Unsupported schema '%s'", filename); free(thiz); thiz = NULL; goto done; } if (thiz->destroy) { efl_egueb_io_request_free(thiz); return NULL; } done: return thiz; }
/** * Load an image synchronously * * @param file The image file to load * @param b The buffer to write the image pixels to. It must not be NULL. * @param mpool The mempool that will create the surface in case the surface * reference is NULL * @param options Any option the emage provider might require * @param[out] err The error in case the file load fails * @return EINA_TRUE in case the image was loaded correctly. EINA_FALSE if not */ EAPI Eina_Bool enesim_image_file_load(const char *file, Enesim_Buffer **b, Enesim_Pool *mpool, const char *options, Eina_Error *err) { Enesim_Stream *data; Eina_Bool ret; const char *mime; if (!_file_load_data_get(file, &data, &mime)) return EINA_FALSE; ret = enesim_image_load(data, mime, b, mpool, options, err); enesim_stream_unref(data); return ret; }
/** * Gets the information about an image file * * @param file The image file to load * @param w The image width * @param h The image height * @param sfmt The image original format * @param[out] err The error in case the file info load fails */ EAPI Eina_Bool enesim_image_file_info_get(const char *file, int *w, int *h, Enesim_Buffer_Format *sfmt, const char *options, Eina_Error *err) { Enesim_Stream *data; Eina_Bool ret; const char *mime; if (!_file_load_data_get(file, &data, &mime)) return EINA_FALSE; ret = enesim_image_info_get(data, mime, w, h, sfmt, options, err); enesim_stream_unref(data); return ret; }
/** * Save an image file synchronously * * @param file The image file to save * @param b The surface to read the image pixels from. It must not be NULL. * @param options Any option the emage provider might require * @param[out] err The error in case the file save fails * @return EINA_TRUE in case the image was saved correctly. EINA_FALSE if not */ EAPI Eina_Bool enesim_image_file_save(const char *file, Enesim_Buffer *b, const char *options, Eina_Error *err) { Enesim_Stream *data; Eina_Bool ret; const char *mime; if (!_file_save_data_get(file, &data, &mime)) { if (err) *err = ENESIM_IMAGE_ERROR_PROVIDER; return EINA_FALSE; } ret = enesim_image_save(data, mime, b, options, err); enesim_stream_unref(data); return ret; }
static Eina_Bool _file_load_data_get(const char *file, Enesim_Stream **data, const char **mime) { Enesim_Stream *d; const char *m; d = enesim_stream_file_new(file, "rb"); if (!d) return EINA_FALSE; m = enesim_image_mime_data_from(d); if (!m) { enesim_stream_unref(d); return EINA_FALSE; } enesim_stream_reset(d); *mime = m; *data = d; return EINA_TRUE; }
static Eina_Bool _efl_egueb_io_request_url_completion_cb(void *data, int type EINA_UNUSED, void *event) { Efl_Egueb_IO_Request *thiz = data; Ecore_Con_Event_Url_Complete *ev = event; Enesim_Stream *s; if (ev->url_con != thiz->conn) return EINA_TRUE; /* TODO can we avoid this dup? */ s = enesim_stream_buffer_new(eina_binbuf_string_steal(thiz->binbuf), eina_binbuf_length_get(thiz->binbuf), free); thiz->in_event = EINA_TRUE; if (thiz->descriptor->completion) thiz->descriptor->completion(thiz, s); thiz->in_event = EINA_FALSE; enesim_stream_unref(s); if (thiz->destroy) efl_egueb_io_request_free(thiz); return EINA_TRUE; }
static Eina_Bool _file_save_data_get(const char *file, Enesim_Stream **data, const char **mime) { Enesim_Stream *d; const char *m; const char *ext; ext = _enesim_image_file_get_extension(file); if (!ext) return EINA_FALSE; d = enesim_stream_file_new(file, "wb"); if (!d) return EINA_FALSE; m = enesim_image_mime_extension_from(ext); if (!m) { enesim_stream_unref(d); return EINA_FALSE; } *mime = m; *data = d; return EINA_TRUE; }