| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 123 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
} |
|---|