Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rle_raw_alc.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  *
18  * Modified at BRL 16-May-88 by Mike Muuss to avoid Alliant STDC desire
19  * to have all "void" functions so declared.
20  */
21 /*
22  * rle_raw_alc.c - Allocate buffers for rle_getraw/rle_putraw.
23  *
24  * Author: Spencer W. Thomas
25  * Computer Science Dept.
26  * University of Utah
27  * Date: Fri Nov 14 1986
28  * Copyright (c) 1986, Spencer W. Thomas
29  */
30 
31 #include <stdio.h>
32 #include "rle.h"
33 #include "rle_raw.h"
34 
35 /*****************************************************************
36  * TAG( rle_raw_alloc )
37  *
38  * Allocate buffer space for use by rle_getraw and rle_putraw.
39  * Inputs:
40  * the_hdr: Header structure for RLE file to be read or
41  * written.
42  * Outputs:
43  * scanp: Pointer to pointer to created opcode buffer.
44  * nrawp: Pointer to pointer to created length buffer.
45  * These pointers are adjusted for the alpha channel,
46  * if present.
47  * Returns 0 for success, -1 if malloc failed.
48  * Assumptions:
49  * Since buffers are built to have as many slots as there are pixels
50  * in the input scanline, it is assumed that no input scanline will
51  * have more data elements than this.
52  * Algorithm:
53  * Count number of channels actually used (check bitmap).
54  * Allocate nchan*rowlength elements, allocate a buffer
55  * to hold (ncolors+alpha) pointers.
56  * Also allocate a buffer of ncolors+alpha
57  * integers for the length buffer.
58  */
59 int
61 rle_hdr *the_hdr;
62 rle_op ***scanp;
63 int **nrawp;
64 {
65  rle_op ** scanbuf, * opbuf;
66  int rowlen, nchan = 0, i, ncol;
67 
68  rowlen = the_hdr->xmax - the_hdr->xmin + 1;
69  if ( the_hdr->alpha && RLE_BIT( *the_hdr, RLE_ALPHA ) )
70  nchan++;
71  for ( i = 0; i < the_hdr->ncolors; i++ )
72  if ( RLE_BIT( *the_hdr, i ) )
73  nchan++;
74 
75  ncol = the_hdr->ncolors + the_hdr->alpha;
76 
77  if ( (scanbuf = (rle_op **) malloc( ncol * sizeof(rle_op *) )) == 0 )
78  return -1;
79 
80  if ( (opbuf = (rle_op *)malloc( nchan * rowlen * sizeof(rle_op) )) == 0 )
81  {
82  free( scanbuf );
83  return -1;
84  }
85 
86  if ( nrawp && (*nrawp = (int *)malloc( ncol * sizeof(int) )) == 0 )
87  {
88  free( scanbuf );
89  free( opbuf );
90  return -1;
91  }
92 
93  if ( the_hdr->alpha )
94  {
95  scanbuf++;
96  if ( nrawp )
97  (*nrawp)++;
98  }
99 
100  for ( i = -the_hdr->alpha; i < the_hdr->ncolors; i++ )
101  if ( RLE_BIT( *the_hdr, i ) )
102  {
103  scanbuf[i] = opbuf;
104  opbuf += rowlen;
105  }
106  else
107  scanbuf[i] = 0;
108  *scanp = scanbuf;
109 
110  return 0;
111 }
112 
113 
114 /*****************************************************************
115  * TAG( rle_raw_free )
116  *
117  * Free storage allocated by rle_raw_alloc().
118  * Inputs:
119  * the_hdr: Header structure as above.
120  * scanp: Pointer to scanbuf above.
121  * nrawp: Pointer to length buffer.
122  * Outputs:
123  * Frees storage referenced by scanp and nrawp.
124  * Assumptions:
125  * Storage was allocated by rle_raw_alloc, or by use of same
126  * algorithm, at least.
127  * Algorithm:
128  * free scanp[0], scanp, and nrawp.
129  */
130 void
132 rle_hdr *the_hdr;
133 rle_op **scanp;
134 int *nrawp ;
135 {
136  int i;
137 
138  if ( the_hdr->alpha )
139  {
140  scanp--;
141  if ( nrawp )
142  nrawp--;
143  }
144  for ( i = 0; i < the_hdr->ncolors + the_hdr->alpha; i++ )
145  if ( scanp[i] != 0 )
146  {
147  free( (char *)scanp[i] );
148  break;
149  }
150  free( (char *)scanp );
151  if ( nrawp )
152  free( (char *)nrawp );
153 }
int xmin
Definition: rle.h:100
int rle_raw_alloc(rle_hdr *the_hdr, rle_op ***scanp, int **nrawp)
Definition: rle_raw_alc.c:60
int xmax
Definition: rle.h:100
int alpha
Definition: rle.h:100
#define RLE_ALPHA
Definition: rle.h:65
void rle_raw_free(rle_hdr *the_hdr, rle_op **scanp, nrawp)
Definition: rle_raw_alc.c:131
int ncolors
Definition: rle.h:100
#define RLE_BIT(glob, bit)
Definition: rle.h:126