Search function to display picture got with libcurl

Hi there,

I’m writing a piece of software to display a picture located on a server in an SDL window. I’m getting the picture with libcurl using this code :

handle=curl_easy_init();
curl_easy_setopt(handle,CURLOPT_URL,“http://192.168.0.20/image.jpg”);
curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,update_img);
curl_easy_perform(handle);
curl_easy_cleanup(handle);

For performance reasons, I cannot make a disk access to store the picture and recall it with with IMG_Load. So I wrote this fonction to display directly the incoming image. curl_easy_setopt require a fonction with the same prototype thant fwrite. I wrote this :

void update_img(void *ptr, size_t size, size_t nmemb, void *stream)
{
img_position.x = 0;
img_position.y = 0;

webcam_img = IMG_Load(ptr);
SDL_BlitSurface(webcam_img, NULL, screen, &img_position);
    SDL_Flip(screen);
pause();

}

It doesn’t work because the IMG_Load argument needs to be the path to a file (stored in an const *char). So I’m searching for an SDL function which can display an image directly from a pointer.

Many thanks in advance for your help.

PS : I use the latest SDL devel lib on MacOSX with Xcode._________________________________________________________________
Besoin d’un e-mail ? Cr?ez gratuitement un compte Windows Live Hotmail et gagnez du temps avec l’interface ? la Outlook !
http://www.windowslive.fr/hotmail/default.asp

Behalf Of Damien Fawks

Hi there,

I’m writing a piece of software to display a picture located on a server in
an SDL window. I’m getting the picture with libcurl using this code :

handle=curl_easy_init();

curl_easy_setopt(handle,CURLOPT_URL,“http://192.168.0.20/image.jpg”);
curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,update_img);
curl_easy_perform(handle);
curl_easy_cleanup(handle);

For performance reasons, I cannot make a disk access to store the picture
and recall it with with IMG_Load. So I wrote this fonction to display
directly the incoming image. curl_easy_setopt require a fonction with the
same prototype thant fwrite. I wrote this :

void update_img(void *ptr, size_t size, size_t nmemb, void *stream) {
img_position.x = 0;
img_position.y = 0;

webcam_img = IMG_Load(ptr);
SDL_BlitSurface(webcam_img, NULL, screen, &img_position);
    SDL_Flip(screen);
pause();

}

It doesn’t work because the IMG_Load argument needs to be the path to a file
(stored in an const *char). So I’m searching for an SDL function which can
display an image directly from a pointer.

Many thanks in advance for your help.


There is another function named IMG_Load_RW. You will need to create a
SDL_RWops structure and fill it our properly, but it may work. Once you
have that structure you can do something like this:

SDL_RWops 	myRWOps;	// You need to create/fill this correctly.
SDL_Surface * pSurface;

pSurface = IMG_Load_RW( myRWOps,0 );

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 6/23/2007
11:08 AM

----- Original Message -----
From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Sent: Sunday, June 24, 2007 9:20 AM
To: sdl at lists.libsdl.org
Subject: [SDL] Search function to display picture got with libcurl

Thank you. I didn’t knew this function. So I changed my function to this :

void update_img(void *ptr, int size, int nmemb, void *stream)
{
int img_size = size * nmemb;

SDL_RWops *dl_img = SDL_RWFromMem(ptr, img_size);
SDL_Surface *webcam_img = IMG_Load_RW(dl_img, 1);

img_position.x = 0;                                                                    
img_position.y = 0;
   
SDL_BlitSurface(webcam_img, NULL, screen, &img_position);
SDL_Flip(screen);
pause();

}

It work in fact : it get an image (better than a black window)

Unfortunately, I only have the first 1/10th of the image. Seems there is a buffer issue somewhere. Maybe libcurl call the function immediately when data come in and don’t wait the end of the transmission, so only a small part of the image is passed to the function.>


There is another function named IMG_Load_RW. You will need to create a
SDL_RWops structure and fill it our properly, but it may work. Once you
have that structure you can do something like this:

SDL_RWops 	myRWOps;	// You need to create/fill this correctly.
SDL_Surface * pSurface;

pSurface = IMG_Load_RW( myRWOps,0 );

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 6/23/2007
11:08 AM


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Besoin d’un e-mail ? Cr?ez gratuitement un compte Windows Live Hotmail, plus s?r, plus simple et plus complet !
http://www.windowslive.fr/hotmail/default.asp

Yes, it looks like the write function gets called for each block of data
that is transferred. You will need to wrap that with another function to
append each block onto your full stream and when you are done receiving data
then you can create your Image the way you showed below. I am not an expert
in using libcurl, so I may have this wrong.

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Damien Fawks
Sent: Sunday, June 24, 2007 11:05 AM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Search function to display picture got with libcurl

Thank you. I didn’t knew this function. So I changed my function to this :

