Welcome to mirror list, hosted at ThFree Co, Russian Federation.

icons.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 838a732e3871adf4b0eef0c1aeadc15aa7e70161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2006-2007 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 *
 */

/** \file blender/blenkernel/intern/icons.c
 *  \ingroup bke
 */


#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "DNA_group_types.h"
#include "DNA_lamp_types.h"
#include "DNA_material_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_texture_types.h"
#include "DNA_world_types.h"
#include "DNA_brush_types.h"

#include "BLI_utildefines.h"
#include "BLI_ghash.h"
#include "BLI_string.h"

#include "BKE_icons.h"
#include "BKE_global.h" /* only for G.background test */

#include "BLI_sys_types.h" // for intptr_t support

#include "GPU_texture.h"

#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "IMB_thumbs.h"

/* GLOBALS */

static GHash *gIcons = NULL;

static int gNextIconId = 1;

static int gFirstIconId = 1;

static GHash *gCachedPreviews = NULL;

static void icon_free(void *val)
{
	Icon *icon = val;

	if (icon) {
		if (icon->drawinfo_free) {
			icon->drawinfo_free(icon->drawinfo);
		}
		else if (icon->drawinfo) {
			MEM_freeN(icon->drawinfo);
		}
		MEM_freeN(icon);
	}
}

/* create an id for a new icon and make sure that ids from deleted icons get reused
 * after the integer number range is used up */
static int get_next_free_id(void)
{
	int startId = gFirstIconId;

	/* if we haven't used up the int number range, we just return the next int */
	if (gNextIconId >= gFirstIconId)
		return gNextIconId++;
	
	/* now we try to find the smallest icon id not stored in the gIcons hash */
	while (BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(startId)) && startId >= gFirstIconId)
		startId++;

	/* if we found a suitable one that isn't used yet, return it */
	if (startId >= gFirstIconId)
		return startId;

	/* fail */
	return 0;
}

void BKE_icons_init(int first_dyn_id)
{
	gNextIconId = first_dyn_id;
	gFirstIconId = first_dyn_id;

	if (!gIcons)
		gIcons = BLI_ghash_int_new(__func__);

	if (!gCachedPreviews) {
		gCachedPreviews = BLI_ghash_str_new(__func__);
	}
}

void BKE_icons_free(void)
{
	if (gIcons) {
		BLI_ghash_free(gIcons, NULL, icon_free);
		gIcons = NULL;
	}

	if (gCachedPreviews) {
		BLI_ghash_free(gCachedPreviews, MEM_freeN, BKE_previewimg_freefunc);
		gCachedPreviews = NULL;
	}
}

static PreviewImage *previewimg_create_ex(size_t deferred_data_size)
{
	PreviewImage *prv_img = NULL;
	int i;

	prv_img = MEM_mallocN(sizeof(PreviewImage) + deferred_data_size, "img_prv");
	memset(prv_img, 0, sizeof(*prv_img));  /* leave deferred data dirty */

	if (deferred_data_size) {
		prv_img->tag |= PRV_TAG_DEFFERED;
	}

	for (i = 0; i < NUM_ICON_SIZES; ++i) {
		prv_img->flag[i] |= PRV_CHANGED;
		prv_img->changed_timestamp[i] = 0;
	}
	return prv_img;
}

PreviewImage *BKE_previewimg_create(void)
{
	return previewimg_create_ex(0);
}

static void previewimg_gputexture_free_single(PreviewImage *prv, enum eIconSizes size)
{
	if (prv->gputexture[size]) {
		GPU_texture_free(prv->gputexture[size]);
		prv->gputexture[size] = NULL;
	}
}

void BKE_previewimg_freefunc(void *link)
{
	PreviewImage *prv = (PreviewImage *)link;
	if (prv) {
		int i;

		for (i = 0; i < NUM_ICON_SIZES; ++i) {
			if (prv->rect[i]) {
				MEM_freeN(prv->rect[i]);
			}
			previewimg_gputexture_free_single(prv, i);
		}
		
		MEM_freeN(prv);
	}
}

void BKE_previewimg_free(PreviewImage **prv)
{
	if (prv && (*prv)) {
		BKE_previewimg_freefunc(*prv);
		*prv = NULL;
	}
}

