1 /*
2 
3  rl2raw -- raw buffer export functions
4 
5  version 0.1, 2013 April 1
6 
7  Author: Sandro Furieri a.furieri@lqt.it
8 
9  -----------------------------------------------------------------------------
10 
11  Version: MPL 1.1/GPL 2.0/LGPL 2.1
12 
13  The contents of this file are subject to the Mozilla Public License Version
14  1.1 (the "License"); you may not use this file except in compliance with
15  the License. You may obtain a copy of the License at
16  http://www.mozilla.org/MPL/
17 
18 Software distributed under the License is distributed on an "AS IS" basis,
19 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
20 for the specific language governing rights and limitations under the
21 License.
22 
23 The Original Code is the SpatiaLite library
24 
25 The Initial Developer of the Original Code is Alessandro Furieri
26 
27 Portions created by the Initial Developer are Copyright (C) 2008-2013
28 the Initial Developer. All Rights Reserved.
29 
30 Alternatively, the contents of this file may be used under the terms of
31 either the GNU General Public License Version 2 or later (the "GPL"), or
32 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33 in which case the provisions of the GPL or the LGPL are applicable instead
34 of those above. If you wish to allow use of your version of this file only
35 under the terms of either the GPL or the LGPL, and not to allow others to
36 use your version of this file under the terms of the MPL, indicate your
37 decision by deleting the provisions above and replace them with the notice
38 and other provisions required by the GPL or the LGPL. If you do not delete
39 the provisions above, a recipient may use your version of this file under
40 the terms of any one of the MPL, the GPL or the LGPL.
41 
42 */
43 
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <float.h>
48 
49 #include "config.h"
50 
51 #ifdef LOADABLE_EXTENSION
52 #include "rasterlite2/sqlite.h"
53 #endif
54 
55 #include "rasterlite2/rasterlite2.h"
56 #include "rasterlite2_private.h"
57 
58 static int
check_as_rgb(rl2PrivRasterPtr rst)59 check_as_rgb (rl2PrivRasterPtr rst)
60 {
61 /* check if this raster could be exported as RGB */
62     switch (rst->pixelType)
63       {
64       case RL2_PIXEL_MONOCHROME:
65       case RL2_PIXEL_PALETTE:
66       case RL2_PIXEL_GRAYSCALE:
67       case RL2_PIXEL_RGB:
68 	  return 1;
69       };
70     return 0;
71 }
72 
73 static void
grayscale_as_rgb(unsigned char sample_type,unsigned char gray,unsigned char * r,unsigned char * g,unsigned char * b)74 grayscale_as_rgb (unsigned char sample_type, unsigned char gray,
75 		  unsigned char *r, unsigned char *g, unsigned char *b)
76 {
77 /* grayscale 8bit resolution as RGB */
78     unsigned char index;
79     *r = 0;
80     *g = 0;
81     *b = 0;
82     switch (sample_type)
83       {
84       case RL2_SAMPLE_2_BIT:
85 	  switch (gray)
86 	    {
87 	    case 3:
88 		index = 255;
89 		break;
90 	    case 2:
91 		index = 170;
92 		break;
93 	    case 1:
94 		index = 86;
95 		break;
96 	    case 0:
97 	    default:
98 		index = 0;
99 		break;
100 	    };
101 	  *r = index;
102 	  *g = index;
103 	  *b = index;
104 	  break;
105       case RL2_SAMPLE_4_BIT:
106 	  switch (gray)
107 	    {
108 	    case 15:
109 		index = 255;
110 		break;
111 	    case 14:
112 		index = 239;
113 		break;
114 	    case 13:
115 		index = 222;
116 		break;
117 	    case 12:
118 		index = 205;
119 		break;
120 	    case 11:
121 		index = 188;
122 		break;
123 	    case 10:
124 		index = 171;
125 		break;
126 	    case 9:
127 		index = 154;
128 		break;
129 	    case 8:
130 		index = 137;
131 		break;
132 	    case 7:
133 		index = 119;
134 		break;
135 	    case 6:
136 		index = 102;
137 		break;
138 	    case 5:
139 		index = 85;
140 		break;
141 	    case 4:
142 		index = 68;
143 		break;
144 	    case 3:
145 		index = 51;
146 		break;
147 	    case 2:
148 		index = 34;
149 		break;
150 	    case 1:
151 		index = 17;
152 		break;
153 	    case 0:
154 	    default:
155 		index = 0;
156 		break;
157 	    };
158 	  *r = index;
159 	  *g = index;
160 	  *b = index;
161 	  break;
162       case RL2_SAMPLE_UINT8:
163 	  *r = gray;
164 	  *g = gray;
165 	  *b = gray;
166 	  break;
167       };
168 }
169 
170 RL2_DECLARE int
rl2_raster_data_to_RGB(rl2RasterPtr ptr,unsigned char ** buffer,int * buf_size)171 rl2_raster_data_to_RGB (rl2RasterPtr ptr, unsigned char **buffer, int *buf_size)
172 {
173 /* attempting to export Raster pixel data as an RGB array */
174     unsigned char *buf;
175     int sz;
176     unsigned int row;
177     unsigned int col;
178     unsigned char *p_in;
179     unsigned char *p_out;
180     unsigned short max_palette;
181     unsigned char *red = NULL;
182     unsigned char *green = NULL;
183     unsigned char *blue = NULL;
184     unsigned char index;
185     unsigned char r;
186     unsigned char g;
187     unsigned char b;
188     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
189 
190     *buffer = NULL;
191     *buf_size = 0;
192     if (rst == NULL)
193 	return RL2_ERROR;
194     if (!check_as_rgb (rst))
195 	return RL2_ERROR;
196 
197     if (rst->pixelType == RL2_PIXEL_PALETTE)
198       {
199 	  /* there is a palette */
200 	  if (rl2_get_palette_colors
201 	      ((rl2PalettePtr) (rst->Palette), &max_palette, &red, &green,
202 	       &blue) != RL2_OK)
203 	      return RL2_ERROR;
204       }
205 
206     sz = rst->width * rst->height * 3;
207     buf = malloc (sz);
208     if (buf == NULL)
209 	return RL2_ERROR;
210 
211     p_in = rst->rasterBuffer;
212     p_out = buf;
213     for (row = 0; row < rst->height; row++)
214       {
215 	  for (col = 0; col < rst->width; col++)
216 	    {
217 		switch (rst->pixelType)
218 		  {
219 		  case RL2_PIXEL_MONOCHROME:
220 		      if (*p_in++ == 0)
221 			  index = 255;
222 		      else
223 			  index = 0;
224 		      *p_out++ = index;
225 		      *p_out++ = index;
226 		      *p_out++ = index;
227 		      break;
228 		  case RL2_PIXEL_PALETTE:
229 		      index = *p_in++;
230 		      if (index < max_palette)
231 			{
232 			    *p_out++ = *(red + index);
233 			    *p_out++ = *(green + index);
234 			    *p_out++ = *(blue + index);
235 			}
236 		      else
237 			{
238 			    /* default - inserting a BLACK pixel */
239 			    *p_out++ = 0;
240 			    *p_out++ = 0;
241 			    *p_out++ = 0;
242 			}
243 		      break;
244 		  case RL2_PIXEL_GRAYSCALE:
245 		      grayscale_as_rgb (rst->sampleType, *p_in++, &r, &g, &b);
246 		      *p_out++ = r;
247 		      *p_out++ = g;
248 		      *p_out++ = b;
249 		      break;
250 		  case RL2_PIXEL_RGB:
251 		      *p_out++ = *p_in++;
252 		      *p_out++ = *p_in++;
253 		      *p_out++ = *p_in++;
254 		      break;
255 		  };
256 	    }
257       }
258 
259     *buffer = buf;
260     *buf_size = sz;
261     if (red != NULL)
262 	free (red);
263     if (green != NULL)
264 	free (green);
265     if (blue != NULL)
266 	free (blue);
267     return RL2_OK;
268 }
269 
270 static int
eval_transparent_pixels(unsigned char r,unsigned char g,unsigned char b,unsigned char transpR,unsigned char transpG,unsigned char transpB)271 eval_transparent_pixels (unsigned char r, unsigned char g, unsigned char b,
272 			 unsigned char transpR, unsigned char transpG,
273 			 unsigned char transpB)
274 {
275 /* evaluating a transparent color match */
276     if (r != transpR)
277 	return 0;
278     if (g != transpG)
279 	return 0;
280     if (b != transpB)
281 	return 0;
282     return 1;
283 }
284 
285 RL2_DECLARE int
rl2_raster_data_to_RGBA(rl2RasterPtr ptr,unsigned char ** buffer,int * buf_size)286 rl2_raster_data_to_RGBA (rl2RasterPtr ptr, unsigned char **buffer,
287 			 int *buf_size)
288 {
289 /* attempting to export Raster pixel data as an RGBA array */
290     unsigned char *buf;
291     int sz;
292     unsigned int row;
293     unsigned int col;
294     unsigned char *p_in;
295     unsigned char *p_mask;
296     unsigned char *p_out;
297     unsigned short max_palette;
298     unsigned char *red = NULL;
299     unsigned char *green = NULL;
300     unsigned char *blue = NULL;
301     unsigned char index;
302     unsigned char r;
303     unsigned char g;
304     unsigned char b;
305     unsigned char a;
306     unsigned char transpR;
307     unsigned char transpG;
308     unsigned char transpB;
309     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
310 
311     *buffer = NULL;
312     *buf_size = 0;
313     if (rst == NULL)
314 	return RL2_ERROR;
315     if (!check_as_rgb (rst))
316 	return RL2_ERROR;
317 
318     if (rst->pixelType == RL2_PIXEL_PALETTE)
319       {
320 	  /* there is a palette */
321 	  if (rl2_get_palette_colors
322 	      ((rl2PalettePtr) (rst->Palette), &max_palette, &red, &green,
323 	       &blue) != RL2_OK)
324 	      return RL2_ERROR;
325       }
326 
327     sz = rst->width * rst->height * 4;
328     buf = malloc (sz);
329     if (buf == NULL)
330 	return RL2_ERROR;
331 
332     if (rst->noData != NULL)
333       {
334 	  /* preparing the transparent color */
335 	  rl2PrivPixelPtr no_data = rst->noData;
336 	  switch (no_data->pixelType)
337 	    {
338 	    case RL2_PIXEL_MONOCHROME:
339 		if (no_data->Samples->uint8 == 0)
340 		  {
341 		      transpR = 255;
342 		      transpG = 255;
343 		      transpB = 255;
344 		  }
345 		else
346 		  {
347 		      transpR = 0;
348 		      transpG = 0;
349 		      transpB = 0;
350 		  }
351 		break;
352 	    case RL2_PIXEL_PALETTE:
353 		index = no_data->Samples->uint8;
354 		if (index < max_palette)
355 		  {
356 		      transpR = *(red + index);
357 		      transpG = *(green + index);
358 		      transpB = *(blue + index);
359 		  }
360 		else
361 		  {
362 		      /* default - inserting a BLACK pixel */
363 		      transpR = 0;
364 		      transpG = 0;
365 		      transpB = 0;
366 		  }
367 		break;
368 	    case RL2_PIXEL_GRAYSCALE:
369 		grayscale_as_rgb (rst->sampleType, no_data->Samples->uint8,
370 				  &transpR, &transpG, &transpB);
371 		break;
372 	    case RL2_PIXEL_RGB:
373 		rl2_get_pixel_sample_uint8 ((rl2PixelPtr) no_data, RL2_RED_BAND,
374 					    &transpR);
375 		rl2_get_pixel_sample_uint8 ((rl2PixelPtr) no_data,
376 					    RL2_GREEN_BAND, &transpG);
377 		rl2_get_pixel_sample_uint8 ((rl2PixelPtr) no_data,
378 					    RL2_BLUE_BAND, &transpB);
379 		break;
380 	    };
381       }
382 
383     p_in = rst->rasterBuffer;
384     p_mask = rst->maskBuffer;
385     p_out = buf;
386     for (row = 0; row < rst->height; row++)
387       {
388 	  for (col = 0; col < rst->width; col++)
389 	    {
390 		switch (rst->pixelType)
391 		  {
392 		  case RL2_PIXEL_MONOCHROME:
393 		      if (*p_in++ == 0)
394 			  index = 255;
395 		      else
396 			  index = 0;
397 		      r = index;
398 		      g = index;
399 		      b = index;
400 		      *p_out++ = r;
401 		      *p_out++ = g;
402 		      *p_out++ = b;
403 		      a = 255;
404 		      if (p_mask != NULL)
405 			{
406 			    if (*p_mask++ == 0)
407 				a = 0;
408 			}
409 		      if (rst->noData != NULL)
410 			{
411 			    /* evaluating transparent color */
412 			    if (eval_transparent_pixels
413 				(r, g, b, transpR, transpG, transpB))
414 				a = 0;
415 			}
416 		      *p_out++ = a;
417 		      break;
418 		  case RL2_PIXEL_PALETTE:
419 		      index = *p_in++;
420 		      if (index < max_palette)
421 			{
422 			    r = *(red + index);
423 			    g = *(green + index);
424 			    b = *(blue + index);
425 			}
426 		      else
427 			{
428 			    /* default - inserting a BLACK pixel */
429 			    r = 0;
430 			    g = 0;
431 			    b = 0;
432 			}
433 		      *p_out++ = r;
434 		      *p_out++ = g;
435 		      *p_out++ = b;
436 		      a = 255;
437 		      if (p_mask != NULL)
438 			{
439 			    if (*p_mask++ == 0)
440 				a = 0;
441 			}
442 		      if (rst->noData != NULL)
443 			{
444 			    /* evaluating transparent color */
445 			    if (eval_transparent_pixels
446 				(r, g, b, transpR, transpG, transpB))
447 				a = 0;
448 			}
449 		      *p_out++ = a;
450 		      break;
451 		  case RL2_PIXEL_GRAYSCALE:
452 		      grayscale_as_rgb (rst->sampleType, *p_in++, &r, &g, &b);
453 		      *p_out++ = r;
454 		      *p_out++ = g;
455 		      *p_out++ = b;
456 		      a = 255;
457 		      if (p_mask != NULL)
458 			{
459 			    if (*p_mask++ == 0)
460 				a = 0;
461 			}
462 		      if (rst->noData != NULL)
463 			{
464 			    /* evaluating transparent color */
465 			    if (eval_transparent_pixels
466 				(r, g, b, transpR, transpG, transpB))
467 				a = 0;
468 			}
469 		      *p_out++ = a;
470 		      break;
471 		  case RL2_PIXEL_RGB:
472 		      r = *p_in++;
473 		      g = *p_in++;
474 		      b = *p_in++;
475 		      *p_out++ = r;
476 		      *p_out++ = g;
477 		      *p_out++ = b;
478 		      a = 255;
479 		      if (p_mask != NULL)
480 			{
481 			    if (*p_mask++ == 0)
482 				a = 0;
483 			}
484 		      if (rst->noData != NULL)
485 			{
486 			    /* evaluating transparent color */
487 			    if (eval_transparent_pixels
488 				(r, g, b, transpR, transpG, transpB))
489 				a = 0;
490 			}
491 		      *p_out++ = a;
492 		      break;
493 		  };
494 	    }
495       }
496 
497     *buffer = buf;
498     *buf_size = sz;
499     if (red != NULL)
500 	free (red);
501     if (green != NULL)
502 	free (green);
503     if (blue != NULL)
504 	free (blue);
505     return RL2_OK;
506 }
507 
508 RL2_DECLARE int
rl2_raster_data_to_ARGB(rl2RasterPtr ptr,unsigned char ** buffer,int * buf_size)509 rl2_raster_data_to_ARGB (rl2RasterPtr ptr, unsigned char **buffer,
510 			 int *buf_size)
511 {
512 /* attempting to export Raster pixel data as an ARGB array */
513     unsigned char *buf;
514     int sz;
515     unsigned int row;
516     unsigned int col;
517     unsigned char *p_in;
518     unsigned char *p_mask;
519     unsigned char *p_out;
520     unsigned short max_palette;
521     unsigned char *red = NULL;
522     unsigned char *green = NULL;
523     unsigned char *blue = NULL;
524     unsigned char index;
525     unsigned char a;
526     unsigned char *p_alpha;
527     unsigned char r;
528     unsigned char g;
529     unsigned char b;
530     unsigned char transpR;
531     unsigned char transpG;
532     unsigned char transpB;
533     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
534 
535     *buffer = NULL;
536     *buf_size = 0;
537     if (rst == NULL)
538 	return RL2_ERROR;
539     if (!check_as_rgb (rst))
540 	return RL2_ERROR;
541 
542     if (rst->pixelType == RL2_PIXEL_PALETTE)
543       {
544 	  /* there is a palette */
545 	  if (rl2_get_palette_colors
546 	      ((rl2PalettePtr) (rst->Palette), &max_palette, &red, &green,
547 	       &blue) != RL2_OK)
548 	      return RL2_ERROR;
549       }
550 
551     sz = rst->width * rst->height * 4;
552     buf = malloc (sz);
553     if (buf == NULL)
554 	return RL2_ERROR;
555 
556     if (rst->noData != NULL)
557       {
558 	  /* preparing the transparent color */
559 	  rl2PrivPixelPtr no_data = rst->noData;
560 	  switch (no_data->pixelType)
561 	    {
562 	    case RL2_PIXEL_MONOCHROME:
563 		if (no_data->Samples->uint8 == 0)
564 		  {
565 		      transpR = 255;
566 		      transpG = 255;
567 		      transpB = 255;
568 		  }
569 		else
570 		  {
571 		      transpR = 0;
572 		      transpG = 0;
573 		      transpB = 0;
574 		  }
575 		break;
576 	    case RL2_PIXEL_PALETTE:
577 		index = no_data->Samples->uint8;
578 		if (index < max_palette)
579 		  {
580 		      transpR = *(red + index);
581 		      transpG = *(green + index);
582 		      transpB = *(blue + index);
583 		  }
584 		else
585 		  {
586 		      /* default - inserting a BLACK pixel */
587 		      transpR = 0;
588 		      transpG = 0;
589 		      transpB = 0;
590 		  }
591 		break;
592 	    case RL2_PIXEL_GRAYSCALE:
593 		grayscale_as_rgb (rst->sampleType, no_data->Samples->uint8,
594 				  &transpR, &transpG, &transpB);
595 		break;
596 	    case RL2_PIXEL_RGB:
597 		rl2_get_pixel_sample_uint8 ((rl2PixelPtr) no_data, RL2_RED_BAND,
598 					    &transpR);
599 		rl2_get_pixel_sample_uint8 ((rl2PixelPtr) no_data,
600 					    RL2_GREEN_BAND, &transpG);
601 		rl2_get_pixel_sample_uint8 ((rl2PixelPtr) no_data,
602 					    RL2_BLUE_BAND, &transpB);
603 		break;
604 	    };
605       }
606 
607     p_in = rst->rasterBuffer;
608     p_mask = rst->maskBuffer;
609     p_out = buf;
610     for (row = 0; row < rst->height; row++)
611       {
612 	  for (col = 0; col < rst->width; col++)
613 	    {
614 		if (p_mask == NULL)
615 		    a = 255;
616 		else
617 		  {
618 		      if (*p_mask++ == 0)
619 			  a = 0;
620 		      else
621 			  a = 255;
622 		  }
623 		switch (rst->pixelType)
624 		  {
625 		  case RL2_PIXEL_MONOCHROME:
626 		      if (*p_in++ == 0)
627 			  index = 255;
628 		      else
629 			  index = 0;
630 		      p_alpha = p_out++;
631 		      r = *p_out++ = index;
632 		      g = *p_out++ = index;
633 		      b = *p_out++ = index;
634 		      break;
635 		  case RL2_PIXEL_PALETTE:
636 		      index = *p_in++;
637 		      if (index < max_palette)
638 			{
639 			    *p_out++ = 255;
640 			    *p_out++ = *(red + index);
641 			    *p_out++ = *(green + index);
642 			    *p_out++ = *(blue + index);
643 			}
644 		      else
645 			{
646 			    /* default - inserting a BLACK pixel */
647 			    *p_out++ = 255;
648 			    *p_out++ = 0;
649 			    *p_out++ = 0;
650 			    *p_out++ = 0;
651 			}
652 		      break;
653 		  case RL2_PIXEL_GRAYSCALE:
654 		      grayscale_as_rgb (rst->sampleType, *p_in++, &r, &g, &b);
655 		      p_alpha = p_out++;
656 		      *p_out++ = r;
657 		      *p_out++ = g;
658 		      *p_out++ = b;
659 		      break;
660 		  case RL2_PIXEL_RGB:
661 		      p_alpha = p_out++;
662 		      r = *p_out++ = *p_in++;
663 		      g = *p_out++ = *p_in++;
664 		      b = *p_out++ = *p_in++;
665 		      break;
666 		  };
667 		if (rst->pixelType != RL2_PIXEL_PALETTE)
668 		  {
669 		      if (rst->noData != NULL)
670 			{
671 			    /* evaluating transparent color */
672 			    if (eval_transparent_pixels
673 				(r, g, b, transpR, transpG, transpB))
674 				a = 0;
675 			}
676 		      *p_alpha = a;
677 		  }
678 	    }
679       }
680 
681     *buffer = buf;
682     *buf_size = sz;
683     if (red != NULL)
684 	free (red);
685     if (green != NULL)
686 	free (green);
687     if (blue != NULL)
688 	free (blue);
689     return RL2_OK;
690 }
691 
692 RL2_DECLARE int
rl2_raster_data_to_BGR(rl2RasterPtr ptr,unsigned char ** buffer,int * buf_size)693 rl2_raster_data_to_BGR (rl2RasterPtr ptr, unsigned char **buffer, int *buf_size)
694 {
695 /* attempting to export Raster pixel data as an BGR array */
696     unsigned char *buf;
697     int sz;
698     unsigned int row;
699     unsigned int col;
700     unsigned char *p_in;
701     unsigned char *p_out;
702     unsigned short max_palette;
703     unsigned char *red = NULL;
704     unsigned char *green = NULL;
705     unsigned char *blue = NULL;
706     unsigned char index;
707     unsigned char r;
708     unsigned char g;
709     unsigned char b;
710     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
711 
712     *buffer = NULL;
713     *buf_size = 0;
714     if (rst == NULL)
715 	return RL2_ERROR;
716     if (!check_as_rgb (rst))
717 	return RL2_ERROR;
718 
719     if (rst->pixelType == RL2_PIXEL_PALETTE)
720       {
721 	  /* there is a palette */
722 	  if (rl2_get_palette_colors
723 	      ((rl2PalettePtr) (rst->Palette), &max_palette, &red, &green,
724 	       &blue) != RL2_OK)
725 	      return RL2_ERROR;
726       }
727 
728     sz = rst->width * rst->height * 3;
729     buf = malloc (sz);
730     if (buf == NULL)
731 	return RL2_ERROR;
732 
733     p_in = rst->rasterBuffer;
734     p_out = buf;
735     for (row = 0; row < rst->height; row++)
736       {
737 	  for (col = 0; col < rst->width; col++)
738 	    {
739 		switch (rst->pixelType)
740 		  {
741 		  case RL2_PIXEL_MONOCHROME:
742 		      if (*p_in++ == 0)
743 			  index = 255;
744 		      else
745 			  index = 0;
746 		      *p_out++ = index;
747 		      *p_out++ = index;
748 		      *p_out++ = index;
749 		      break;
750 		  case RL2_PIXEL_PALETTE:
751 		      index = *p_in++;
752 		      if (index < max_palette)
753 			{
754 			    *p_out++ = *(blue + index);
755 			    *p_out++ = *(green + index);
756 			    *p_out++ = *(red + index);
757 			}
758 		      else
759 			{
760 			    /* default - inserting a BLACK pixel */
761 			    *p_out++ = 0;
762 			    *p_out++ = 0;
763 			    *p_out++ = 0;
764 			}
765 		      break;
766 		  case RL2_PIXEL_GRAYSCALE:
767 		      grayscale_as_rgb (rst->sampleType, *p_in++, &r, &g, &b);
768 		      *p_out++ = b;
769 		      *p_out++ = g;
770 		      *p_out++ = r;
771 		      break;
772 		  case RL2_PIXEL_RGB:
773 		      r = *p_in++;
774 		      g = *p_in++;
775 		      b = *p_in++;
776 		      *p_out++ = b;
777 		      *p_out++ = g;
778 		      *p_out++ = r;
779 		      break;
780 		  };
781 	    }
782       }
783 
784     *buffer = buf;
785     *buf_size = sz;
786     if (red != NULL)
787 	free (red);
788     if (green != NULL)
789 	free (green);
790     if (blue != NULL)
791 	free (blue);
792     return RL2_OK;
793 }
794 
795 RL2_DECLARE int
rl2_raster_data_to_BGRA(rl2RasterPtr ptr,unsigned char ** buffer,int * buf_size)796 rl2_raster_data_to_BGRA (rl2RasterPtr ptr, unsigned char **buffer,
797 			 int *buf_size)
798 {
799 /* attempting to export Raster pixel data as an BGRA array */
800     unsigned char *buf;
801     int sz;
802     unsigned int row;
803     unsigned int col;
804     unsigned char *p_in;
805     unsigned char *p_mask;
806     unsigned char *p_out;
807     unsigned short max_palette;
808     unsigned char *red = NULL;
809     unsigned char *green = NULL;
810     unsigned char *blue = NULL;
811     unsigned char index;
812     unsigned char r;
813     unsigned char g;
814     unsigned char b;
815     unsigned char a;
816     unsigned char transpR;
817     unsigned char transpG;
818     unsigned char transpB;
819     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
820 
821     *buffer = NULL;
822     *buf_size = 0;
823     if (rst == NULL)
824 	return RL2_ERROR;
825     if (!check_as_rgb (rst))
826 	return RL2_ERROR;
827 
828     if (rst->pixelType == RL2_PIXEL_PALETTE)
829       {
830 	  /* there is a palette */
831 	  if (rl2_get_palette_colors
832 	      ((rl2PalettePtr) (rst->Palette), &max_palette, &red, &green,
833 	       &blue) != RL2_OK)
834 	      return RL2_ERROR;
835       }
836 
837     sz = rst->width * rst->height * 4;
838     buf = malloc (sz);
839     if (buf == NULL)
840 	return RL2_ERROR;
841 
842     if (rst->noData != NULL)
843       {
844 	  /* preparing the transparent color */
845 	  rl2PrivPixelPtr no_data = rst->noData;
846 	  switch (no_data->pixelType)
847 	    {
848 	    case RL2_PIXEL_MONOCHROME:
849 		if (no_data->Samples->uint8 == 0)
850 		  {
851 		      transpR = 255;
852 		      transpG = 255;
853 		      transpB = 255;
854 		  }
855 		else
856 		  {
857 		      transpR = 0;
858 		      transpG = 0;
859 		      transpB = 0;
860 		  }
861 		break;
862 	    case RL2_PIXEL_PALETTE:
863 		index = no_data->Samples->uint8;
864 		if (index < max_palette)
865 		  {
866 		      transpR = *(red + index);
867 		      transpG = *(green + index);
868 		      transpB = *(blue + index);
869 		  }
870 		else
871 		  {
872 		      /* default - inserting a BLACK pixel */
873 		      transpR = 0;
874 		      transpG = 0;
875 		      transpB = 0;
876 		  }
877 		break;
878 	    case RL2_PIXEL_GRAYSCALE:
879 		grayscale_as_rgb (rst->sampleType, no_data->Samples->uint8,
880 				  &transpR, &transpG, &transpB);
881 		break;
882 	    case RL2_PIXEL_RGB:
883 		rl2_get_pixel_sample_uint8 ((rl2PixelPtr) no_data, RL2_RED_BAND,
884 					    &transpR);
885 		rl2_get_pixel_sample_uint8 ((rl2PixelPtr) no_data,
886 					    RL2_GREEN_BAND, &transpG);
887 		rl2_get_pixel_sample_uint8 ((rl2PixelPtr) no_data,
888 					    RL2_BLUE_BAND, &transpB);
889 		break;
890 	    };
891       }
892 
893     p_in = rst->rasterBuffer;
894     p_mask = rst->maskBuffer;
895     p_out = buf;
896     for (row = 0; row < rst->height; row++)
897       {
898 	  for (col = 0; col < rst->width; col++)
899 	    {
900 		if (p_mask == NULL)
901 		    a = 255;
902 		else
903 		  {
904 		      if (*p_mask++ == 0)
905 			  a = 0;
906 		      else
907 			  a = 255;
908 		  }
909 		switch (rst->pixelType)
910 		  {
911 		  case RL2_PIXEL_MONOCHROME:
912 		      if (*p_in++ == 0)
913 			  index = 255;
914 		      else
915 			  index = 0;
916 		      b = *p_out++ = index;
917 		      g = *p_out++ = index;
918 		      r = *p_out++ = index;
919 		      break;
920 		  case RL2_PIXEL_PALETTE:
921 		      index = *p_in++;
922 		      if (index < max_palette)
923 			{
924 			    *p_out++ = *(blue + index);
925 			    *p_out++ = *(green + index);
926 			    *p_out++ = *(red + index);
927 			    *p_out++ = 255;
928 			}
929 		      else
930 			{
931 			    /* default - inserting a BLACK pixel */
932 			    *p_out++ = 0;
933 			    *p_out++ = 0;
934 			    *p_out++ = 0;
935 			    *p_out++ = 255;
936 			}
937 		      break;
938 		  case RL2_PIXEL_GRAYSCALE:
939 		      grayscale_as_rgb (rst->sampleType, *p_in++, &r, &g, &b);
940 		      *p_out++ = b;
941 		      *p_out++ = g;
942 		      *p_out++ = r;
943 		      break;
944 		  case RL2_PIXEL_RGB:
945 		      r = *p_in++;
946 		      g = *p_in++;
947 		      b = *p_in++;
948 		      *p_out++ = b;
949 		      *p_out++ = g;
950 		      *p_out++ = r;
951 		      break;
952 		  };
953 		if (rst->pixelType != RL2_PIXEL_PALETTE)
954 		  {
955 		      if (rst->noData != NULL)
956 			{
957 			    /* evaluating transparent color */
958 			    if (eval_transparent_pixels
959 				(r, g, b, transpR, transpG, transpB))
960 				a = 0;
961 			}
962 		      *p_out++ = a;
963 		  }
964 	    }
965       }
966 
967     *buffer = buf;
968     *buf_size = sz;
969     if (red != NULL)
970 	free (red);
971     if (green != NULL)
972 	free (green);
973     if (blue != NULL)
974 	free (blue);
975     return RL2_OK;
976 }
977 
978 static int
check_as_datagrid(rl2PrivRasterPtr rst,unsigned char sample_type)979 check_as_datagrid (rl2PrivRasterPtr rst, unsigned char sample_type)
980 {
981 /* check if this raster could be exported as a DATAGRID */
982     if (rst->pixelType == RL2_PIXEL_DATAGRID && rst->sampleType == sample_type)
983 	return 1;
984     return 0;
985 }
986 
987 static int
check_as_grayscale256(rl2PrivRasterPtr rst)988 check_as_grayscale256 (rl2PrivRasterPtr rst)
989 {
990 /* check if this raster could be exported as a GRAYSCALE 8 bit */
991     if (rst->pixelType == RL2_PIXEL_GRAYSCALE
992 	&& rst->sampleType == RL2_SAMPLE_UINT8)
993 	return 1;
994     return 0;
995 }
996 
997 static int
check_as_palette256(rl2PrivRasterPtr rst)998 check_as_palette256 (rl2PrivRasterPtr rst)
999 {
1000 /* check if this raster could be exported as a PALETTE 8 bit */
1001     if (rst->pixelType == RL2_PIXEL_PALETTE
1002 	&& rst->sampleType == RL2_SAMPLE_UINT8)
1003 	return 1;
1004     return 0;
1005 }
1006 
1007 RL2_DECLARE int
rl2_raster_data_to_int8(rl2RasterPtr ptr,char ** buffer,int * buf_size)1008 rl2_raster_data_to_int8 (rl2RasterPtr ptr, char **buffer, int *buf_size)
1009 {
1010 /* attempting to export Raster DATAGRID data as an INT-8 array */
1011     char *buf;
1012     int sz;
1013     unsigned int row;
1014     unsigned int col;
1015     char *p_in;
1016     char *p_out;
1017     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1018 
1019     *buffer = NULL;
1020     *buf_size = 0;
1021     if (rst == NULL)
1022 	return RL2_ERROR;
1023     if (!check_as_datagrid (rst, RL2_SAMPLE_INT8))
1024 	return RL2_ERROR;
1025 
1026     sz = rst->width * rst->height;
1027     buf = malloc (sz);
1028     if (buf == NULL)
1029 	return RL2_ERROR;
1030 
1031     p_in = (char *) (rst->rasterBuffer);
1032     p_out = buf;
1033     for (row = 0; row < rst->height; row++)
1034       {
1035 	  for (col = 0; col < rst->width; col++)
1036 	      *p_out++ = *p_in++;
1037       }
1038 
1039     *buffer = buf;
1040     *buf_size = sz;
1041     return RL2_OK;
1042 }
1043 
1044 RL2_DECLARE int
rl2_raster_data_to_uint8(rl2RasterPtr ptr,unsigned char ** buffer,int * buf_size)1045 rl2_raster_data_to_uint8 (rl2RasterPtr ptr, unsigned char **buffer,
1046 			  int *buf_size)
1047 {
1048 /* attempting to export Raster DATAGRID/GRAYSCALE data as a UINT-8 array */
1049     unsigned char *buf;
1050     int sz;
1051     unsigned int row;
1052     unsigned int col;
1053     unsigned char *p_in;
1054     unsigned char *p_out;
1055     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1056 
1057     *buffer = NULL;
1058     *buf_size = 0;
1059     if (rst == NULL)
1060 	return RL2_ERROR;
1061     if (!check_as_datagrid (rst, RL2_SAMPLE_UINT8))
1062       {
1063 	  if (!check_as_grayscale256 (rst) && !check_as_palette256 (rst))
1064 	      return RL2_ERROR;
1065       }
1066 
1067     sz = rst->width * rst->height;
1068     buf = malloc (sz);
1069     if (buf == NULL)
1070 	return RL2_ERROR;
1071 
1072     p_in = rst->rasterBuffer;
1073     p_out = buf;
1074     for (row = 0; row < rst->height; row++)
1075       {
1076 	  for (col = 0; col < rst->width; col++)
1077 	      *p_out++ = *p_in++;
1078       }
1079 
1080     *buffer = buf;
1081     *buf_size = sz;
1082     return RL2_OK;
1083 }
1084 
1085 RL2_DECLARE int
rl2_raster_data_to_int16(rl2RasterPtr ptr,short ** buffer,int * buf_size)1086 rl2_raster_data_to_int16 (rl2RasterPtr ptr, short **buffer, int *buf_size)
1087 {
1088 /* attempting to export Raster DATAGRID data as an INT-16 array */
1089     short *buf;
1090     int sz;
1091     unsigned int row;
1092     unsigned int col;
1093     short *p_in;
1094     short *p_out;
1095     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1096 
1097     *buffer = NULL;
1098     *buf_size = 0;
1099     if (rst == NULL)
1100 	return RL2_ERROR;
1101     if (!check_as_datagrid (rst, RL2_SAMPLE_INT16))
1102 	return RL2_ERROR;
1103 
1104     sz = rst->width * rst->height * sizeof (short);
1105     buf = malloc (sz);
1106     if (buf == NULL)
1107 	return RL2_ERROR;
1108 
1109     p_in = (short *) (rst->rasterBuffer);
1110     p_out = buf;
1111     for (row = 0; row < rst->height; row++)
1112       {
1113 	  for (col = 0; col < rst->width; col++)
1114 	      *p_out++ = *p_in++;
1115       }
1116 
1117     *buffer = buf;
1118     *buf_size = sz;
1119     return RL2_OK;
1120 }
1121 
1122 RL2_DECLARE int
rl2_raster_data_to_uint16(rl2RasterPtr ptr,unsigned short ** buffer,int * buf_size)1123 rl2_raster_data_to_uint16 (rl2RasterPtr ptr, unsigned short **buffer,
1124 			   int *buf_size)
1125 {
1126 /* attempting to export Raster DATAGRID data as a UINT-16 array */
1127     unsigned short *buf;
1128     int sz;
1129     unsigned int row;
1130     unsigned int col;
1131     unsigned short *p_in;
1132     unsigned short *p_out;
1133     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1134 
1135     *buffer = NULL;
1136     *buf_size = 0;
1137     if (rst == NULL)
1138 	return RL2_ERROR;
1139     if (!check_as_datagrid (rst, RL2_SAMPLE_UINT16))
1140 	return RL2_ERROR;
1141 
1142     sz = rst->width * rst->height * sizeof (unsigned short);
1143     buf = malloc (sz);
1144     if (buf == NULL)
1145 	return RL2_ERROR;
1146 
1147     p_in = (unsigned short *) (rst->rasterBuffer);
1148     p_out = buf;
1149     for (row = 0; row < rst->height; row++)
1150       {
1151 	  for (col = 0; col < rst->width; col++)
1152 	      *p_out++ = *p_in++;
1153       }
1154 
1155     *buffer = buf;
1156     *buf_size = sz;
1157     return RL2_OK;
1158 }
1159 
1160 RL2_DECLARE int
rl2_raster_data_to_int32(rl2RasterPtr ptr,int ** buffer,int * buf_size)1161 rl2_raster_data_to_int32 (rl2RasterPtr ptr, int **buffer, int *buf_size)
1162 {
1163 /* attempting to export Raster DATAGRID data as an INT-32 array */
1164     int *buf;
1165     int sz;
1166     unsigned int row;
1167     unsigned int col;
1168     int *p_in;
1169     int *p_out;
1170     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1171 
1172     *buffer = NULL;
1173     *buf_size = 0;
1174     if (rst == NULL)
1175 	return RL2_ERROR;
1176     if (!check_as_datagrid (rst, RL2_SAMPLE_INT32))
1177 	return RL2_ERROR;
1178 
1179     sz = rst->width * rst->height * sizeof (int);
1180     buf = malloc (sz);
1181     if (buf == NULL)
1182 	return RL2_ERROR;
1183 
1184     p_in = (int *) (rst->rasterBuffer);
1185     p_out = buf;
1186     for (row = 0; row < rst->height; row++)
1187       {
1188 	  for (col = 0; col < rst->width; col++)
1189 	      *p_out++ = *p_in++;
1190       }
1191 
1192     *buffer = buf;
1193     *buf_size = sz;
1194     return RL2_OK;
1195 }
1196 
1197 RL2_DECLARE int
rl2_raster_data_to_uint32(rl2RasterPtr ptr,unsigned int ** buffer,int * buf_size)1198 rl2_raster_data_to_uint32 (rl2RasterPtr ptr, unsigned int **buffer,
1199 			   int *buf_size)
1200 {
1201 /* attempting to export Raster DATAGRID data as a UINT-32 array */
1202     unsigned int *buf;
1203     int sz;
1204     unsigned int row;
1205     unsigned int col;
1206     unsigned int *p_in;
1207     unsigned int *p_out;
1208     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1209 
1210     *buffer = NULL;
1211     *buf_size = 0;
1212     if (rst == NULL)
1213 	return RL2_ERROR;
1214     if (!check_as_datagrid (rst, RL2_SAMPLE_UINT32))
1215 	return RL2_ERROR;
1216 
1217     sz = rst->width * rst->height * sizeof (unsigned int);
1218     buf = malloc (sz);
1219     if (buf == NULL)
1220 	return RL2_ERROR;
1221 
1222     p_in = (unsigned int *) (rst->rasterBuffer);
1223     p_out = buf;
1224     for (row = 0; row < rst->height; row++)
1225       {
1226 	  for (col = 0; col < rst->width; col++)
1227 	      *p_out++ = *p_in++;
1228       }
1229 
1230     *buffer = buf;
1231     *buf_size = sz;
1232     return RL2_OK;
1233 }
1234 
1235 RL2_DECLARE int
rl2_raster_data_to_float(rl2RasterPtr ptr,float ** buffer,int * buf_size)1236 rl2_raster_data_to_float (rl2RasterPtr ptr, float **buffer, int *buf_size)
1237 {
1238 /* attempting to export Raster DATAGRID data as a FLOAT array */
1239     float *buf;
1240     int sz;
1241     unsigned int row;
1242     unsigned int col;
1243     float *p_in;
1244     float *p_out;
1245     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1246 
1247     *buffer = NULL;
1248     *buf_size = 0;
1249     if (rst == NULL)
1250 	return RL2_ERROR;
1251     if (!check_as_datagrid (rst, RL2_SAMPLE_FLOAT))
1252 	return RL2_ERROR;
1253 
1254     sz = rst->width * rst->height * sizeof (float);
1255     buf = malloc (sz);
1256     if (buf == NULL)
1257 	return RL2_ERROR;
1258 
1259     p_in = (float *) (rst->rasterBuffer);
1260     p_out = buf;
1261     for (row = 0; row < rst->height; row++)
1262       {
1263 	  for (col = 0; col < rst->width; col++)
1264 	      *p_out++ = *p_in++;
1265       }
1266 
1267     *buffer = buf;
1268     *buf_size = sz;
1269     return RL2_OK;
1270 }
1271 
1272 RL2_DECLARE int
rl2_raster_data_to_double(rl2RasterPtr ptr,double ** buffer,int * buf_size)1273 rl2_raster_data_to_double (rl2RasterPtr ptr, double **buffer, int *buf_size)
1274 {
1275 /* attempting to export Raster DATAGRID data as a DOUBLE array */
1276     double *buf;
1277     int sz;
1278     unsigned int row;
1279     unsigned int col;
1280     double *p_in;
1281     double *p_out;
1282     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1283 
1284     *buffer = NULL;
1285     *buf_size = 0;
1286     if (rst == NULL)
1287 	return RL2_ERROR;
1288     if (!check_as_datagrid (rst, RL2_SAMPLE_DOUBLE))
1289 	return RL2_ERROR;
1290 
1291     sz = rst->width * rst->height * sizeof (double);
1292     buf = malloc (sz);
1293     if (buf == NULL)
1294 	return RL2_ERROR;
1295 
1296     p_in = (double *) (rst->rasterBuffer);
1297     p_out = buf;
1298     for (row = 0; row < rst->height; row++)
1299       {
1300 	  for (col = 0; col < rst->width; col++)
1301 	      *p_out++ = *p_in++;
1302       }
1303 
1304     *buffer = buf;
1305     *buf_size = sz;
1306     return RL2_OK;
1307 }
1308 
1309 static int
check_as_band(rl2PrivRasterPtr rst,int band,unsigned char sample_type)1310 check_as_band (rl2PrivRasterPtr rst, int band, unsigned char sample_type)
1311 {
1312 /* check if this raster could be exported as a MULTIBAND */
1313     if (rst->pixelType == RL2_PIXEL_MULTIBAND && rst->sampleType == sample_type)
1314       {
1315 	  if (band >= 0 && band < rst->nBands)
1316 	      return 1;
1317       }
1318     if (rst->pixelType == RL2_PIXEL_RGB && rst->sampleType == sample_type)
1319       {
1320 	  if (band >= 0 && band < rst->nBands)
1321 	      return 1;
1322       }
1323     return 0;
1324 }
1325 
1326 RL2_DECLARE int
rl2_raster_data_to_1bit(rl2RasterPtr ptr,unsigned char ** buffer,int * buf_size)1327 rl2_raster_data_to_1bit (rl2RasterPtr ptr, unsigned char **buffer,
1328 			 int *buf_size)
1329 {
1330 /* attempting to export Raster PALETTE/MONOCHROME 1bit data as a UINT-8 array */
1331     unsigned char *buf;
1332     int sz;
1333     unsigned int row;
1334     unsigned int col;
1335     unsigned char *p_in;
1336     unsigned char *p_out;
1337     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1338 
1339     *buffer = NULL;
1340     *buf_size = 0;
1341     if (rst == NULL)
1342 	return RL2_ERROR;
1343     if (rst->sampleType == RL2_SAMPLE_1_BIT
1344 	&& (rst->pixelType == RL2_PIXEL_MONOCHROME
1345 	    || rst->pixelType == RL2_PIXEL_PALETTE))
1346 	;
1347     else
1348 	return RL2_ERROR;
1349 
1350     sz = rst->width * rst->height;
1351     buf = malloc (sz);
1352     if (buf == NULL)
1353 	return RL2_ERROR;
1354 
1355     p_in = rst->rasterBuffer;
1356     p_out = buf;
1357     for (row = 0; row < rst->height; row++)
1358       {
1359 	  for (col = 0; col < rst->width; col++)
1360 	      *p_out++ = *p_in++;
1361       }
1362 
1363     *buffer = buf;
1364     *buf_size = sz;
1365     return RL2_OK;
1366 }
1367 
1368 RL2_DECLARE int
rl2_raster_data_to_2bit(rl2RasterPtr ptr,unsigned char ** buffer,int * buf_size)1369 rl2_raster_data_to_2bit (rl2RasterPtr ptr, unsigned char **buffer,
1370 			 int *buf_size)
1371 {
1372 /* attempting to export Raster PALETTE/GRAYSCALE 2bit data as a UINT-8 array */
1373     unsigned char *buf;
1374     int sz;
1375     unsigned int row;
1376     unsigned int col;
1377     unsigned char *p_in;
1378     unsigned char *p_out;
1379     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1380 
1381     *buffer = NULL;
1382     *buf_size = 0;
1383     if (rst == NULL)
1384 	return RL2_ERROR;
1385     if (rst->sampleType == RL2_SAMPLE_2_BIT
1386 	&& (rst->pixelType == RL2_PIXEL_PALETTE
1387 	    || rst->pixelType == RL2_PIXEL_GRAYSCALE))
1388 	;
1389     else
1390 	return RL2_ERROR;
1391 
1392     sz = rst->width * rst->height;
1393     buf = malloc (sz);
1394     if (buf == NULL)
1395 	return RL2_ERROR;
1396 
1397     p_in = rst->rasterBuffer;
1398     p_out = buf;
1399     for (row = 0; row < rst->height; row++)
1400       {
1401 	  for (col = 0; col < rst->width; col++)
1402 	      *p_out++ = *p_in++;
1403       }
1404 
1405     *buffer = buf;
1406     *buf_size = sz;
1407     return RL2_OK;
1408 }
1409 
1410 RL2_DECLARE int
rl2_raster_data_to_4bit(rl2RasterPtr ptr,unsigned char ** buffer,int * buf_size)1411 rl2_raster_data_to_4bit (rl2RasterPtr ptr, unsigned char **buffer,
1412 			 int *buf_size)
1413 {
1414 /* attempting to export Raster PALETTE/GRAYSCALE 4bit data as a UINT-8 array */
1415     unsigned char *buf;
1416     int sz;
1417     unsigned int row;
1418     unsigned int col;
1419     unsigned char *p_in;
1420     unsigned char *p_out;
1421     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1422 
1423     *buffer = NULL;
1424     *buf_size = 0;
1425     if (rst == NULL)
1426 	return RL2_ERROR;
1427     if (rst->sampleType == RL2_SAMPLE_4_BIT
1428 	&& (rst->pixelType == RL2_PIXEL_PALETTE
1429 	    || rst->pixelType == RL2_PIXEL_GRAYSCALE))
1430 	;
1431     else
1432 	return RL2_ERROR;
1433 
1434     sz = rst->width * rst->height;
1435     buf = malloc (sz);
1436     if (buf == NULL)
1437 	return RL2_ERROR;
1438 
1439     p_in = rst->rasterBuffer;
1440     p_out = buf;
1441     for (row = 0; row < rst->height; row++)
1442       {
1443 	  for (col = 0; col < rst->width; col++)
1444 	      *p_out++ = *p_in++;
1445       }
1446 
1447     *buffer = buf;
1448     *buf_size = sz;
1449     return RL2_OK;
1450 }
1451 
1452 RL2_DECLARE int
rl2_raster_band_to_uint8(rl2RasterPtr ptr,int band,unsigned char ** buffer,int * buf_size)1453 rl2_raster_band_to_uint8 (rl2RasterPtr ptr, int band, unsigned char **buffer,
1454 			  int *buf_size)
1455 {
1456 /* attempting to export Raster BAND data as a UINT-8 array */
1457     unsigned char *buf;
1458     int sz;
1459     unsigned int row;
1460     unsigned int col;
1461     int nBand;
1462     unsigned char *p_in;
1463     unsigned char *p_out;
1464     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1465 
1466     *buffer = NULL;
1467     *buf_size = 0;
1468     if (rst == NULL)
1469 	return RL2_ERROR;
1470     if (!check_as_band (rst, band, RL2_SAMPLE_UINT8))
1471 	return RL2_ERROR;
1472 
1473     sz = rst->width * rst->height;
1474     buf = malloc (sz);
1475     if (buf == NULL)
1476 	return RL2_ERROR;
1477 
1478     p_in = rst->rasterBuffer;
1479     p_out = buf;
1480     for (row = 0; row < rst->height; row++)
1481       {
1482 	  for (col = 0; col < rst->width; col++)
1483 	    {
1484 		for (nBand = 0; nBand < rst->nBands; nBand++)
1485 		  {
1486 		      if (nBand == band)
1487 			  *p_out++ = *p_in++;
1488 		      else
1489 			  p_in++;
1490 		  }
1491 	    }
1492       }
1493 
1494     *buffer = buf;
1495     *buf_size = sz;
1496     return RL2_OK;
1497 }
1498 
1499 RL2_DECLARE int
rl2_raster_band_to_uint16(rl2RasterPtr ptr,int band,unsigned short ** buffer,int * buf_size)1500 rl2_raster_band_to_uint16 (rl2RasterPtr ptr, int band, unsigned short **buffer,
1501 			   int *buf_size)
1502 {
1503 /* attempting to export Raster BAND data as a UINT-16 array */
1504     unsigned short *buf;
1505     int sz;
1506     unsigned int row;
1507     unsigned int col;
1508     int nBand;
1509     unsigned short *p_in;
1510     unsigned short *p_out;
1511     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1512 
1513     *buffer = NULL;
1514     *buf_size = 0;
1515     if (rst == NULL)
1516 	return RL2_ERROR;
1517     if (!check_as_band (rst, band, RL2_SAMPLE_UINT16))
1518 	return RL2_ERROR;
1519 
1520     sz = rst->width * rst->height * sizeof (unsigned short);
1521     buf = malloc (sz);
1522     if (buf == NULL)
1523 	return RL2_ERROR;
1524 
1525     p_in = (unsigned short *) (rst->rasterBuffer);
1526     p_out = buf;
1527     for (row = 0; row < rst->height; row++)
1528       {
1529 	  for (col = 0; col < rst->width; col++)
1530 	    {
1531 		for (nBand = 0; nBand < rst->nBands; nBand++)
1532 		  {
1533 		      if (nBand == band)
1534 			  *p_out++ = *p_in++;
1535 		      else
1536 			  p_in++;
1537 		  }
1538 	    }
1539       }
1540 
1541     *buffer = buf;
1542     *buf_size = sz;
1543     return RL2_OK;
1544 }
1545 
1546 RL2_DECLARE int
rl2_raster_bands_to_RGB(rl2RasterPtr ptr,int bandR,int bandG,int bandB,unsigned char ** buffer,int * buf_size)1547 rl2_raster_bands_to_RGB (rl2RasterPtr ptr, int bandR, int bandG, int bandB,
1548 			 unsigned char **buffer, int *buf_size)
1549 {
1550 /* attempting to export Raster MULTIBAND data as an RGB array */
1551     unsigned char *buf;
1552     int sz;
1553     unsigned int row;
1554     unsigned int col;
1555     int nBand;
1556     unsigned char *p_in;
1557     unsigned char *p_out;
1558     rl2PrivRasterPtr rst = (rl2PrivRasterPtr) ptr;
1559 
1560     *buffer = NULL;
1561     *buf_size = 0;
1562     if (rst == NULL)
1563 	return RL2_ERROR;
1564     if (!check_as_band (rst, bandR, RL2_SAMPLE_UINT8))
1565 	return RL2_ERROR;
1566     if (!check_as_band (rst, bandG, RL2_SAMPLE_UINT8))
1567 	return RL2_ERROR;
1568     if (!check_as_band (rst, bandB, RL2_SAMPLE_UINT8))
1569 	return RL2_ERROR;
1570 
1571     sz = rst->width * rst->height * 3;
1572     buf = malloc (sz);
1573     if (buf == NULL)
1574 	return RL2_ERROR;
1575 
1576     p_in = rst->rasterBuffer;
1577     p_out = buf;
1578     for (row = 0; row < rst->height; row++)
1579       {
1580 	  for (col = 0; col < rst->width; col++)
1581 	    {
1582 		unsigned char red;
1583 		unsigned char green;
1584 		unsigned char blue;
1585 		for (nBand = 0; nBand < rst->nBands; nBand++)
1586 		  {
1587 		      if (nBand == bandR)
1588 			  red = *p_in;
1589 		      if (nBand == bandG)
1590 			  green = *p_in;
1591 		      if (nBand == bandB)
1592 			  blue = *p_in;
1593 		      p_in++;
1594 		  }
1595 		*p_out++ = red;
1596 		*p_out++ = green;
1597 		*p_out++ = blue;
1598 	    }
1599       }
1600 
1601     *buffer = buf;
1602     *buf_size = sz;
1603     return RL2_OK;
1604 }
1605