jdk/src/share/native/sun/awt/medialib/mlib_ImageCreate.c
author bae
Wed, 10 Apr 2013 15:55:07 +0400
changeset 18228 f14a008f78be
parent 16868 b5e2827ecc50
child 23010 6dadb192ad81
permissions -rw-r--r--
8011243: Improve ImagingLib Reviewed-by: prr, vadim
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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"
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   123
#include "safe_math.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
mlib_image* mlib_ImageSet(mlib_image *image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                          mlib_type  type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                          mlib_s32   channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                          mlib_s32   width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                          mlib_s32   height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                          mlib_s32   stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                          const void *data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
  mlib_s32        wb;                /* width in bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  mlib_s32        mask;              /* mask for check of stride */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
  if (image == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
/* for some ugly functions calling with incorrect parameters */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  image -> type     = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
  image -> channels = channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
  image -> width    = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
  image -> height   = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
  image -> stride   = stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
  image -> data     = (void *)data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
  image -> state    = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
  image -> format   = MLIB_FORMAT_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
  image -> paddings[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
  image -> paddings[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
  image -> paddings[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
  image -> paddings[3] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
  image -> bitoffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
  if (width <= 0 || height <= 0 || channels < 1 || channels > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
/* Check if stride == width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
   * If it is then image can be treated as a 1-D vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 */
18228
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   163
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   164
  if (!SAFE_TO_MULT(width, channels)) {
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   165
    return NULL;
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   166
  }
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   167
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   168
  wb = width * channels;
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   169
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
  switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    case MLIB_DOUBLE:
18228
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   172
      if (!SAFE_TO_MULT(wb, 8)) {
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   173
        return NULL;
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   174
      }
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   175
      wb *= 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
      mask = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    case MLIB_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    case MLIB_INT:
18228
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   180
      if (!SAFE_TO_MULT(wb, 4)) {
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   181
        return NULL;
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   182
      }
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   183
      wb *= 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
      mask = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    case MLIB_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    case MLIB_SHORT:
18228
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   188
      if (!SAFE_TO_MULT(wb, 2)) {
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   189
        return NULL;
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   190
      }
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   191
      wb *= 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
      mask = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    case MLIB_BYTE:
18228
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   195
      // wb is ready
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
      mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    case MLIB_BIT:
18228
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   199
      if (!SAFE_TO_ADD(7, wb)) {
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   200
        return NULL;
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   201
      }
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   202
      wb = (wb + 7) / 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
      mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
  if (stride & mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
  image -> flags    = ((width & 0xf) << 8);          /* set width field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
  image -> flags   |= ((stride & 0xf) << 16);        /* set stride field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
  image -> flags   |= ((height & 0xf) << 12);        /* set height field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
  image -> flags   |= (mlib_addr)data & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
  image -> flags   |= MLIB_IMAGE_USERALLOCATED;      /* user allocated data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
  if ((stride != wb) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
      ((type == MLIB_BIT) && (stride * 8 != width * channels))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    image -> flags |= MLIB_IMAGE_ONEDVECTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
  image -> flags &= MLIB_IMAGE_ATTRIBUTESET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
  return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
mlib_image *mlib_ImageCreateStruct(mlib_type  type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                   mlib_s32   channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                   mlib_s32   width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                   mlib_s32   height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                   mlib_s32   stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                   const void *data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
  mlib_image *image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
  if (stride <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
  image = (mlib_image *)mlib_malloc(sizeof(mlib_image));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
  if (image == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
  if (mlib_ImageSet(image, type, channels, width, height, stride, data) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    mlib_free(image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    image = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
  return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
mlib_image *mlib_ImageCreate(mlib_type type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                             mlib_s32  channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                             mlib_s32  width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                             mlib_s32  height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
  mlib_image *image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
  mlib_s32        wb;                /* width in bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
  void       *data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
/* sanity check */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
  if (width <= 0 || height <= 0 || channels < 1 || channels > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   270
  if (!SAFE_TO_MULT(width, channels)) {
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   271
    return NULL;
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   272
  }
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   273
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   274
  wb = width * channels;
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   275
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
  switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    case MLIB_DOUBLE:
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   278
      if (!SAFE_TO_MULT(wb, 8)) {
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   279
        return NULL;
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   280
      }
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   281
      wb *= 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    case MLIB_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    case MLIB_INT:
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   285
      if (!SAFE_TO_MULT(wb, 4)) {
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   286
        return NULL;
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   287
      }
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   288
      wb *= 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    case MLIB_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    case MLIB_SHORT:
18228
f14a008f78be 8011243: Improve ImagingLib
bae
parents: 16868
diff changeset
   292
      if (!SAFE_TO_MULT(wb, 2)) {
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   293
        return NULL;
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   294
      }
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   295
      wb *= 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    case MLIB_BYTE:
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   298
      // wb is ready
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    case MLIB_BIT:
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   301
      if (!SAFE_TO_ADD(7, wb)) {
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   302
        return NULL;
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   303
      }
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   304
      wb = (wb + 7) / 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
16868
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   310
  if (!SAFE_TO_MULT(wb, height)) {
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   311
      return NULL;
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   312
  }
b5e2827ecc50 8007014: Improve image handling
bae
parents: 15628
diff changeset
   313
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
  data = mlib_malloc(wb * height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
  if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
  image = (mlib_image *)mlib_malloc(sizeof(mlib_image));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
  if (image == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    mlib_free(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
  image -> type     = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
  image -> channels = channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
  image -> width    = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
  image -> height   = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
  image -> stride   = wb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
  image -> data     = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
  image -> flags    = ((width & 0xf) << 8);        /* set width field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
  image -> flags   |= ((height & 0xf) << 12);      /* set height field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
  image -> flags   |= ((wb & 0xf) << 16);          /* set stride field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
  image -> flags   |= (mlib_addr)data & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
  image -> format   = MLIB_FORMAT_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
  image -> paddings[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
  image -> paddings[1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
  image -> paddings[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
  image -> paddings[3] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
  image -> bitoffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
  if ((type == MLIB_BIT) && (wb * 8 != width * channels)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    image -> flags |= MLIB_IMAGE_ONEDVECTOR;       /* not 1-d vector */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
  image -> flags &= MLIB_IMAGE_ATTRIBUTESET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
  image -> state  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
  return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
void mlib_ImageDelete(mlib_image *img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
  if (img == NULL) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
  if ((img -> flags & MLIB_IMAGE_USERALLOCATED) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    mlib_free(img -> data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
  mlib_ImageDeleteRowTable(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
  mlib_free(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
mlib_image *mlib_ImageCreateSubimage(mlib_image *img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                     mlib_s32   x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                     mlib_s32   y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                     mlib_s32   w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                     mlib_s32   h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
  mlib_image     *subimage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
  mlib_type      type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
  mlib_s32       channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
  mlib_s32       width;                 /* for parent image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
  mlib_s32       height;                /* for parent image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
  mlib_s32       stride;
8939
04615dca2a76 6989717: media native code compiler warnings
bae
parents: 5506
diff changeset
   379
  mlib_s32       bitoffset = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
  void           *data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
/* sanity check */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
  if (w <= 0 || h <= 0 || img == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
  type     = img -> type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
  channels = img -> channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
  width    = img -> width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
  height   = img -> height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
  stride   = img -> stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
/* clip the sub-image with respect to the parent image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
  if (((x + w) <= 0) || ((y + h) <= 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
      (x >= width) || (y >= height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    if (x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
      w += x;                                        /* x is negative */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
      x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    if (y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
      h += y;                                        /* y is negative */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
      y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    if ((x + w) > width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
      w = width - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    if ((y + h) > height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
      h = height - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
/* compute sub-image origin */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
  data = (mlib_u8 *)(img -> data) + y * stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
  switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    case MLIB_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      data = (mlib_u8 *)data + x * channels * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    case MLIB_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    case MLIB_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
      data = (mlib_u8 *)data + x * channels * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    case MLIB_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
      data = (mlib_u8 *)data + x * channels * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
      data = (mlib_u8 *)data + x * channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    case MLIB_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
      bitoffset = img -> bitoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
      data = (mlib_u8 *)data + (x * channels + bitoffset) / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
      bitoffset = (x * channels + bitoffset) & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
  subimage = mlib_ImageCreateStruct(type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                                    channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                                    w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                    h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                                    stride,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                    data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
  if (subimage != NULL && type == MLIB_BIT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    subimage -> bitoffset = bitoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
  return subimage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
mlib_image *mlib_ImageSetSubimage(mlib_image       *dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                  const mlib_image *src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                  mlib_s32         x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                  mlib_s32         y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                  mlib_s32         w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                  mlib_s32         h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
  mlib_type  type     = src -> type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
  mlib_s32   channels = src -> channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
  mlib_s32   stride   = src -> stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
  mlib_u8    *data    = src -> data;
8939
04615dca2a76 6989717: media native code compiler warnings
bae
parents: 5506
diff changeset
   468
  mlib_s32   bitoffset = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
  data += y * stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
  switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    case MLIB_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
      data += channels * x * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    case MLIB_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    case MLIB_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
      data += channels * x * 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    case MLIB_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    case MLIB_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
      data += channels * x * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    case MLIB_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
      data += channels * x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    case MLIB_BIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
      bitoffset = src -> bitoffset + channels * x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
      data += (bitoffset >= 0) ? bitoffset/8 : (bitoffset - 7)/8; /* with rounding toward -Inf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
      bitoffset &= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
  if (h > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    dst = mlib_ImageSet(dst, type, channels, w, h, stride, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    h = - h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    dst = mlib_ImageSet(dst, type, channels, w, h, - stride, data + (h - 1)*stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
  if (dst != NULL && type == MLIB_BIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    dst -> bitoffset = bitoffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
  return dst;
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
void *mlib_ImageCreateRowTable(mlib_image *img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
  mlib_u8  **rtable, *tline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
  mlib_s32 i, im_height, im_stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
  if (img == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
  if (img -> state)  return img -> state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
  im_height = mlib_ImageGetHeight(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
  im_stride = mlib_ImageGetStride(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
  tline     = mlib_ImageGetData(img);
15628
228422512f97 8005129: [parfait] #1122 - #1130 native/sun/awt/medialib/mlib_Image*.c Memory leak of pointer 'k' allocated with mlib_malloc
jgodinez
parents: 8939
diff changeset
   522
  if (tline == NULL) return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
  rtable    = mlib_malloc((3 + im_height)*sizeof(mlib_u8 *));
15628
228422512f97 8005129: [parfait] #1122 - #1130 native/sun/awt/medialib/mlib_Image*.c Memory leak of pointer 'k' allocated with mlib_malloc
jgodinez
parents: 8939
diff changeset
   524
  if (rtable == NULL) return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
  rtable[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
  rtable[1] = (mlib_u8*)((void **)rtable + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
  rtable[2 + im_height] = (mlib_u8*)((void **)rtable + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
  for (i = 0; i < im_height; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    rtable[i+2] = tline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    tline    += im_stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
  img -> state = ((void **)rtable + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
  return img -> state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
void mlib_ImageDeleteRowTable(mlib_image *img)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
  void **state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
  if (img == NULL) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
  state = img -> state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
  if (!state) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
  mlib_free(state - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
  img -> state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
mlib_status mlib_ImageSetPaddings(mlib_image *img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                  mlib_u8    left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                  mlib_u8    top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                                  mlib_u8    right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                  mlib_u8    bottom)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
  if (img == NULL) return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
  if ((left + right) >= img -> width ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
      (top + bottom) >= img -> height) return MLIB_OUTOFRANGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
  img -> paddings[0] = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
  img -> paddings[1] = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
  img -> paddings[2] = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
  img -> paddings[3] = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
  return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
/***************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
mlib_status mlib_ImageSetFormat(mlib_image  *img,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                mlib_format format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
  if (img == NULL) return MLIB_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
  img -> format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
  return MLIB_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
/***************************************************************/