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