void BKE_previewimg_clear_single(PreviewImage *prv, enum eIconSizes size)
{
	MEM_SAFE_FREE(prv->rect[size]);
	previewimg_gputexture_free_single(prv, size);
	prv->h[size] = prv->w[size] = 0;
	prv->flag[size] |= PRV_CHANGED;
	prv->flag[size] &= ~PRV_USER_EDITED;
	prv->changed_timestamp[size] = 0;
}

void BKE_previewimg_clear(struct PreviewImage *prv)
{
	prv->num_frames = 0;
	for (int i = 0; i < NUM_ICON_SIZES; ++i) {
		BKE_previewimg_clear_single(prv, i);
	}
}

PreviewImage *BKE_previewimg_copy(PreviewImage *prv)
{
	PreviewImage *prv_img = NULL;
	int i;

	if (prv) {
		prv_img = MEM_dupallocN(prv);
		for (i = 0; i < NUM_ICON_SIZES; ++i) {
			if (prv->rect[i]) {
				prv_img->rect[i] = MEM_dupallocN(prv->rect[i]);
			}
			prv_img->gputexture[i] = NULL;
		}
	}
	return prv_img;
}

#define PREVIEWIMG_OFFSET(_prv, _size, _idx) \
	((_idx) * (_prv)->w[(_size)] * (_prv)->h[(_size)] + (_idx))

size_t BKE_previewimg_get_rect_size(struct PreviewImage *prv, const int size)
{
	if (prv->num_frames != 0) {
		return PREVIEWIMG_OFFSET(prv, size, prv->num_frames) * sizeof(*prv->rect[size]);
	}
	else {
		return prv->w[size] * prv->h[size] * sizeof(*prv->rect[size]);
	}
}

void BKE_previewimg_num_frames_set(struct PreviewImage *prv, const short num_frames)
{
	BLI_assert(prv != NULL);

	if (num_frames == prv->num_frames) {
		return;
	}

	prv->num_frames = num_frames;
	for (int i = 0; i < NUM_ICON_SIZES; i++) {
		if (prv->rect[i]) {
			prv->rect[i] = MEM_recallocN_id(prv->rect[i], BKE_previewimg_get_rect_size(prv, i), __func__);
		}
		previewimg_gputexture_free_single(prv, i);
	}
}

void BKE_previewimg_frame_delete(struct PreviewImage *prv, const short frame_idx)
{
	BLI_assert(prv != NULL && frame_idx < prv->num_frames && frame_idx >= 0);

	if (prv->num_frames <= 1) {
		BKE_previewimg_clear(prv);
		return;
	}

	prv->num_frames--;
	for (int i = 0; i < NUM_ICON_SIZES; i++) {
		if (prv->rect[i]) {
			if (frame_idx != prv->num_frames) {
				unsigned int *p_del_idx = &prv->rect[i][PREVIEWIMG_OFFSET(prv, i, frame_idx)];
				unsigned int *p_nxt_idx = &prv->rect[i][PREVIEWIMG_OFFSET(prv, i, frame_idx + 1)];
				size_t nxt_size = (size_t)(&prv->rect[i][PREVIEWIMG_OFFSET(prv, i, prv->num_frames + 1)] - p_nxt_idx) * sizeof(*prv->rect[i]);
				memmove(p_del_idx, p_nxt_idx, nxt_size);
			}
			prv->rect[i] = MEM_reallocN_id(prv->rect[i], BKE_previewimg_get_rect_size(prv, i), __func__);
		}
		previewimg_gputexture_free_single(prv, i);
	}
}

unsigned int *BKE_previewimg_frame_data_get(
        const PreviewImage *prv, const unsigned short frame_idx, const enum eIconSizes size, int *r_meta)
{
	BLI_assert(prv != NULL && frame_idx < prv->num_frames);

	unsigned int *frame = NULL;
	if (r_meta) {
		*r_meta = 0;
	}

	if (prv->rect[size]) {
		frame = &prv->rect[size][PREVIEWIMG_OFFSET(prv, size, frame_idx)];
		if (r_meta && prv->num_frames > 0) {
			*r_meta = frame[prv->w[size] * prv->h[size]];
		}
	}
	return frame;
}

