FL_Shared_Image::get(...) returns NULL for an exists image

32 views
Skip to first unread message

K Dmitrij

unread,
Sep 27, 2017, 2:42:55 AM9/27/17
to fltkg...@googlegroups.com
For subj:


FL_Shared_Image::add() does qsort, and by that
original_ images stayed in end of array images_.
When happens calling bsearch (in find() method)
then it will does search only for first occurence.


In result: I have two images with same name, but
with different sizes. When I'm trying to get
third image with that name but with other sizes,
I got nothing.


In this moment I have using this hack:


[CODE=CPP]
/** Finds a shared image from its named and size specifications */
Fl_Shared_Image* Fl_Shared_Image::find(const char *n, int W, int H) {
  Fl_Shared_Image       *key,           // Image key
                        **match;        // Matching image


  if (num_images_) {
    key = new Fl_Shared_Image();
    key->name_ = new char[strlen(n) + 1];
    strcpy((char *)key->name_, n);
    key->w(W);
    key->h(H);


    for (int _i = 0; _i < num_images_; _i++) {
      if (compare(&key, &images_[_i]) == 0) {
        images_[_i]->refcount_ ++;
        return images_[_i];
      }
    }


    //     match = (Fl_Shared_Image **)bsearch(&key, images_, num_images_,
    //                                         sizeof(Fl_Shared_Image *),
    //                                         (compare_func_t)compare);
    //
    delete key;
    //
    //     if (match) {
    //       (*match)->refcount_ ++;
    //       return *match;
    //     }
  }


  return 0;
}
[/CODE]


PS: please, tell me how can I to subscribes for fltkcoredev via email ?

K Dmitrij

unread,
Sep 27, 2017, 2:45:15 AM9/27/17
to fltkg...@googlegroups.com

and there are needs to delete key before do return in for loop !





 
--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Google Groups allows you to create and participate in online forums and email-based groups with a rich experience for community conversations.


Manolo

unread,
Sep 27, 2017, 12:26:07 PM9/27/17
to fltk.general


On Wednesday, 27 September 2017 08:42:55 UTC+2, TheKdiman wrote:
For subj:


FL_Shared_Image::add() does qsort, and by that
original_ images stayed in end of array images_.
When happens calling bsearch (in find() method)
then it will does search only for first occurence.


In result: I have two images with same name, but
with different sizes. When I'm trying to get
third image with that name but with other sizes,
I got nothing.


This is my understanding of what you do:
  const char *fname = "/path/to/your/image.png";
  Fl_Shared_Image *ori = Fl_Shared_Image::get(fname); // creates a 1st shared image
  Fl_Shared_Image *cop = Fl_Shared_Image::get(fname,100,100); // creates a 2nd one with same name and another size
  Fl_Shared_Image *f = Fl_Shared_Image::find(fname,200,200); // call find() with yet another width x height
and you obtain f == NULL

This is exactly what the doc of Fl_Shared_Image::find(const char *name, int W=0, int H=0) predicts:

   Finds a shared image from its name and size specifications.

   If the image name exists with the exact width W and height H, then it is returned.

   If W == 0 and the image name exists with another size, then the original image with that name is returned.


To obtain a non-null result from the call to get() you either ask for an existing shared image with

the size of that image, or set W=0, and then you get the first created among those images with that name.


If I understand correctly what you would like to obtain, you could get it with

   Fl_Shared_Image *f = Fl_Shared_Image::find(fname);
   Fl_Shared_Image *f2  = (Fl_Shared_Image*)f->copy(200,200);

K Dmitrij

unread,
Sep 27, 2017, 10:25:24 PM9/27/17
to fltkg...@googlegroups.com

Manolo, please, read last messages this thread.




From: Manolo
 
Reply all
Reply to author
Forward
0 new messages