CodeSnippets: rgb2pct.c

File rgb2pct.c, 5.8 kB (added by rouault, 6 months ago)

C Port of rgb2pct.py

Line 
1 /* Port of rgb2pct.py */
2
3 /*
4 #******************************************************************************
5 #  $Id: rgb2pct.c 6219 2004-06-01 13:24:09Z warmerda $
6 #
7 #  Name:     rgb2pct
8 #  Project:  GDAL Python Interface
9 #  Purpose:  Application for converting an RGB image to a pseudocolored image.
10 #  Author:   Frank Warmerdam, warmerdam@pobox.com
11 #
12 #******************************************************************************
13 #  Copyright (c) 2001, Frank Warmerdam
14 #  Copyright (c) 2007, Even Rouault
15 #
16 #  Permission is hereby granted, free of charge, to any person obtaining a
17 #  copy of this software and associated documentation files (the "Software"),
18 #  to deal in the Software without restriction, including without limitation
19 #  the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 #  and/or sell copies of the Software, and to permit persons to whom the
21 #  Software is furnished to do so, subject to the following conditions:
22 #
23 #  The above copyright notice and this permission notice shall be included
24 #  in all copies or substantial portions of the Software.
25 #
26 #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 #  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 #  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 #  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 #  DEALINGS IN THE SOFTWARE.
33 #******************************************************************************
34 */
35
36 #include "gdal.h"
37 #include "gdal_alg.h"
38 #include "cpl_port.h"
39 #include "cpl_string.h"
40 #include "tiffio.h"
41
42 int main(int argc, char* argv[])
43 {
44     int i = 0;
45     char* format = "GTiff";
46     int nColors = 256;
47     char* src_filename = NULL;
48     char* dst_filename = NULL;
49     char  **papszCreateOptions = NULL;
50
51     for(i = 1; i < argc; i++)
52     {
53         if (strcmp(argv[i], "-of") == 0)
54         {
55             format = argv[i+1];
56             i++;
57         }
58         else if (strcmp(argv[i], "-n") == 0)
59         {
60             nColors = atoi(argv[i+1]);
61             i++;
62         }
63         else if( EQUAL(argv[i],"-co") && i < argc-1 )
64         {
65             papszCreateOptions = CSLAddString( papszCreateOptions, argv[++i] );
66         }
67         else if (src_filename == NULL)
68         {
69             src_filename = argv[i];
70         }
71         else if (dst_filename == NULL)
72         {
73             dst_filename = argv[i];
74         }
75         else
76         {
77             printf("Usage: rgb2pct [-n colors] [-of format] [-co \"NAME=VALUE\"]* source_file dest_file\n");
78             return 1;
79         }
80     }
81    
82     if (dst_filename == NULL)
83     {
84         printf("Usage: rgb2pct [-n colors] [-of format] [-co \"NAME=VALUE\"]* source_file dest_file\n");
85         return 1;
86     }
87    
88     GDALAllRegister();
89    
90     GDALDatasetH ds = GDALOpen(src_filename, GA_ReadOnly);
91     if (ds == NULL)
92     {
93         return 1;
94     }
95    
96     if (GDALGetRasterCount(ds) < 3)
97     {
98         fprintf(stderr, "%s has %d bands, need 3 for inputs red, green and blue.\n",
99                 src_filename, GDALGetRasterCount(ds));
100         return 1;
101     }
102    
103     GDALDriverH dst_driver = GDALGetDriverByName(format);
104     if (dst_driver == NULL)
105     {
106         fprintf(stderr, "%s driver is not registered\n", format);
107         return 1;
108     }
109    
110     GDALColorTableH color_table = GDALCreateColorTable(GPI_RGB);
111    
112     /* Generate histogram */
113     GDALComputeMedianCutPCT( GDALGetRasterBand(ds, 1),
114                             GDALGetRasterBand(ds, 2),
115                             GDALGetRasterBand(ds, 3),
116                             NULL,
117                             nColors,
118                             color_table,
119                             GDALTermProgress,
120                             NULL);
121    
122     // Create the working file.  We have to use TIFF since there are few formats
123     // that allow setting the color table after creation.
124     
125     char* tif_filename;
126     if (strcmp(format, "GTiff") == 0)
127     {
128         tif_filename = dst_filename;
129     }
130     else
131     {
132         tif_filename = "temp.tif";
133     }
134    
135     GDALDriverH gtiff_driver = GDALGetDriverByName("GTiff");
136     GDALDatasetH tif_ds = GDALCreate( gtiff_driver, tif_filename,
137                                       GDALGetRasterXSize(ds), GDALGetRasterYSize(ds), 1, GDT_Byte,
138                                       (strcmp(format, "GTiff") == 0) ? papszCreateOptions : NULL);
139
140     // We should copy projection information and so forth at this point.
141     GDALSetProjection(tif_ds, GDALGetProjectionRef(ds));
142     double adfGeoTransform[6];
143     GDALGetGeoTransform(ds, adfGeoTransform);
144     GDALSetGeoTransform(tif_ds, adfGeoTransform);
145
146     GDALSetRasterColorTable(GDALGetRasterBand(tif_ds, 1), color_table);
147    
148     // Actually transfer and dither the data.
149     printf("Dither:");
150     GDALDitherRGB2PCT( GDALGetRasterBand(ds, 1),
151                     GDALGetRasterBand(ds, 2),
152                     GDALGetRasterBand(ds, 3),
153                     GDALGetRasterBand(tif_ds, 1),
154                     color_table,
155                     GDALTermProgress,
156                     NULL);
157
158     GDALClose(tif_ds);
159
160
161     if (strcmp(format, "GTiff") != 0)
162     {
163         tif_ds = GDALOpen(tif_filename, GA_ReadOnly);
164         printf("Copy to %s:", format);
165         GDALDatasetH dst_ds = GDALCreateCopy( dst_driver, dst_filename, tif_ds, FALSE, papszCreateOptions, GDALTermProgress, NULL);
166         if (dst_ds)
167             GDALClose(dst_ds);
168         GDALClose(tif_ds);
169         GDALDeleteDataset(gtiff_driver, tif_filename);
170     }
171    
172     GDALDestroyColorTable(color_table);
173
174     GDALClose(ds);
175
176     GDALDumpOpenDatasets( stderr );
177
178     GDALDestroyDriverManager();
179    
180     CSLDestroy(papszCreateOptions);
181
182     return 0;
183 }