/* Would we want to rather use a search callback? */
unsigned int *BKE_previewimg_frame_data_search(
        const PreviewImage *prv, const int meta, const enum eIconSizes size, unsigned short *r_frame_idx)
{
	BLI_assert(prv != NULL);

	unsigned int *frame = NULL;
	if (r_frame_idx) {
		*r_frame_idx = 0;
	}

	if (prv->rect[size]) {
		if (prv->num_frames == 0) {
			if (meta == 0) {  /* Convention... */
				frame = prv->rect[size];
			}
		}
		else {
			ptrdiff_t stride = &prv->rect[size][PREVIEWIMG_OFFSET(prv, size, 1)] - &prv->rect[size][0];
			unsigned int *frame_it = prv->rect[size];
			for (unsigned short i = 0; i < prv->num_frames; i++, frame_it += stride) {
				if (frame_it[prv->w[size] * prv->h[size]] == meta) {
					if (r_frame_idx) {
						*r_frame_idx = i;
					}
					frame = frame_it;
					break;
				}
			}
		}
	}
	return frame;
}

void BKE_previewimg_frame_data_set(
        const PreviewImage *prv,
        const unsigned short frame_idx, const enum eIconSizes size, const int meta, const unsigned int *data)
{
	BLI_assert(prv != NULL && frame_idx < prv->num_frames);

	unsigned int *frame = &prv->rect[size][PREVIEWIMG_OFFSET(prv, size, frame_idx)];
	memcpy(frame, data, prv->w[size] * prv->h[size] * sizeof(*prv->rect[size]));
	if (prv->num_frames > 0) {
		frame[prv->w[size] * prv->h[size]] = meta;
	}
}

/** Duplicate preview image from \a id and clear icon_id, to be used by datablock copy functions. */
void BKE_previewimg_id_copy(ID *new_id, ID *old_id)
{
	PreviewImage **old_prv_p = BKE_previewimg_id_get_p(old_id);
	PreviewImage **new_prv_p = BKE_previewimg_id_get_p(new_id);

	if (old_prv_p && *old_prv_p) {
		BLI_assert(new_prv_p != NULL && ELEM(*new_prv_p, NULL, *old_prv_p));
//		const int new_icon_id = get_next_free_id();

//		if (new_icon_id == 0) {
//			return;  /* Failure. */
//		}
		*new_prv_p = BKE_previewimg_copy(*old_prv_p);
		new_id->icon_id = (*new_prv_p)->icon_id = 0;
	}
}

PreviewImage **BKE_previewimg_id_get_p(ID *id)
{
	switch (GS(id->name)) {
#define ID_PRV_CASE(id_code, id_struct) case id_code: { return &((id_struct *)id)->preview; } ((void)0)
		ID_PRV_CASE(ID_MA, Material);
		ID_PRV_CASE(ID_TE, Tex);
		ID_PRV_CASE(ID_WO, World);
		ID_PRV_CASE(ID_LA, Lamp);
		ID_PRV_CASE(ID_IM, Image);
		ID_PRV_CASE(ID_BR, Brush);
		ID_PRV_CASE(ID_OB, Object);
		ID_PRV_CASE(ID_GR, Group);
		ID_PRV_CASE(ID_SCE, Scene);
		ID_PRV_CASE(ID_AC, bAction);
#undef ID_PRV_CASE
	}

	return NULL;
}

void BKE_previewimg_id_free(ID *id)
{
	PreviewImage **prv_p = BKE_previewimg_id_get_p(id);
	if (prv_p) {
		BKE_previewimg_free(prv_p);
	}
}

PreviewImage *BKE_previewimg_id_ensure(ID *id)
{
	PreviewImage **prv_p = BKE_previewimg_id_get_p(id);

	if (prv_p) {
		if (*prv_p == NULL) {
			*prv_p = BKE_previewimg_create();
		}
		return *prv_p;
	}

	return NULL;
}

PreviewImage *BKE_previewimg_cached_get(const char *name)
{
	return BLI_ghash_lookup(gCachedPreviews, name);
}

/**
 * Generate an empty PreviewImage, if not yet existing.
 */
PreviewImage *BKE_previewimg_cached_ensure(const char *name)
{
	PreviewImage *prv = NULL;
	void **key_p, **prv_p;

	if (!BLI_ghash_ensure_p_ex(gCachedPreviews, name, &key_p, &prv_p)) {
		*key_p = BLI_strdup(name);
		*prv_p = BKE_previewimg_create();
	}
	prv = *prv_p;
	BLI_assert(prv);

	return prv;
}

/**
 * Generate a PreviewImage from given file path, using thumbnails management, if not yet existing.
 */
