File: makegeo.c

package info (click to toggle)
libgeotiff 1.7.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,916 kB
  • sloc: ansic: 8,252; sh: 4,189; makefile: 164
file content (97 lines) | stat: -rw-r--r-- 2,642 bytes parent folder | download | duplicates (2)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * makegeo.c -- example client code for LIBGEO geographic
 *     TIFF tag support.
 *
 *  Author: Niles D. Ritter
 *
 * Revision History:
 *   31 October, 1995    Fixed reversed lat-long coordinates   NDR
 *
 */

#include "geotiffio.h"
#include "xtiffio.h"
#include <stdlib.h>
#include <string.h>

void SetUpTIFFDirectory(TIFF *tif);
void SetUpGeoKeys(GTIF *gtif);
void WriteImage(TIFF *tif);

#define WIDTH 20L
#define HEIGHT 20L

int main()
{
	const char *fname = "newgeo.tif";

	TIFF *tif=XTIFFOpen(fname,"w");  /* TIFF-level descriptor */
	if (!tif) {
		printf("failure in makegeo\n");
		return -1;
	}

	GTIF *gtif = GTIFNew(tif);  /* GeoKey-level descriptor */
	if (!gtif)
	{
		printf("failure in makegeo\n");
		printf("failed in GTIFNew\n");
		TIFFClose(tif);
		return -1;
	}

	SetUpTIFFDirectory(tif);
	SetUpGeoKeys(gtif);
	WriteImage(tif);

	GTIFWriteKeys(gtif);
	GTIFFree(gtif);
	XTIFFClose(tif);
	return 0;
}


void SetUpTIFFDirectory(TIFF *tif)
{
	TIFFSetField(tif,TIFFTAG_IMAGEWIDTH,    WIDTH);
	TIFFSetField(tif,TIFFTAG_IMAGELENGTH,   HEIGHT);
	TIFFSetField(tif,TIFFTAG_COMPRESSION,   COMPRESSION_NONE);
	TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,   PHOTOMETRIC_MINISBLACK);
	TIFFSetField(tif,TIFFTAG_PLANARCONFIG,  PLANARCONFIG_CONTIG);
	TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE, 8);
	TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP,  20L);

	const double tiepoints[6]={0,0,0,130.0,32.0,0.0};
	const double pixscale[3]={1,1,0};
	TIFFSetField(tif,TIFFTAG_GEOTIEPOINTS, 6,tiepoints);
	TIFFSetField(tif,TIFFTAG_GEOPIXELSCALE, 3,pixscale);
}

void SetUpGeoKeys(GTIF *gtif)
{
	GTIFKeySet(gtif, GTModelTypeGeoKey, TYPE_SHORT, 1, ModelGeographic);
	GTIFKeySet(gtif, GTRasterTypeGeoKey, TYPE_SHORT, 1, RasterPixelIsArea);
	GTIFKeySet(gtif, GTCitationGeoKey, TYPE_ASCII, 0, "Just An Example");
	GTIFKeySet(gtif, GeographicTypeGeoKey, TYPE_SHORT,  1, KvUserDefined);
	GTIFKeySet(gtif, GeogCitationGeoKey, TYPE_ASCII, 0, "Everest Ellipsoid Used.");
	GTIFKeySet(gtif, GeogAngularUnitsGeoKey, TYPE_SHORT,  1, Angular_Degree);
	GTIFKeySet(gtif, GeogLinearUnitsGeoKey, TYPE_SHORT,  1, Linear_Meter);
	GTIFKeySet(gtif, GeogGeodeticDatumGeoKey, TYPE_SHORT,     1, KvUserDefined);
	GTIFKeySet(gtif, GeogEllipsoidGeoKey, TYPE_SHORT,     1, Ellipse_Everest_1830_1967_Definition);
	GTIFKeySet(gtif, GeogSemiMajorAxisGeoKey, TYPE_DOUBLE, 1, (double)6377298.556);
	GTIFKeySet(gtif, GeogInvFlatteningGeoKey, TYPE_DOUBLE, 1, (double)300.8017);
}

void WriteImage(TIFF *tif)
{
	char buffer[WIDTH];

	memset(buffer,0,(size_t)WIDTH);
	for (int i=0;i<HEIGHT;i++)
		if (!TIFFWriteScanline(tif, buffer, i, 0))
			TIFFError("WriteImage","failure in WriteScanline\n");
}