bool Image::IsLoading() { if (!image_) return false; if (!sp_image_is_loaded(image_)) return true; return false; }
void Spotify::Session::loadCoverart( const byte* coverartIdentifier, Spotify::ImageContainer* imageContainer ) { if( coverartIdentifier && imageContainer ) { sp_image* image = sp_image_create( m_session, coverartIdentifier ); if( image ) { // Already loaded: if( sp_image_is_loaded( image ) ) { Spotify::ImageData* imageData = new Spotify::ImageData( image ); imageContainer->imageReadySlot( imageData, imageContainer ); } // Load: else { sp_image_add_load_callback( image, &Spotify::Session::imageLoadedCallback, imageContainer ); connect( this, SIGNAL(coverArtReady(Spotify::ImageData*,Spotify::ImageContainer*)), imageContainer, SLOT(imageReadySlot(Spotify::ImageData*,Spotify::ImageContainer*)) ); } } else {
gboolean track_get_image_file(sp_track* track, gchar** filename) { if (!filename) return FALSE; sp_image* img = track_get_image(track); if (!img) { /* No cover */ *filename = NULL; return FALSE; } /* Build filename */ const guchar* img_id = sp_image_image_id(img); gchar* b64_id = g_base64_encode(img_id, 20); gchar* img_name = g_strdup_printf("%s.jpg", b64_id); /* Avoid / in base64-encoded file name! */ g_strdelimit(img_name, "/", '_'); *filename = g_build_filename(g_get_user_cache_dir(), g_get_prgname(), img_name, NULL); g_free(b64_id); g_free(img_name); /* If the file already exists, we're done. */ if (g_file_test(*filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) { sp_image_release(img); } /* If image is already loaded, write it to the file now */ else if (sp_image_is_loaded(img)) { gchar* fn_copy = g_strdup(*filename); _track_write_image_to_file(img, fn_copy); } /* Load the image and write it with a callback */ else { gchar* fn_copy = g_strdup(*filename); sp_image_add_load_callback(img, _track_write_image_to_file, fn_copy); } return TRUE; }
SP_LIBEXPORT(sp_image *) sp_image_create(sp_session *session, const byte image_id[20]) { sp_image *image; void **container; struct image_ctx *image_ctx; image = osfy_image_create(session, image_id); sp_image_add_ref(image); if(sp_image_is_loaded(image)) return image; /* Prevent the image from being loaded twice */ if(image->error == SP_ERROR_IS_LOADING) return image; image->error = SP_ERROR_IS_LOADING; image_ctx = malloc(sizeof(struct image_ctx)); image_ctx->session = session; image_ctx->req = NULL; image_ctx->image = image; container = (void **)malloc(sizeof(void *)); *container = image_ctx; { char buf[41]; hex_bytes_to_ascii(image->id, buf, 20); DSFYDEBUG("Requesting download of image '%s'\n", buf); } request_post(session, REQ_TYPE_IMAGE, container); return image; }
gboolean track_get_image_data(sp_track* track, gpointer* data, gsize* len) { sp_image* img; const guchar* img_data = NULL; img = track_get_image(track); if (!img) { /* No cover */ *data = NULL; *len = 0; return TRUE; } if (!sp_image_is_loaded(img)) return FALSE; img_data = sp_image_data(img, len); if (!img_data) g_error("Can't read image data"); *data = g_memdup(img_data, *len); sp_image_release(img); return TRUE; }
PHP_METHOD(SpotifyArtist, getPortrait) { int timeout = 0; zval *index, *object = getThis(); spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(object TSRMLS_CC); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) == FAILURE) { return; } zval tempretval, *thisptr = getThis(); SPOTIFY_METHOD(SpotifyArtist, browse, &tempretval, thisptr); int numportraits = sp_artistbrowse_num_portraits(p->artistbrowse); if(Z_LVAL_P(index) > numportraits) { RETURN_FALSE; } const byte* image_id = sp_artistbrowse_portrait(p->artistbrowse, Z_LVAL_P(index)); sp_image *image = sp_image_create(p->session, image_id); while(!sp_image_is_loaded(image)) { sp_session_process_events(p->session, &timeout); } size_t size; const byte* image_data = sp_image_data(image, &size); RETURN_STRINGL(image_data, size, 1); sp_image_release(image); }
JNIEXPORT int JNICALL Java_jahspotify_impl_JahSpotifyImpl_readImage (JNIEnv *env, jobject obj, jstring uri, jobject outputStream) { uint8_t *nativeURI = ( uint8_t * ) ( *env )->GetStringUTFChars ( env, uri, NULL ); sp_link *imageLink = sp_link_create_from_string(nativeURI); size_t size; jclass jClass; int numBytesWritten = -1; if (imageLink) { sp_link_add_ref(imageLink); sp_image *image = sp_image_create_from_link(g_sess,imageLink); if (image) { sp_image_add_ref(image); sp_image_add_load_callback(image, imageLoadedCallback, NULL); int count = 0; while (!sp_image_is_loaded(image) && count < 5) { sleep(1); count++; } if (count == 5) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_readImage: Timeout waiting for image to load ...\n"); sp_image_release(image); sp_link_release(imageLink); return -1; } byte *data = (byte*)sp_image_data(image,&size); jClass = (*env)->FindClass(env,"Ljava/io/OutputStream;"); if (jClass == NULL) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_readImage: could not load class java.io.OutputStream\n"); return -1; } // Lookup the method now - saves us looking it up for each iteration of the loop jmethodID jMethod = (*env)->GetMethodID(env,jClass,"write","(I)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_readImage: could not load method write(int) on class java.io.OutputStream\n"); return -1; } int i = 0; for (i = 0; i < size; i++) { (*env)->CallVoidMethod(env,outputStream,jMethod,*data); data++; numBytesWritten++; } sp_image_release(image); } sp_link_release(imageLink); } // Plus one because we start at -1 return numBytesWritten+1; }
static PyObject *Image_is_loaded(Image *self) { return Py_BuildValue("i", sp_image_is_loaded(self->_image)); }