PreviewImage *BKE_previewimg_cached_thumbnail_read(
        const char *name, const char *path, const int source, bool force_update)
{
	PreviewImage *prv = NULL;
	void **prv_p;

	prv_p = BLI_ghash_lookup_p(gCachedPreviews, name);

	if (prv_p) {
		prv = *prv_p;
		BLI_assert(prv);
	}

	if (prv && force_update) {
		const char *prv_deferred_data = PRV_DEFERRED_DATA(prv);
		if (((int)prv_deferred_data[0] == source) && STREQ(&prv_deferred_data[1], path)) {
			/* If same path, no need to re-allocate preview, just clear it up. */
			BKE_previewimg_clear(prv);
		}
		else {
			BKE_previewimg_free(&prv);
		}
	}

	if (!prv) {
		/* We pack needed data for lazy loading (source type, in a single char, and path). */
		const size_t deferred_data_size = strlen(path) + 2;
		char *deferred_data;

		prv = previewimg_create_ex(deferred_data_size);
		deferred_data = PRV_DEFERRED_DATA(prv);
		deferred_data[0] = source;
		memcpy(&deferred_data[1], path, deferred_data_size - 1);

		force_update = true;
	}

	if (force_update) {
		if (prv_p) {
			*prv_p = prv;
		}
		else {
			BLI_ghash_insert(gCachedPreviews, BLI_strdup(name), prv);
		}
	}

	return prv;
}

void BKE_previewimg_cached_release_pointer(PreviewImage *prv)
{
	if (prv) {
		if (prv->tag & PRV_TAG_DEFFERED_RENDERING) {
			/* We cannot delete the preview while it is being loaded in another thread... */
			prv->tag |= PRV_TAG_DEFFERED_DELETE;
			return;
		}
		if (prv->icon_id) {
			BKE_icon_delete(prv->icon_id);
		}
		BKE_previewimg_freefunc(prv);
	}
}

void BKE_previewimg_cached_release(const char *name)
{
	PreviewImage *prv = BLI_ghash_popkey(gCachedPreviews, name, MEM_freeN);

	BKE_previewimg_cached_release_pointer(prv);
}

/** Handle deferred (lazy) loading/generation of preview image, if needed.
 * For now, only used with file thumbnails. */
void BKE_previewimg_ensure(PreviewImage *prv, const int size)
{
	if ((prv->tag & PRV_TAG_DEFFERED) != 0) {
		const bool do_icon = ((size == ICON_SIZE_ICON) && !prv->rect[ICON_SIZE_ICON]);
		const bool do_preview = ((size == ICON_SIZE_PREVIEW) && !prv->rect[ICON_SIZE_PREVIEW]);

		if (do_icon || do_preview) {
			ImBuf *thumb;
			char *prv_deferred_data = PRV_DEFERRED_DATA(prv);
			int source =  prv_deferred_data[0];
			char *path = &prv_deferred_data[1];
			int icon_w, icon_h;

			thumb = IMB_thumb_manage(path, THB_LARGE, source);

			if (thumb) {
				/* PreviewImage assumes premultiplied alhpa... */
				IMB_premultiply_alpha(thumb);

				if (do_preview) {
					prv->w[ICON_SIZE_PREVIEW] = thumb->x;
					prv->h[ICON_SIZE_PREVIEW] = thumb->y;
					prv->rect[ICON_SIZE_PREVIEW] = MEM_dupallocN(thumb->rect);
					prv->flag[ICON_SIZE_PREVIEW] &= ~(PRV_CHANGED | PRV_USER_EDITED);
				}
				if (do_icon) {
					if (thumb->x > thumb->y) {
						icon_w = ICON_RENDER_DEFAULT_HEIGHT;
						icon_h = (thumb->y * icon_w) / thumb->x + 1;
					}
					else if (thumb->x < thumb->y) {
						icon_h = ICON_RENDER_DEFAULT_HEIGHT;
						icon_w = (thumb->x * icon_h) / thumb->y + 1;
					}
					else {
						icon_w = icon_h = ICON_RENDER_DEFAULT_HEIGHT;
					}

					IMB_scaleImBuf(thumb, icon_w, icon_h);
					prv->w[ICON_SIZE_ICON] = icon_w;
					prv->h[ICON_SIZE_ICON] = icon_h;
					prv->rect[ICON_SIZE_ICON] = MEM_dupallocN(thumb->rect);
					prv->flag[ICON_SIZE_ICON] &= ~(PRV_CHANGED | PRV_USER_EDITED);
				}
				IMB_freeImBuf(thumb);
			}
		}
	}
}

