How to restore users (id.us) of an Image * stored in ListBase

Hey guys!
I’m trying to extend the Blender’s painting system, to make it be able to paint with multiple canvas and textures. I’ve tested with two fixed canvas, it works.
I’m starting to extending the Brush with a ListBase which will store PaintTarget

typedef struct Brush{
  ListBase mtexs;
} Brush;

typedef struct PaintTarget {
  struct PaintTarget *next, *prev;
  struct ImageUser iuser;
  struct Tex *tex;
  struct Image *ima;   // the canvas image
} PaintTarget;

I’ve already defined the dna/rna stuff.

Write in writefile.c

// static void write_brush
// adapted from color palette
PaintTarget *paint_target;
for (paint_target = brush->mtexs.first; paint_target; paint_target = paint_target->next) {
    BLO_write_struct(writer, PaintTarget, paint_target);
}

Read in readfile.c

// static void direct_link_brush
BLO_read_list(reader, &(brush->mtexs));

// static void lib_link_brush
LISTBASE_FOREACH (PaintTarget *, paint_target, &brush->mtexs) {
  paint_target->iuser.ok = 1;
  paint_target->iuser.scene = NULL;
  if (paint_target->ima) {
    paint_target->ima = newlibadr(fd, brush->id.lib, paint_target->ima);
  }
}

The code above worked almost. But after save it to disk and reload it lost the user of that Image, the UI looks like this
Screenshot from 2020-06-17 22-11-09
In Python Console,
bpy.data.images['Material Base Color.png'].users equals to 0

Brushes should not store pointers to canvas images. They should be independent of the mesh and shaders that they work on. So this specific design is not something we’d accept in Blender.

It would be useful to support brushes that paint e.g. base color and metallic textures at the same time, but this would probably tie into a bigger design change to better support multi-layer image painting, where information about which images correspond to which layers is available and brushes would use that.

Regarding the implementation, I suggest to look at the Brush clone.image member as an example, see how that is used. Perhaps you didn’t add the canvas pointer in brush_foreach_id.

Thanks for your reply.
I saw the multi-layer image painting TODO in blender projects, it looks cool.
I guess it’s only for the canvas, right? So ListBase of texture corresponding to each layer will be need in a brush.
I’ll start to look into this.