void update_img(void *ptr, int size, int nmemb, void *stream) {
int img_size = size * nmemb;

SDL_RWops *dl_img = SDL_RWFromMem(ptr, img_size);
SDL_Surface *webcam_img = IMG_Load_RW(dl_img, 1);

img_position.x = 0;

img_position.y = 0;
   
SDL_BlitSurface(webcam_img, NULL, screen, &img_position);
SDL_Flip(screen);
pause();

}

It work in fact : it get an image (better than a black window)

Unfortunately, I only have the first 1/10th of the image. Seems there is a
buffer issue somewhere. Maybe libcurl call the function immediately when
data come in and don’t wait the end of the transmission, so only a small
part of the image is passed to the function.


There is another function named IMG_Load_RW. You will need to create
a SDL_RWops structure and fill it our properly, but it may work. Once
you have that structure you can do something like this:

SDL_RWops 	myRWOps;	// You need to create/fill this

correctly.

SDL_Surface * pSurface;

pSurface = IMG_Load_RW( myRWOps,0 );

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date:
6/23/2007
11:08 AM


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Besoin d’un e-mail ? Cr?ez gratuitement un compte Windows Live Hotmail, plus
s?r, plus simple et plus complet !
http://www.windowslive.fr/hotmail/default.asp


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 6/23/2007
11:08 AM

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 6/23/2007
11:08 AM

I took a look at the curl documentation and we are both correct in our
assumptions.

If you look at the sample “getinmemory.c” in the docs\examples folder you
will see the proper way to do what you want. The thing you are missing is
the writedata callback.

After you modify your code you have two choices on the image.

  1. You can keep the code to create and show the image in your writefunction
    call back (but use the mem->memory pointer instead of the current block
    memory pointer. This will allow you to show progressive gets of the data as
    a single large image. If you do this make sure you delete your SDL surface
    when you re-enter the function.

  2. You can just create and display the data after the call to
    curl_easy_perform() with the chunk.memory pointer. This will have all of
    the data for the entire image. Simplier of the two approaches.

I have attached the getinmemory.c file in case you don’t have access to it.

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Damien Fawks
Sent: Sunday, June 24, 2007 11:05 AM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Search function to display picture got with libcurl

Thank you. I didn’t knew this function. So I changed my function to this :

void update_img(void *ptr, int size, int nmemb, void *stream) {
int img_size = size * nmemb;

SDL_RWops *dl_img = SDL_RWFromMem(ptr, img_size);
SDL_Surface *webcam_img = IMG_Load_RW(dl_img, 1);

img_position.x = 0;

img_position.y = 0;
   
SDL_BlitSurface(webcam_img, NULL, screen, &img_position);
SDL_Flip(screen);
pause();

}

It work in fact : it get an image (better than a black window)

Unfortunately, I only have the first 1/10th of the image. Seems there is a
buffer issue somewhere. Maybe libcurl call the function immediately when
data come in and don’t wait the end of the transmission, so only a small
part of the image is passed to the function.


There is another function named IMG_Load_RW. You will need to create
a SDL_RWops structure and fill it our properly, but it may work. Once
you have that structure you can do something like this:

SDL_RWops 	myRWOps;	// You need to create/fill this

correctly.

SDL_Surface * pSurface;

pSurface = IMG_Load_RW( myRWOps,0 );

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date:
6/23/2007
11:08 AM


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Besoin d’un e-mail ? Cr?ez gratuitement un compte Windows Live Hotmail, plus
s?r, plus simple et plus complet !
http://www.windowslive.fr/hotmail/default.asp


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 6/23/2007
11:08 AM

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.472 / Virus Database: 269.9.6/863 - Release Date: 6/23/2007
11:08 AM

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: getinmemory.c
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070624/dfec0471/attachment.txt

Ok, many thanks for everything it works great now. I used getinmemory.c example code for that._________________________________________________________

I took a look at the curl documentation and we are both correct in our
assumptions.

If you look at the sample “getinmemory.c” in the docs\examples folder you
will see the proper way to do what you want. The thing you are missing is
the writedata callback.

After you modify your code you have two choices on the image.

  1. You can keep the code to create and show the image in your writefunction
    call back (but use the mem->memory pointer instead of the current block
    memory pointer. This will allow you to show progressive gets of the data as
    a single large image. If you do this make sure you delete your SDL surface
    when you re-enter the function.

  2. You can just create and display the data after the call to
    curl_easy_perform() with the chunk.memory pointer. This will have all of
    the data for the entire image. Simplier of the two approaches.

I have attached the getinmemory.c file in case you don’t have access to it.

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/


Windows Live Messenger vous offre 30 nouvelles ?motic?nes gratuites, install?es directement dans votre Messenger !
http://www.emoticones-messenger.fr/