summaryrefslogtreecommitdiff
path: root/im/src/im_image.cpp
blob: 5f865f8e8411f5cbc229a7915905fc3b3e3dd192 (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
729
730
731
732
733
734
735
736
737
/** \file
 * \brief Image Manipulation
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_image.cpp,v 1.7 2010/01/17 18:18:12 scuri Exp $
 */

#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <assert.h>

#include "im.h"
#include "im_image.h"
#include "im_util.h"
#include "im_attrib.h"
#include "im_file.h"


int imImageCheckFormat(int color_mode, int data_type)
{
  if ((imColorModeSpace(color_mode) == IM_MAP || imColorModeSpace(color_mode) == IM_BINARY) &&
      (data_type != IM_BYTE))
    return 0;

  return 1;
}

int imImagePixelOffset(int is_packed, int width, int height, int depth, int col, int row, int plane)
{
  if (is_packed) 
    return row*width*depth + col*depth + plane;
  else           
    return plane*width*height + row*width + col;
}

int imImageDataSize(int width, int height, int color_mode, int data_type)
{
  return width * height * imColorModeDepth(color_mode) * imDataTypeSize(data_type);
}
                           
int imImageLineCount(int width, int color_mode)
{
  if (imColorModeIsPacked(color_mode))
    return width*imColorModeDepth(color_mode);
  else
    return width;
}

int imImageLineSize(int width, int color_mode, int data_type)
{
  return imImageLineCount(width, color_mode) * imDataTypeSize(data_type);
}

static void iImageInit(imImage* image, int width, int height, int color_space, int data_type, int has_alpha)
{
  assert(width>0);
  assert(height>0);
  assert(color_space >= IM_RGB && color_space <= IM_XYZ);
  assert(data_type >= IM_BYTE && data_type <= IM_CFLOAT);

  image->width = width;
  image->height = height;
  image->color_space = color_space;
  image->data_type = data_type;
  image->has_alpha = has_alpha;

  image->depth = imColorModeDepth(color_space);
  image->line_size = image->width * imDataTypeSize(data_type); 
  image->plane_size = image->line_size * image->height; 
  image->size = image->plane_size * image->depth;
  image->count = image->width * image->height; 

  int depth = image->depth+1;  // add room for an alpha plane pointer, even if does not have alpha now.

  if (image->data)
  {
    /* if reallocating, preserve the data buffer */
    void* data0 = image->data[0];

    free(image->data);
    image->data = (void**)malloc(depth * sizeof(void*));  

    image->data[0] = data0;
  }
  else
    image->data = (void**)malloc(depth * sizeof(void*));
}

imImage* imImageInit(int width, int height, int color_space, int data_type, void* data_buffer, long* palette, int palette_count)
{
  if (!imImageCheckFormat(color_space, data_type))
    return NULL;
                 
  imImage* image = (imImage*)malloc(sizeof(imImage));
  image->data = 0;
    
  iImageInit(image, width, height, color_space, data_type, 0);

  if (data_buffer)
  {
    for (int d = 0; d < image->depth; d++)
      image->data[d] = (imbyte*)data_buffer + d*image->plane_size;
  }

  if (imColorModeDepth(color_space) == 1)
  {
    image->palette = palette;
    image->palette_count = palette_count;
  }
  else
  {
    image->palette = NULL;
    image->palette_count = 0;
  }

  image->attrib_table = new imAttribTable(599);

  return image;
}

imImage* imImageCreate(int width, int height, int color_space, int data_type)
{
  imImage* image = imImageInit(width, height, color_space, data_type, NULL, NULL, 0);
  if (!image) return NULL;

  /* palette is available to BINARY, MAP and GRAY */
  if (imColorModeDepth(color_space) == 1)
  {
    image->palette = (long*)malloc(256*sizeof(long));

    if (image->color_space == IM_BINARY)
    {
      image->palette_count = 2;
      image->palette[0] = imColorEncode(0, 0, 0);
      image->palette[1] = imColorEncode(255, 255, 255);
    }
    else
    {
      image->palette_count = 256;
      for (int i = 0; i < 256; i++)
        image->palette[i] = imColorEncode((imbyte)i, (imbyte)i, (imbyte)i);
    }
  }
  
  /* allocate data buffer */
  image->data[0] = malloc(image->size);
  if (!image->data[0])
  {
    imImageDestroy(image);
    return NULL;
  }

  /* initialize data plane pointers */
  for (int d = 1; d < image->depth; d++)
    image->data[d] = (imbyte*)(image->data[0]) + d*image->plane_size;

  imImageClear(image);

  return image;
}

imImage* imImageCreateBased(const imImage* image, int width, int height, int color_space, int data_type)
{
  assert(image);

  if (width <= 0) width = image->width;
  if (height <= 0) height = image->height;
  if (color_space < 0) color_space = image->color_space;
  if (data_type < 0) data_type = image->data_type;

  imImage* new_image = imImageCreate(width, height, color_space, data_type);
  imImageCopyAttributes(image, new_image);

  if (image->has_alpha)
    imImageAddAlpha(new_image);

  return new_image;
}

void imImageAddAlpha(imImage* image)
{
  assert(image);

  if (image->has_alpha)
    return;

  unsigned char* new_data = (unsigned char*)realloc(image->data[0], image->size+image->plane_size);
  if (!new_data)
    return;

 image->data[0] = new_data;
  for (int d = 1; d < image->depth+1; d++)
    image->data[d] = (imbyte*)(image->data[0]) + d*image->plane_size;

  memset(image->data[image->depth], 0, image->plane_size);

  image->has_alpha = IM_ALPHA;
}

void imImageReshape(imImage* image, int width, int height)
{
  assert(image);

  int old_size = image->size, 
      old_width = width, 
      old_height = height;

  iImageInit(image, width, height, image->color_space, image->data_type, image->has_alpha);

  if (old_size < image->size)
  {
    void* data0 = realloc(image->data[0], image->has_alpha? image->size+image->plane_size: image->size);
    if (!data0) // if failed restore the previous size
      iImageInit(image, old_width, old_height, image->color_space, image->data_type, image->has_alpha);
    else
      image->data[0] = data0;
  }

  /* initialize data plane pointers */
  int depth = image->has_alpha? image->depth+1: image->depth;
  for (int d = 1; d < depth; d++)
    image->data[d] = (imbyte*)image->data[0] + d*image->plane_size;
}

void imImageDestroy(imImage* image)
{
  assert(image);

  imAttribTable* attrib_table = (imAttribTable*)image->attrib_table;
  delete attrib_table;

  if (image->data[0])
    free(image->data[0]);

  if (image->palette)
    free(image->palette);

  free(image->data);

  // This will help detect invalid image usage after destroy.
  memset(image, 0, sizeof(imImage));

  free(image);
}

void imImageClear(imImage* image)
{
  assert(image);

  if ((image->color_space == IM_YCBCR || image->color_space == IM_LAB || image->color_space == IM_LUV) && 
      (image->data_type == IM_BYTE || image->data_type == IM_USHORT))
  {
    memset(image->data[0], 0, image->plane_size);

    if (image->data_type == IM_BYTE)
    {
      imbyte* usdata = (imbyte*)image->data[1];
      for (int i = 0; i < 2*image->count; i++)
        *usdata++ = 128;
    }
    else
    {
      imushort* usdata = (imushort*)image->data[1];
      for (int i = 0; i < 2*image->count; i++)
        *usdata++ = 32768;
    }
  }
  else
    memset(image->data[0], 0, image->size);

  if (image->has_alpha)
    memset(image->data[image->depth], 0, image->plane_size);
}

template <class T> 
inline void iSet(T *map, T value, int count)
{
  for (int i = 0; i < count; i++)
  {
    *map++ = value;
  }
}
  
void imImageSetAlpha(imImage* image, float alpha)
{
  assert(image);

  if (image->has_alpha)
  {
    switch(image->data_type)
    {
    case IM_BYTE:
      memset(image->data[image->depth], (imbyte)alpha, image->plane_size);
      break;                                                                                
    case IM_USHORT:                                                                           
      iSet((imushort*)image->data[image->depth], (imushort)alpha, image->plane_size);
      break;                                                                                
    case IM_INT:                                                                           
      iSet((int*)image->data[image->depth], (int)alpha, image->plane_size);
      break;                                                                                
    case IM_FLOAT:                                                                           
      iSet((float*)image->data[image->depth], (float)alpha, image->plane_size);
      break;                                                                                
    }
  }
}

int imImageIsBitmap(const imImage* image)
{
  assert(image);
  return imColorModeIsBitmap(image->color_space, image->data_type);
}

void imImageCopy(const imImage* src_image, imImage* dst_image)
{
  assert(src_image);
  assert(dst_image);

  imImageCopyData(src_image, dst_image);

  if (dst_image != src_image)
    imImageCopyAttributes(src_image, dst_image);
}

void imImageCopyData(const imImage* src_image, imImage* dst_image)
{
  assert(src_image);
  assert(dst_image);
  assert(imImageMatch(src_image, dst_image));

  if (dst_image != src_image)
  {
    memcpy(dst_image->data[0], src_image->data[0], (src_image->has_alpha && dst_image->has_alpha)? src_image->size+src_image->plane_size: src_image->size);
  }
}

void imImageCopyPlane(const imImage* src_image, int src_plane, imImage* dst_image, int dst_plane)
{
  assert(src_image);
  assert(dst_image);
  assert(imImageMatchDataType(src_image, dst_image));

  memcpy(dst_image->data[dst_plane], src_image->data[src_plane], src_image->plane_size);
}

imImage* imImageDuplicate(const imImage* image)
{
  assert(image);

  imImage* new_image = imImageCreate(image->width, image->height, image->color_space, image->data_type);
  if (!new_image)
    return NULL;

  if (image->has_alpha)
    imImageAddAlpha(new_image);

  imImageCopy(image, new_image);

  return new_image;
}

imImage* imImageClone(const imImage* image)
{
  assert(image);

  imImage* new_image = imImageCreate(image->width, image->height, image->color_space, image->data_type);
  if (!new_image)
    return NULL;

  if (image->has_alpha)
    imImageAddAlpha(new_image);

  imImageCopyAttributes(image, new_image);

  return new_image;
}

void imImageSetAttribute(const imImage* image, const char* attrib, int data_type, int count, const void* data)
{
  assert(image);
  assert(attrib);
  imAttribTable* attrib_table = (imAttribTable*)image->attrib_table;
  if (data)
  {
    if (count == -1 && data_type == IM_BYTE) // Data is zero terminated like a string
      count = strlen((char*)data)+1;

    attrib_table->Set(attrib, data_type, count, data);
  }
  else if (count == 0)
    attrib_table->UnSet(attrib);
  else
    attrib_table->Set(attrib, data_type, count, NULL);
}

const void* imImageGetAttribute(const imImage* image, const char* attrib, int *data_type, int *count)
{
  assert(image);
  assert(attrib);
  imAttribTable* attrib_table = (imAttribTable*)image->attrib_table;
  return attrib_table->Get(attrib, data_type, count);
}

static void iAttributeTableCopy(const void* src_attrib_table, void* dst_attrib_table)
{
  const imAttribTable* src_table = (const imAttribTable*)src_attrib_table;
  imAttribTable* dst_table = (imAttribTable*)dst_attrib_table;
  dst_table->CopyFrom(*src_table);
}

void imImageCopyAttributes(const imImage* src_image, imImage* dst_image)
{
  assert(src_image);
  assert(dst_image);

  if (src_image->palette && dst_image->palette &&
      src_image->color_space == dst_image->color_space)
  {
    memcpy(dst_image->palette, src_image->palette, 256*sizeof(long));
    dst_image->palette_count = src_image->palette_count;
  }

  iAttributeTableCopy(src_image->attrib_table, dst_image->attrib_table);
}

static int iAttribCB(void* user_data, int index, const char* name, int data_type, int count, const void* data)
{
  (void)data_type;
  (void)data;
  (void)count;
  char** attrib = (char**)user_data;
  attrib[index] = (char*)name;
  return 1;
}

void imImageGetAttributeList(const imImage* image, char** attrib, int *attrib_count)
{
  assert(image);
  assert(attrib_count);

  imAttribTable* attrib_table = (imAttribTable*)image->attrib_table;
  *attrib_count = attrib_table->Count();

  if (attrib) attrib_table->ForEach((void*)attrib, iAttribCB);
}

void imImageSetPalette(imImage* image, long* palette, int palette_count)
{
  assert(image);

  if (image->palette)
  {
    free(image->palette);
    image->palette = palette;
    image->palette_count = palette_count;
  }
}

int imImageMatchSize(const imImage* image1, const imImage* image2)
{
  assert(image1);
  assert(image2);

  return ((image1->width == image2->width) &&
          (image1->height == image2->height));
}

int imImageMatchColor(const imImage* image1, const imImage* image2)
{
  assert(image1);
  assert(image2);

  return (image1->data_type == image2->data_type &&
          image1->color_space == image2->color_space);
}

int imImageMatchDataType(const imImage* image1, const imImage* image2)
{
  assert(image1);
  assert(image2);

  return (image1->data_type == image2->data_type &&
          image1->width == image2->width &&
          image1->height == image2->height);
}

int imImageMatchColorSpace(const imImage* image1, const imImage* image2)
{
  assert(image1);
  assert(image2);

  return (image1->width == image2->width &&
          image1->height == image2->height &&
          image1->color_space == image2->color_space);
}

int imImageMatch(const imImage* image1, const imImage* image2)
{
  assert(image1);
  assert(image2);

  return (image1->data_type == image2->data_type &&
          image1->width == image2->width &&
          image1->height == image2->height &&
          image1->color_space == image2->color_space);
}

void imImageSetBinary(imImage* image)
{
  assert(image);

  if (image->palette)
  {
    image->color_space = IM_BINARY;
    image->palette_count = 2;
    image->palette[0] = imColorEncode(0, 0, 0);
    image->palette[1] = imColorEncode(255, 255, 255);
  }
}

void imImageMakeBinary(imImage *image)
{
  assert(image);

  imbyte *map = (imbyte*)image->data[0];
  for(int i = 0; i < image->count; i++)
  {
    if (*map)
      *map = 1;
    map++;
  }
}

void imImageMakeGray(imImage *image)
{
  assert(image);

  imbyte *map = (imbyte*)image->data[0];
  for(int i = 0; i < image->count; i++)
  {
    if (*map)
      *map = 255;
    map++;
  }
}

static void iLoadImageData(imFile* ifile, imImage* image, int *error, int bitmap)
{
  iAttributeTableCopy(ifile->attrib_table, image->attrib_table);
  *error = imFileReadImageData(ifile, image->data[0], bitmap, image->has_alpha);
  if (image->color_space == IM_MAP)
    imFileGetPalette(ifile, image->palette, &image->palette_count);
}

imImage* imFileLoadImage(imFile* ifile, int index, int *error)
{
  assert(ifile);

  int width, height, color_mode, data_type;
  *error = imFileReadImageInfo(ifile, index, &width, &height, &color_mode, &data_type);
  if (*error) return NULL; 
  
  imImage* image = imImageCreate(width, height, imColorModeSpace(color_mode), data_type);
  if (!image) 
  {
    *error = IM_ERR_MEM;
    return NULL;
  }

  if (imColorModeHasAlpha(color_mode))
    imImageAddAlpha(image);

  iLoadImageData(ifile, image, error, 0);

  return image;
}

void imFileLoadImageFrame(imFile* ifile, int index, imImage* image, int *error)
{
  assert(ifile);

  int width, height, color_mode, data_type;
  *error = imFileReadImageInfo(ifile, index, &width, &height, &color_mode, &data_type);
  if (*error) return; 
  
  // check if we can reuse the data
  if (image->width != width || 
      image->height != height ||
      image->depth != imColorModeDepth(imColorModeSpace(color_mode)) ||
      image->has_alpha != imColorModeHasAlpha(color_mode) ||
      image->data_type != data_type) 
  {
    *error = IM_ERR_DATA;
    return;
  }

  image->color_space = imColorModeSpace(color_mode);
  iLoadImageData(ifile, image, error, 0);
}

imImage* imFileLoadBitmap(imFile* ifile, int index, int *error)
{
  assert(ifile);

  int width, height, color_mode, data_type;
  *error = imFileReadImageInfo(ifile, index, &width, &height, &color_mode, &data_type);
  if (*error) return NULL; 
  
  imImage* image = imImageCreate(width, height, imColorModeToBitmap(color_mode), IM_BYTE);
  if (!image) 
  {
    *error = IM_ERR_MEM;
    return NULL;
  }

  if (imColorModeHasAlpha(color_mode))
    imImageAddAlpha(image);

  iLoadImageData(ifile, image, error, 1);

  return image;
}

void imFileLoadBitmapFrame(imFile* ifile, int index, imImage* image, int *error)
{
  assert(ifile);

  int width, height, color_mode, data_type;
  *error = imFileReadImageInfo(ifile, index, &width, &height, &color_mode, &data_type);
  if (*error) return; 
  
  // check if we can reuse the data
  if (image->width != width || 
      image->height != height ||
      image->depth != imColorModeDepth(imColorModeToBitmap(color_mode)) ||
      image->has_alpha != imColorModeHasAlpha(color_mode) ||
      image->data_type != IM_BYTE) 
  {
    *error = IM_ERR_DATA;
    return;
  }

  image->color_space = imColorModeToBitmap(color_mode);
  iLoadImageData(ifile, image, error, 1);
}

imImage* imFileLoadImageRegion(imFile* ifile, int index, int bitmap, int *error, 
                          int xmin, int xmax, int ymin, int ymax, int width, int height)
{
  assert(ifile);

  int color_mode, data_type;
  *error = imFileReadImageInfo(ifile, index, NULL, NULL, &color_mode, &data_type);
  if (*error) return NULL; 
  
  imImage* image = imImageCreate(width, height, 
                                 bitmap? imColorModeToBitmap(color_mode): imColorModeSpace(color_mode), 
                                 bitmap? IM_BYTE: data_type);
  if (!image) 
  {
    *error = IM_ERR_MEM;
    return NULL;
  }

  if (imColorModeHasAlpha(color_mode))
    imImageAddAlpha(image);

  imFileSetAttribute(ifile, "ViewXmin", IM_INT, 1, &xmin);
  imFileSetAttribute(ifile, "ViewXmax", IM_INT, 1, &xmax);
  imFileSetAttribute(ifile, "ViewYmin", IM_INT, 1, &ymin);
  imFileSetAttribute(ifile, "ViewYmax", IM_INT, 1, &ymax);
  imFileSetAttribute(ifile, "ViewWidth", IM_INT, 1, &width);
  imFileSetAttribute(ifile, "ViewHeight", IM_INT, 1, &height);

  iLoadImageData(ifile, image, error, bitmap);

  return image;
}

int imFileSaveImage(imFile* ifile, const imImage* image)
{
  assert(ifile);
  assert(image);

  if (image->color_space == IM_MAP)
    imFileSetPalette(ifile, image->palette, image->palette_count);

  iAttributeTableCopy(image->attrib_table, ifile->attrib_table);

  int color_mode = image->color_space;
  if (image->has_alpha)
    color_mode |= IM_ALPHA;

  int error = imFileWriteImageInfo(ifile, image->width, image->height, color_mode, image->data_type);
  if (error) return error;
  
  return imFileWriteImageData(ifile, image->data[0]);
}

imImage* imFileImageLoad(const char* file_name, int index, int *error)
{
  imFile* ifile = imFileOpen(file_name, error);
  if (!ifile) return NULL;
  imImage* image = imFileLoadImage(ifile, index, error);
  imFileClose(ifile);
  return image;
}

imImage* imFileImageLoadBitmap(const char* file_name, int index, int *error)
{
  imFile* ifile = imFileOpen(file_name, error);
  if (!ifile) return NULL;
  imImage* image = imFileLoadBitmap(ifile, index, error);
  imFileClose(ifile);
  return image;
}

imImage* imFileImageLoadRegion(const char* file_name, int index, int bitmap, int *error, 
                               int xmin, int xmax, int ymin, int ymax, int width, int height)
{
  imFile* ifile = imFileOpen(file_name, error);
  if (!ifile) return NULL;
  imImage* image = imFileLoadImageRegion(ifile, index, bitmap, error, xmin, xmax, ymin, ymax, width, height);
  imFileClose(ifile);
  return image;
}

int imFileImageSave(const char* file_name, const char* format, const imImage* image)
{
  int error;
  imFile* ifile = imFileNew(file_name, format, &error);
  if (!ifile) return error;
  error = imFileSaveImage(ifile, image);
  imFileClose(ifile);
  return error;
}