imagerylib.dox 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*! \page imagerylib GRASS Imagery Library
  2. <!-- doxygenized from "GRASS 5 Programmer's Manual"
  3. by M. Neteler 2/2004
  4. -->
  5. \section imageryintro Introduction to Imagery Library
  6. The <I>Imagery Library</I> was created for version 3.0 of GRASS to
  7. support integrated image processing directly in GRASS. It contains
  8. routines that provide access to the <I>group</I> database structure
  9. which was also introduced in GRASS 3.0 for the same purpose. It is
  10. assumed that the reader has read Database_Structure for a general
  11. description of GRASS databases, \ref Image_Data_Groups for a description of
  12. imagery groups, and \ref Raster_Maps for details about map layers in
  13. GRASS. The routines in the \ref Imagery_Library are presented in functional
  14. groupings, rather than in alphabetical order. The order of
  15. presentation will, it is hoped, provide a better understanding of how
  16. the library is to be used, as well as show the interrelationships
  17. among the various routines. Note that a good way to understand how to
  18. use these routines is to look at the source code for GRASS modules
  19. which use them. Most routines in this library require that the header
  20. file <grass/imagery.h> be included in any code using these
  21. routines. Therefore, programmers should always include this file when
  22. writing code using routines from this library:
  23. \verbatim
  24. #include <grass/imagery.h>
  25. \endverbatim
  26. <P>
  27. <B>Note.</B> All routines and global variables in this library,
  28. documented or undocumented, start with the prefix <B>I_.</B> To avoid
  29. name conflicts, programmers should not create variables or routines in
  30. their own modules which use this prefix.
  31. \subsection Group_Processing Group Processing
  32. The group is the key database structure which permits integration of
  33. image processing in GRASS.
  34. <P>
  35. \subsection Prompting_for_a_Group Prompting for a Group
  36. <P>
  37. The following routines interactively prompt the user for a group name
  38. in the current mapset (This library only works with groups in the
  39. current mapset. Other mapsets, even those in the user's mapset search
  40. path, are ignored.) In each, the <B>prompt</B> string will be printed
  41. as the first line of the full prompt which asks the user to enter a
  42. group name. If <B>prompt</B> is the empty string "", then an
  43. appropriate prompt will be substituted. The name that the user enters
  44. is copied into the <B>group</B> buffer. The size of group should be
  45. large enough to hold any GRASS file name. Most systems allow file
  46. names to be quite long. It is recommended that name be declared
  47. <tt>char group</tt>. These routines have a built-in 'list' capability
  48. which allows the user to get a list of existing groups.
  49. <P>
  50. The user is required to enter a valid group name, or else hit the
  51. RETURN key to cancel the request. If the user enters an invalid
  52. response, a message is printed, and the user is prompted again. If the
  53. user cancels the request, 0 is returned; otherwise, 1 is returned.
  54. <P>
  55. int I_ask_group_old() prompt for an existing group
  56. Asks the user to enter the name of an existing <B>group</B> in the
  57. current mapset.
  58. <P>
  59. <B>Note.</B> The user is not warned if the <B>group</B> exists. The
  60. programmer should use <I>I_find_group()</I> to determine if the
  61. <B>group</B> exists.
  62. <P>
  63. Here is an example of how to use these routines. Note that the
  64. programmer must handle the 0 return properly:
  65. \verbatim
  66. char group[INAME_LEN];
  67. if ( ! I_ask_group_any ("Enter group to be processed", group))
  68. exit(0);
  69. \endverbatim
  70. <P>
  71. \subsection Finding_Groups_in_the_Database Finding Groups in the Database
  72. <P>
  73. Sometimes it is necessary to determine if a given group already
  74. exists. The following routine provides this service:
  75. <P>
  76. int I_find_group() does group exist?
  77. Returns 1 if the specified <B>group</B> exists in the current mapset;
  78. 0 otherwise.
  79. \subsection REF_File REF File
  80. <P>
  81. These routines provide access to the information contained in the REF
  82. file for groups and subgroups, as well as routines to update this
  83. information. They use the <I>Ref</I> structure, which is defined in
  84. the <grass/imagery.h> header file; see \ref Imagery_Library_Data_Structures.
  85. <P>
  86. The contents of the REF file are read or updated by the following
  87. routines:
  88. <P>
  89. int I_get_group_ref() read group REF file
  90. Reads the contents of the REF file for the specified <B>group</B> into
  91. the <B>ref</B> structure.
  92. <P>
  93. Returns 1 if successful; 0 otherwise (but no error messages are printed).
  94. <P>
  95. int I_put_group_ref() write group REF file
  96. Writes the contents of the <B>ref</B> structure to the REF file for
  97. the specified <B>group.</B>
  98. <P>
  99. Returns 1 if successful; 0 otherwise (and prints a diagnostic error).
  100. <P>
  101. <B>Note.</B> This routine will create the <B>group</B>, if it does not
  102. already exist.
  103. <P>
  104. int I_get_subgroup_ref() read subgroup REF file
  105. Reads the contents of the REF file for the specified <B>subgroup</B>
  106. of the specified <B>group</B> into the <B>ref</B> structure.
  107. <P>
  108. Returns 1 if successful; 0 otherwise (but no error messages are printed).
  109. <P>
  110. int I_put_subgroup_ref() write subgroup REF file
  111. Writes the contents of the <B>ref</B> structure into the REF file for
  112. the specified <B>subgroup</B> of the specified <B>group.</B>
  113. <P>
  114. Returns 1 if successful; 0 otherwise (and prints a diagnostic error).
  115. <P>
  116. <B>Note.</B> This routine will create the <B>subgroup</B>, if it does
  117. not already exist.
  118. <P>
  119. These next routines manipulate the <I>Ref</I> structure:
  120. <P>
  121. int I_init_group_ref() initialize Ref structure
  122. This routine initializes the <B>ref</B> structure for other library
  123. calls which require a <I>Ref</I> structure. This routine must be
  124. called before any use of the structure can be made.
  125. <P>
  126. <B>Note.</B> The routines I_get_group_ref() and I_get_subgroup_ref() call
  127. this routine automatically.
  128. <P>
  129. int I_add_file_to_group_ref() add file name to Ref structure
  130. This routine adds the file <B>name</B> and <B>mapset</B> to the list
  131. contained in the <B>ref</B> structure, if it is not already in the
  132. list. The <B>ref</B> structure must have been properly
  133. initialized. This routine is used by programs, such as
  134. <I>i.maxlik</I>, to add to the group new raster files created from
  135. files already in the group.
  136. <P>
  137. Returns the index into the <I>file</I> array within the <B>ref</B>
  138. structure for the file after insertion; see \ref
  139. Imagery_Library_Data_Structures.
  140. <P>
  141. int I_transfer_group_ref_file() copy Ref lists
  142. This routine is used to copy file names from one <I>Ref</I> structure
  143. to another. The name and mapset for file <B>n</B> from the <B>src</B>
  144. structure are copied into the <B>dst</B> structure (which must be
  145. properly initialized).
  146. <P>
  147. For example, the following code copies one <I>Ref</I> structure to another:
  148. \verbatim
  149. struct Ref src,dst;
  150. int n;
  151. /* some code to get information into src */
  152. ...
  153. I_init_group_ref(&dst);
  154. for (n = 0; n < src.nfiles; n++)
  155. I_transfer_group_ref_file (&src, n, &dst);
  156. \endverbatim
  157. <P>
  158. This routine is used by <I>g.gui.gcp</I> to create the REF file for a
  159. subgroup.
  160. <P>
  161. int I_free_group_ref() free Ref structure
  162. This routine frees memory allocated to the <B>ref</B> structure.
  163. \subsection TARGET_File TARGET File
  164. <P>
  165. The following two routines read and write the TARGET file.
  166. <P>
  167. int I_get_target() read target information
  168. Reads the target <B>location</B> and <B>mapset</B> from the TARGET
  169. file for the specified group. Returns 1 if successful; 0 otherwise
  170. (and prints a diagnostic error). This routine is used by
  171. <I>g.gui.gcp</I> and <I>i.rectify</I> and probably should not be used
  172. by other programs.
  173. <P>
  174. <B>Note.</B> This routine does <B>not</B> validate the target information.
  175. <P>
  176. int I_put_target() write target information
  177. Writes the target <B>location</B> and <B>mapset</B> to the TARGET file
  178. for the specified <B>group.</B> Returns 1 if successful; 0 otherwise
  179. (but no error messages are printed).
  180. <P>
  181. This routine is used by <I>i.target</I> and probably should not be
  182. used by other programs.
  183. <P>
  184. <B>Note.</B> This routine does <B>not</B> validate the target
  185. information.
  186. \subsection POINTS_File POINTS File
  187. <P>
  188. The following routines read and write the POINTS file, which contains
  189. the image registration control points. This file is created and
  190. updated by the module <I>g.gui.gcp</I>,and read by <I>i.rectify.</I>
  191. <P>
  192. These routines use the <I>Control_Points</I> structure, which is
  193. defined in the <grass/imagery.h> <I>header file</I>; see \ref
  194. Imagery_Library_Data_Structures.
  195. <P>
  196. <B>Note.</B> The interface to the <I>Control_Points</I> structure
  197. provided by the routines below is incomplete. A routine to initialize
  198. the structure is needed.
  199. <P>
  200. int I_get_control_points() read group control points
  201. Reads the control points from the POINTS file for the <B>group</B>
  202. into the <B>cp</B> structure. Returns 1 if successful; 0 otherwise
  203. (and prints a diagnostic error).
  204. <P>
  205. <B>Note.</B> An error message is printed if the POINTS file is
  206. invalid, or does not exist.
  207. <P>
  208. int I_new_control_point() add new control point
  209. Once the control points have been read into the <B>cp</B> structure,
  210. this routine adds new points to it. The new control point is given by
  211. <B>e1</B> (column) and <B>n1</B> (row) on the image, and the <B>e2</B>
  212. (east) and <B>n2</B> (north) for the target database. The value of
  213. <B>status</B> should be 1 if the point is a valid point; 0
  214. otherwise.Use of this routine implies that the point is probably good,
  215. so status should be set to 1.
  216. <P>
  217. int I_put_control_points() write group control points
  218. Writes the control points from the <B>cp</B> structure to the POINTS
  219. file for the specified group.
  220. <P>
  221. <B>Note.</B> Points in <B>cp</B> with a negative <I>status</I> are not
  222. written to the POINTS file.
  223. \subsection Loading_the_Imagery_Library Loading the Imagery Library
  224. <P>
  225. The library is loaded by specifying $(IMAGERYLIB) in the
  226. Makefile. The following example is a complete Makefile which
  227. compiles code that uses this library:
  228. <P>
  229. <B>Makefile for $(IMAGERYLIB)</B>
  230. \verbatim
  231. #UPDATE THIS EXAMPLE!!
  232. OBJ = main.o sub1.o sub2.o
  233. PGM: $(OBJ) $(IMAGERYLIB) $(GISLIB)
  234. $(CC) $(LDFLAGS) -o $@ $(OBJ) $(IMAGERYLIB) $(GISLIB)
  235. $(IMAGERYLIB): # in case the library changes
  236. $(GISLIB): # in case the library changes
  237. \endverbatim
  238. <B>Note.</B> This library must be loaded with $(GISLIB) since it uses
  239. routines from that library. See \ref GIS_Library or details on that
  240. library. See \ref Compiling_and_Installing_GRASS_Modules for a complete
  241. discussion of Makefiles.
  242. \section Imagery_Library_Data_Structures Imagery Library Data Structures
  243. Some of the data structures in the <grass/imagery.h> header file are
  244. described below.
  245. \subsection struct_Ref struct Ref
  246. <P>
  247. The <I>Ref</I> structure is used to hold the information from the REF
  248. file for groups and subgroups. The structure is:
  249. \verbatim
  250. struct Ref
  251. {
  252. int nfiles; /* number of REF files */
  253. struct Ref_Files
  254. {
  255. char name[INAME_LEN]; /* REF file name */
  256. char mapset[INAME_LEN]; /* REF file mapset */
  257. } *file;
  258. struct Ref_Color
  259. {
  260. unsigned char *table; /* color table for min-max values */
  261. unsigned char *index; /* data translation index */
  262. unsigned char *buf; /* data buffer for reading color file */
  263. int fd; /* for image i/o */
  264. CELL min, max; /* min,max CELL values */
  265. int n; /* index into Ref_Files */
  266. } red, grn, blu;
  267. };
  268. \endverbatim
  269. The <I>Ref</I> structure has <I>nfiles</I> (the number of raster
  270. files), <I>file</I> (the name and mapset of each file), and
  271. <I>red,grn,blu</I> (color information for the group or subgroup) Note:
  272. The red,grn,blu elements are expected to change as the imagery code
  273. develops. Do not reference them. Pretend they do not exist.
  274. <P>
  275. There is no function interface to the <I>nfiles</I> and <I>file</I>
  276. elements in the structure. This means that the programmer must
  277. reference the elements of the structure directly (The nfiles and file
  278. elements are not expected to change in the future). The name and
  279. <I>mapset for the i th file are file[i].name, and file[i].mapset.</I>
  280. <P>
  281. For example, to print out the names of the raster files in the structure:
  282. \verbatim
  283. int i;
  284. struct Ref ref;
  285. ...
  286. /* some code to get the REF file for a group into <B>ref</B> */
  287. ...
  288. for (i = 0; i<ref.nfiles; i++)
  289. fprintf(stdout, "%s in %s\n", ref.file[i].name, ref.file[i].mapset);
  290. \endverbatim
  291. \subsection struct_Control_Points struct Control_Points
  292. The <I>Control_Points</I> structure is used to hold the control points
  293. from the group POINTS file. The structure is:
  294. \verbatim
  295. struct
  296. Control_Points
  297. {
  298. int count; /* number of control points */
  299. double *e1; /* image east (column) */
  300. double *n1; /* image north (row) */
  301. double *e2; /* target east */
  302. double *n2; /* target north */
  303. int *status; /* status of control point */
  304. };
  305. \endverbatim
  306. The number of control points is <I>count.</I>
  307. <P>
  308. Control point <I>i</I> is <I>e1</I> [i], <I>n1</I> [i], <I>e2</I> [i],
  309. <I>n2</I> [i], and its status is <I>status</I> [i].
  310. */