void BKE_icon_changed(int id)
{
	Icon *icon = NULL;
	
	if (!id || G.background) return;

	icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(id));
	
	if (icon) {
		PreviewImage *prv = BKE_previewimg_id_ensure((ID *)icon->obj);

		/* all previews changed */
		if (prv) {
			int i;
			for (i = 0; i < NUM_ICON_SIZES; ++i) {
				prv->flag[i] |= PRV_CHANGED;
				prv->changed_timestamp[i]++;
			}
		}
	}
}

static int icon_id_ensure_create_icon(struct ID *id)
{
	Icon *new_icon = NULL;

	new_icon = MEM_mallocN(sizeof(Icon), __func__);

	new_icon->obj = id;
	new_icon->type = GS(id->name);

	/* next two lines make sure image gets created */
	new_icon->drawinfo = NULL;
	new_icon->drawinfo_free = NULL;

	BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(id->icon_id), new_icon);

	return id->icon_id;
}

int BKE_icon_id_ensure(struct ID *id)
{
	if (!id || G.background)
		return 0;

	if (id->icon_id)
		return id->icon_id;

	id->icon_id = get_next_free_id();

	if (!id->icon_id) {
		printf("%s: Internal error - not enough IDs\n", __func__);
		return 0;
	}

	/* Ensure we synchronize ID icon_id with its previewimage if it has one. */
	PreviewImage **p_prv = BKE_previewimg_id_get_p(id);
	if (p_prv && *p_prv) {
		BLI_assert(ELEM((*p_prv)->icon_id, 0, id->icon_id));
		(*p_prv)->icon_id = id->icon_id;
	}

	return icon_id_ensure_create_icon(id);
}

/**
 * Return icon id of given preview, or create new icon if not found.
 */
int BKE_icon_preview_ensure(ID *id, PreviewImage *preview)
{
	Icon *new_icon = NULL;

	if (!preview || G.background)
		return 0;

	if (id) {
		BLI_assert(BKE_previewimg_id_ensure(id) == preview);
	}

	if (preview->icon_id) {
		BLI_assert(!id || !id->icon_id || id->icon_id == preview->icon_id);
		return preview->icon_id;
	}

	if (id && id->icon_id) {
		preview->icon_id = id->icon_id;
		return preview->icon_id;
	}

	preview->icon_id = get_next_free_id();

	if (!preview->icon_id) {
		printf("%s: Internal error - not enough IDs\n", __func__);
		return 0;
	}

	/* Ensure we synchronize ID icon_id with its previewimage if available, and generate suitable 'ID' icon. */
	if (id) {
		id->icon_id = preview->icon_id;
		return icon_id_ensure_create_icon(id);
	}

	new_icon = MEM_mallocN(sizeof(Icon), __func__);

	new_icon->obj = preview;
	new_icon->type = 0;  /* Special, tags as non-ID icon/preview. */

	/* next two lines make sure image gets created */
	new_icon->drawinfo = NULL;
	new_icon->drawinfo_free = NULL;

	BLI_ghash_insert(gIcons, SET_INT_IN_POINTER(preview->icon_id), new_icon);

	return preview->icon_id;
}

Icon *BKE_icon_get(int icon_id)
{
	Icon *icon = NULL;

	icon = BLI_ghash_lookup(gIcons, SET_INT_IN_POINTER(icon_id));
	
	if (!icon) {
		printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
		return NULL;
	}

	return icon;
}

void BKE_icon_set(int icon_id, struct Icon *icon)
{
	void **val_p;

	if (BLI_ghash_ensure_p(gIcons, SET_INT_IN_POINTER(icon_id), &val_p)) {
		printf("%s: Internal error, icon already set: %d\n", __func__, icon_id);
		return;
	}

	*val_p = icon;
}

void BKE_icon_id_delete(struct ID *id)
{
	if (!id->icon_id) return;  /* no icon defined for library object */

	BLI_ghash_remove(gIcons, SET_INT_IN_POINTER(id->icon_id), NULL, icon_free);
	id->icon_id = 0;
}

/**
 * Remove icon and free data.
 */
void BKE_icon_delete(int icon_id)
{
	Icon *icon;

	if (!icon_id) return;  /* no icon defined for library object */

	icon = BLI_ghash_popkey(gIcons, SET_INT_IN_POINTER(icon_id), NULL);

	if (icon) {
		if (icon->type) {
			((ID *)(icon->obj))->icon_id = 0;
		}
		else {
			((PreviewImage *)(icon->obj))->icon_id = 0;
		}
		icon_free(icon);
	}
}