jdk/src/share/native/sun/awt/medialib/mlib_ImageCreate.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * FUNCTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *      mlib_ImageCreateStruct   - create image data structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *      mlib_ImageCreate         - create image data structure and allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *                                 memory for image data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *      mlib_ImageDelete         - delete image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *      mlib_ImageCreateSubimage - create sub-image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *      mlib_ImageCreateRowTable - create row starts pointer table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *      mlib_ImageDeleteRowTable - delete row starts pointer table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *      mlib_ImageSetPaddings    - set paddings for clipping box borders
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *      mlib_ImageSetFormat      - set image format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * SYNOPSIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *        mlib_image *mlib_ImageCreateStruct(mlib_type  type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *                                           mlib_s32   channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *                                           mlib_s32   width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *                                           mlib_s32   height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *                                           mlib_s32   stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *                                           const void *data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *        mlib_image *mlib_ImageCreate(mlib_type type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *                                     mlib_s32  channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *                                     mlib_s32  width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *                                     mlib_s32  height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *        void mlib_ImageDelete(mlib_image *img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *        mlib_image *mlib_ImageCreateSubimage(mlib_image *img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *                                             mlib_s32   x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *                                             mlib_s32   y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *                                             mlib_s32   w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *                                             mlib_s32   h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *        void *mlib_ImageCreateRowTable(mlib_image *img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *        void mlib_ImageDeleteRowTable(mlib_image *img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *        mlib_status mlib_ImageSetPaddings(mlib_image *img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *                                          mlib_u8    left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *                                          mlib_u8    top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *                                          mlib_u8    right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *                                          mlib_u8    bottom)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *        mlib_status mlib_ImageSetFormat(mlib_image  *img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *                                        mlib_format format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * ARGUMENTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      img       pointer to image data structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      type      image data type, one of MLIB_BIT, MLIB_BYTE, MLIB_SHORT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *                MLIB_USHORT, MLIB_INT, MLIB_FLOAT or MLIB_DOUBLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      channels  number of image channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      width     image width in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      height    image height in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *      stride    linebytes( bytes to next row) of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      data      pointer to image data allocated by user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      x         x coordinate of the left border in the source image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *      y         y coordinate of the top border in the source image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *      w         width of the sub-image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *      h         height of the sub-image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *      left      clipping box left padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *      top       clipping box top padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *      right     clipping box right padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *      bottom    clipping box bottom padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *      format    image format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * DESCRIPTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *      mlib_ImageCreateStruct() creates a mediaLib image data structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *      using parameter supplied by user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *      mlib_ImageCreate() creates a mediaLib image data structure and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *      allocates memory space for image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *      mlib_ImageDelete() deletes the mediaLib image data structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      and frees the memory space of the image data if it is allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *      through mlib_ImageCreate().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *      mlib_ImageCreateSubimage() creates a mediaLib image structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *      for a sub-image based on a source image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *      mlib_ImageCreateRowTable() creates row starts pointer table and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *      puts it into mlib_image->state field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *      mlib_ImageDeleteRowTable() deletes row starts pointer table from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *      image and puts NULL into mlib_image->state field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *      mlib_ImageSetPaddings() sets new values for the clipping box paddings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *      mlib_ImageSetFormat() sets new value for the image format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
#include "mlib_image.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
#include "mlib_ImageRowTable.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
#include "mlib_ImageCreate.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
mlib_image* mlib_ImageSet(mlib_image *image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                          mlib_type  type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                          mlib_s32   channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                          mlib_s32   width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                          mlib_s32   height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                          mlib_s32   stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                          const void *data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  mlib_s32        wb;                /* width in bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
  mlib_s32        mask;              /* mask for check of stride */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  if (image == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
/* for some ugly functions calling with incorrect parameters */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
  image -> type     = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  image -> channels = channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
  image -> width    = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
  image -> height   = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
  image -> stride   = stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
  image -> data     = (void *)data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
  image -> state    = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
  image -> format   = MLIB_FORMAT_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
  image -> paddings[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
  image -> paddings[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
  image -> paddings[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
  image -> paddings[3] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
  image -> bitoffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
  if (width <= 0 || height <= 0 || channels < 1 || channels > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
/* Check if stride == width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
   * If it is then image can be treated as a 1-D vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
  switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    case MLIB_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      wb = width * channels * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
      mask = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    case MLIB_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    case MLIB_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
      wb = width * channels * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
      mask = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    case MLIB_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
      wb = width * channels * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
      mask = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
      wb = width * channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
      mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    case MLIB_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
      wb = (width * channels + 7) / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
      mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
  if (stride & mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
  image -> flags    = ((width & 0xf) << 8);          /* set width field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
  image -> flags   |= ((stride & 0xf) << 16);        /* set stride field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
  image -> flags   |= ((height & 0xf) << 12);        /* set height field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
  image -> flags   |= (mlib_addr)data & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
  image -> flags   |= MLIB_IMAGE_USERALLOCATED;      /* user allocated data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
  if ((stride != wb) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
      ((type == MLIB_BIT) && (stride * 8 != width * channels))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    image -> flags |= MLIB_IMAGE_ONEDVECTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
  image -> flags &= MLIB_IMAGE_ATTRIBUTESET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
  return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
mlib_image *mlib_ImageCreateStruct(mlib_type  type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                   mlib_s32   channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                   mlib_s32   width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                   mlib_s32   height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                   mlib_s32   stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                   const void *data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
  mlib_image *image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
  if (stride <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
  image = (mlib_image *)mlib_malloc(sizeof(mlib_image));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
  if (image == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
  if (mlib_ImageSet(image, type, channels, width, height, stride, data) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    mlib_free(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    image = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
  return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
mlib_image *mlib_ImageCreate(mlib_type type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                             mlib_s32  channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                             mlib_s32  width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                             mlib_s32  height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
  mlib_image *image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
  mlib_s32        wb;                /* width in bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
  void       *data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
/* sanity check */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
  if (width <= 0 || height <= 0 || channels < 1 || channels > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
  switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    case MLIB_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
      wb = width * channels * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    case MLIB_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    case MLIB_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
      wb = width * channels * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    case MLIB_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
      wb = width * channels * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
      wb = width * channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    case MLIB_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
      wb = (width * channels + 7) / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
  data = mlib_malloc(wb * height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
  if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
  image = (mlib_image *)mlib_malloc(sizeof(mlib_image));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
  if (image == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    mlib_free(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
  image -> type     = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
  image -> channels = channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
  image -> width    = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
  image -> height   = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
  image -> stride   = wb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
  image -> data     = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
  image -> flags    = ((width & 0xf) << 8);        /* set width field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
  image -> flags   |= ((height & 0xf) << 12);      /* set height field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
  image -> flags   |= ((wb & 0xf) << 16);          /* set stride field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
  image -> flags   |= (mlib_addr)data & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
  image -> format   = MLIB_FORMAT_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
  image -> paddings[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
  image -> paddings[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
  image -> paddings[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
  image -> paddings[3] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
  image -> bitoffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
  if ((type == MLIB_BIT) && (wb * 8 != width * channels)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    image -> flags |= MLIB_IMAGE_ONEDVECTOR;       /* not 1-d vector */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
  image -> flags &= MLIB_IMAGE_ATTRIBUTESET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
  image -> state  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
  return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
void mlib_ImageDelete(mlib_image *img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
  if (img == NULL) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
  if ((img -> flags & MLIB_IMAGE_USERALLOCATED) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    mlib_free(img -> data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
  mlib_ImageDeleteRowTable(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
  mlib_free(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
mlib_image *mlib_ImageCreateSubimage(mlib_image *img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                     mlib_s32   x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                     mlib_s32   y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                     mlib_s32   w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                     mlib_s32   h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
  mlib_image     *subimage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
  mlib_type      type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
  mlib_s32       channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
  mlib_s32       width;                 /* for parent image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
  mlib_s32       height;                /* for parent image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
  mlib_s32       stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
  mlib_s32       bitoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
  void           *data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
/* sanity check */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
  if (w <= 0 || h <= 0 || img == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
  type     = img -> type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
  channels = img -> channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
  width    = img -> width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
  height   = img -> height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
  stride   = img -> stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
/* clip the sub-image with respect to the parent image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
  if (((x + w) <= 0) || ((y + h) <= 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
      (x >= width) || (y >= height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    if (x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
      w += x;                                        /* x is negative */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
      x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    if (y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
      h += y;                                        /* y is negative */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
      y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    if ((x + w) > width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
      w = width - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    if ((y + h) > height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
      h = height - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
/* compute sub-image origin */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
  data = (mlib_u8 *)(img -> data) + y * stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
  switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    case MLIB_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
      data = (mlib_u8 *)data + x * channels * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    case MLIB_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    case MLIB_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
      data = (mlib_u8 *)data + x * channels * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    case MLIB_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
      data = (mlib_u8 *)data + x * channels * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
      data = (mlib_u8 *)data + x * channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    case MLIB_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
      bitoffset = img -> bitoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
      data = (mlib_u8 *)data + (x * channels + bitoffset) / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
      bitoffset = (x * channels + bitoffset) & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
  subimage = mlib_ImageCreateStruct(type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                    channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                    w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                    h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                    stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                    data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
  if (subimage != NULL && type == MLIB_BIT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    subimage -> bitoffset = bitoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
  return subimage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
mlib_image *mlib_ImageSetSubimage(mlib_image       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                  const mlib_image *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                  mlib_s32         x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                  mlib_s32         y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                  mlib_s32         w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                  mlib_s32         h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
  mlib_type  type     = src -> type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
  mlib_s32   channels = src -> channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
  mlib_s32   stride   = src -> stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
  mlib_u8    *data    = src -> data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
  mlib_s32   bitoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
  data += y * stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
  switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    case MLIB_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
      data += channels * x * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    case MLIB_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    case MLIB_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
      data += channels * x * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    case MLIB_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
      data += channels * x * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
      data += channels * x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    case MLIB_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
      bitoffset = src -> bitoffset + channels * x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
      data += (bitoffset >= 0) ? bitoffset/8 : (bitoffset - 7)/8; /* with rounding toward -Inf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
      bitoffset &= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
  if (h > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    dst = mlib_ImageSet(dst, type, channels, w, h, stride, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    h = - h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    dst = mlib_ImageSet(dst, type, channels, w, h, - stride, data + (h - 1)*stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
  if (dst != NULL && type == MLIB_BIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    dst -> bitoffset = bitoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
  return dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
void *mlib_ImageCreateRowTable(mlib_image *img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
  mlib_u8  **rtable, *tline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
  mlib_s32 i, im_height, im_stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
  if (img == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
  if (img -> state)  return img -> state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
  im_height = mlib_ImageGetHeight(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
  im_stride = mlib_ImageGetStride(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
  tline     = mlib_ImageGetData(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
  rtable    = mlib_malloc((3 + im_height)*sizeof(mlib_u8 *));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
  if (rtable == NULL || tline == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
  rtable[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
  rtable[1] = (mlib_u8*)((void **)rtable + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
  rtable[2 + im_height] = (mlib_u8*)((void **)rtable + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
  for (i = 0; i < im_height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    rtable[i+2] = tline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    tline    += im_stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
  img -> state = ((void **)rtable + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
  return img -> state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
void mlib_ImageDeleteRowTable(mlib_image *img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
  void **state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
  if (img == NULL) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
  state = img -> state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
  if (!state) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
  mlib_free(state - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
  img -> state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
mlib_status mlib_ImageSetPaddings(mlib_image *img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                  mlib_u8    left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                  mlib_u8    top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                  mlib_u8    right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                  mlib_u8    bottom)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
  if (img == NULL) return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
  if ((left + right) >= img -> width ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
      (top + bottom) >= img -> height) return MLIB_OUTOFRANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
  img -> paddings[0] = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
  img -> paddings[1] = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
  img -> paddings[2] = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
  img -> paddings[3] = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
  return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
mlib_status mlib_ImageSetFormat(mlib_image  *img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                mlib_format format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
  if (img == NULL) return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
  img -> format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
  return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
/***************************************************************/