Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rle_row_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_row_alc.c - Allocate buffers for rle_getrow/rle_putrow.
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 
34 /*****************************************************************
35  * TAG( rle_row_alloc )
36  *
37  * Allocate buffer space for use by rle_getrow and rle_putrow.
38  * Inputs:
39  * the_hdr: Header structure for RLE file to be read or
40  * written.
41  * Outputs:
42  * scanp: Pointer to pointer to created scanline buffer.
43  * This pointer is adjusted for the alpha channel,
44  * if present.
45  * Returns 0 for success, -1 if malloc failed.
46  * Assumptions:
47  * No input scanline will extend beyond the declared xmax endpoint.
48  * Algorithm:
49  * Count number of channels actually used (check bitmap).
50  * Allocate nchan*rowlength pixels, allocate a buffer
51  * to hold ncolors+alpha pointers, and give each channel
52  * rowlength pixels. Rowlength is xmax + 1, to allow for rle_getrow
53  * usage.
54  */
55 int
57 rle_hdr *the_hdr;
58 rle_pixel ***scanp;
59 {
60  rle_pixel ** scanbuf, * pixbuf;
61  int rowlen, nchan = 0, i, ncol;
62 
63  rowlen = the_hdr->xmax + 1;
64  if ( the_hdr->alpha && RLE_BIT( *the_hdr, RLE_ALPHA ) )
65  nchan++;
66  for ( i = 0; i < the_hdr->ncolors; i++ )
67  if ( RLE_BIT( *the_hdr, i ) )
68  nchan++;
69 
70  ncol = the_hdr->ncolors + the_hdr->alpha;
71 
72  if ( (scanbuf = (rle_pixel **)malloc( ncol * sizeof(rle_pixel *) )) == 0 )
73  return -1;
74  if ( (pixbuf = (rle_pixel *)malloc( nchan * rowlen *
75  sizeof(rle_pixel) )) == 0 )
76  {
77  free( scanbuf );
78  return -1;
79  }
80 
81  if ( the_hdr->alpha )
82  scanbuf++;
83 
84  for ( i = -the_hdr->alpha; i < the_hdr->ncolors; i++ )
85  if ( RLE_BIT( *the_hdr, i ) )
86  {
87  scanbuf[i] = pixbuf;
88  pixbuf += rowlen;
89  }
90  else
91  scanbuf[i] = 0;
92  *scanp = scanbuf;
93 
94  return 0;
95 }
96 
97 
98 /*****************************************************************
99  * TAG( rle_row_free )
100  *
101  * Free storage allocated by rle_row_alloc().
102  * Inputs:
103  * the_hdr: Header structure as above.
104  * scanp: Pointer to scanbuf above.
105  * Outputs:
106  * Frees storage referenced by scanp and nrawp.
107  * Assumptions:
108  * Storage was allocated by rle_row_alloc, or by use of same
109  * algorithm, at least.
110  * Algorithm:
111  * free scanp[0] and scanp.
112  */
113 void
115 rle_hdr *the_hdr;
116 rle_pixel **scanp;
117 {
118  int i;
119 
120  if ( the_hdr->alpha )
121  scanp--;
122  for ( i = 0; i < the_hdr->ncolors + the_hdr->alpha; i++ )
123  if ( scanp[i] != 0 )
124  {
125  free( (char *)scanp[i] );
126  break;
127  }
128  free( (char *)scanp );
129 }
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int xmax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
int alpha
Definition: rle.h:100
#define RLE_ALPHA
Definition: rle.h:65
int ncolors
Definition: rle.h:100
#define RLE_BIT(glob, bit)
Definition: rle.h:126