summaryrefslogtreecommitdiff
path: root/src/im_convertbitmap.cpp
blob: c9570d7afbdf15a613434ba08f1adeaffde118ba (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
/** \file
 * \brief Image Conversion
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_convertbitmap.cpp,v 1.1 2008/10/17 06:10:16 scuri Exp $
 */

#include "im.h"
#include "im_util.h"
#include "im_complex.h"
#include "im_image.h"
#include "im_convert.h"
#include "im_counter.h"

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

int imConvertToBitmap(const imImage* src_image, imImage* dst_image, int cpx2real, float gamma, int abssolute, int cast_mode)
{
  assert(src_image);
  assert(dst_image);

  if (!imImageMatchSize(src_image, dst_image) || !imImageIsBitmap(dst_image))
    return IM_ERR_DATA;

  int counter = imCounterBegin("Building Bitmap");

  int ret;
  if (src_image->data_type == IM_BYTE)
  {
    // NO data type conversion, only color mode conversion
    ret = imConvertColorSpace(src_image, dst_image);
  }
  else
  {
    if (src_image->color_space == IM_RGB || 
        src_image->color_space == IM_GRAY)
    {
      // data type conversion, but NO color mode conversion
      ret = imConvertDataType(src_image, dst_image, cpx2real, gamma, abssolute, cast_mode);
    }
    else
    {
      // data type conversion AND color mode conversion
      imImage* temp_image = imImageCreate(src_image->width, src_image->height, dst_image->color_space, src_image->data_type);
      if (!temp_image)
        ret = IM_ERR_MEM;
      else
      {
        // first convert color_mode in the bigger precision
        ret = imConvertColorSpace(src_image, temp_image);
        if (ret == IM_ERR_NONE)
        {
          // second just convert data type
          ret = imConvertDataType(temp_image, dst_image, cpx2real, gamma, abssolute, cast_mode);
        }
        imImageDestroy(temp_image);
      }
    }
  }

  imCounterEnd(counter);
  return ret;
}


template <class T>
void iDoChangePacking(const T* src_data, T* dst_data, int width, int height, int depth, 
                             int src_is_packed)
{
  int count = width*height;
  if (src_is_packed)
  {
    for (int i = 0; i < count; i++)
    {
      for (int d = 0; d < depth; d++)
      {
        *(dst_data + d*count) = *src_data++;
      }

      dst_data++;
    }
  }
  else
  {
    for (int i = 0; i < count; i++)
    {
      for (int d = 0; d < depth; d++)
      {
        *dst_data++ = *(src_data + d*count);
      }

      src_data++;
    }
  }
}

void imConvertPacking(const void* src_data, void* dst_data, int width, int height, int depth, 
                      int data_type, int src_is_packed)
{
  switch(data_type)
  {
  case IM_BYTE:
    iDoChangePacking((const imbyte*)src_data, (imbyte*)dst_data, width, height, depth, src_is_packed); 
    break;
  case IM_USHORT:
    iDoChangePacking((const imushort*)src_data, (imushort*)dst_data, width, height, depth, src_is_packed); 
    break;
  case IM_INT:
    iDoChangePacking((const int*)src_data, (int*)dst_data, width, height, depth, src_is_packed); 
    break;
  case IM_FLOAT:
    iDoChangePacking((const float*)src_data, (float*)dst_data, width, height, depth, src_is_packed); 
    break;
  case IM_CFLOAT:
    iDoChangePacking((const imcfloat*)src_data, (imcfloat*)dst_data, width, height, depth, src_is_packed); 
    break;
  }
}