void *Rast3d_open_cell_old_no_header(const char *name, const char *mapset) { RASTER3D_Map *map; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; Rast3d_init_defaults(); if (!Rast3d_mask_open_old()) { Rast3d_error(_("Rast3d_open_cell_old_no_header: error in Rast3d_mask_open_old")); return (void *)NULL; } map = Rast3d_malloc(sizeof(RASTER3D_Map)); if (map == NULL) { Rast3d_error(_("Rast3d_open_cell_old_no_header: error in Rast3d_malloc")); return (void *)NULL; } G_unqualified_name(name, mapset, xname, xmapset); map->fileName = G_store(xname); map->mapset = G_store(xmapset); map->data_fd = G_open_old_misc(RASTER3D_DIRECTORY, RASTER3D_CELL_ELEMENT, xname, xmapset); if (map->data_fd < 0) { Rast3d_error(_("Rast3d_open_cell_old_no_header: error in G_open_old")); return (void *)NULL; } Rast3d_range_init(map); Rast3d_mask_off(map); return map; }
int Rast3d_flush_index(RASTER3D_Map * map) { int sizeCompressed, indexLength, tileIndex; unsigned char *tmp; long ldummy; if (!map->hasIndex) return 1; map->indexOffset = lseek(map->data_fd, (long)0, SEEK_END); if (map->indexOffset == -1) { Rast3d_error("Rast3d_flush_index: can't rewind file"); return 0; } map->indexNbytesUsed = Rast3d_long_encode(&(map->indexOffset), (unsigned char *)&ldummy, 1); tmp = Rast3d_malloc(sizeof(long) * map->nTiles); if (tmp == NULL) { Rast3d_error("Rast3d_flush_index: error in Rast3d_malloc"); return 0; } for (tileIndex = 0; tileIndex < map->nTiles; tileIndex++) if (map->index[tileIndex] == -1) map->index[tileIndex] = 0; (void)Rast3d_long_encode(map->index, tmp, map->nTiles); sizeCompressed = Rast3d_rle_count_only(tmp, sizeof(long) * map->nTiles, 1); if (sizeCompressed >= map->nTiles * sizeof(long)) { indexLength = map->nTiles * sizeof(long); if (write(map->data_fd, tmp, indexLength) != indexLength) { Rast3d_error("Rast3d_flush_index: can't write file"); return 0; } } else { indexLength = sizeCompressed; Rast3d_rle_encode(tmp, (char *)map->index, sizeof(long) * map->nTiles, 1); if (write(map->data_fd, map->index, sizeCompressed) != sizeCompressed) { Rast3d_error("Rast3d_flush_index: can't write file"); return 0; } } Rast3d_free(tmp); if (!Rast3d_readIndex(map)) { Rast3d_error("Rast3d_flush_index: error in Rast3d_readIndex"); return 0; } return 1; }
void Rast3d_set_standard3d_input_params() { param = Rast3d_malloc(sizeof(Rast3d_paramType)); param->type = G_define_standard_option(G_OPT_R3_TYPE); param->precision = G_define_standard_option(G_OPT_R3_PRECISION); param->compression = G_define_standard_option(G_OPT_R3_COMPRESSION); param->dimension = G_define_standard_option(G_OPT_R3_TILE_DIMENSION); }
void *Rast3d_alloc_tiles_type(RASTER3D_Map * map, int nofTiles, int type) { void *tiles; tiles = Rast3d_malloc(map->tileSize * Rast3d_length(type) * nofTiles); if (tiles == NULL) { Rast3d_error("Rast3d_alloc_tiles_type: error in Rast3d_malloc"); return NULL; } return tiles; }
static void cache_test_add(void *c, int name, int val) { static int firstTime = 1; if (firstTime) { ctd.value = Rast3d_malloc(((RASTER3D_cache *) c)->eltSize * sizeof(int)); firstTime = 0; } ctd.value[17] = val; ctd.size = ((RASTER3D_cache *) c)->eltSize; Rast3d_cache_load(c, name); }
int Rast3d_init_fp_xdr(RASTER3D_Map * map, int misuseBytes) /* nof addtl bytes allocated for the xdr array so that */ /* the array can also be (mis)used for other purposes */ { int doAlloc; doAlloc = 0; if (xdr == NULL) { xdrLength = map->tileSize * RASTER3D_MAX(map->numLengthExtern, map->numLengthIntern) + misuseBytes; xdr = Rast3d_malloc(xdrLength); if (xdr == NULL) { Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_malloc"); return 0; } doAlloc = 1; } else if (map->tileSize * RASTER3D_MAX(map->numLengthExtern, map->numLengthIntern) + misuseBytes > xdrLength) { xdrLength = map->tileSize * RASTER3D_MAX(map->numLengthExtern, map->numLengthIntern) + misuseBytes; xdr = Rast3d_realloc(xdr, xdrLength); if (xdr == NULL) { Rast3d_error("Rast3d_init_fp_xdr: error in Rast3d_realloc"); return 0; } doAlloc = 1; } if (doAlloc) { xdrmem_create(&(xdrEncodeStream), xdr, (u_int) xdrLength, XDR_ENCODE); xdrmem_create(&(xdrDecodeStream), xdr, (u_int) xdrLength, XDR_DECODE); } return 1; }
void *Rast3d_cache_new(int nofElts, int sizeOfElts, int nofNames, int (*eltRemoveFun) (), void *eltRemoveFunData, int (*eltLoadFun) (), void *eltLoadFunData) { RASTER3D_cache *tmp; int i; tmp = Rast3d_malloc(sizeof(RASTER3D_cache)); if (tmp == NULL) { Rast3d_error("Rast3d_cache_new: error in Rast3d_malloc"); return (void *)NULL; } tmp->hash = NULL; tmp->nofElts = nofElts; tmp->eltSize = sizeOfElts; tmp->elts = Rast3d_malloc(tmp->eltSize * tmp->nofElts); tmp->names = Rast3d_malloc(sizeof(int) * tmp->nofElts); tmp->locks = Rast3d_malloc(tmp->nofElts); tmp->next = Rast3d_malloc(sizeof(int) * tmp->nofElts); tmp->prev = Rast3d_malloc(sizeof(int) * tmp->nofElts); if ((tmp->elts == NULL) || (tmp->names == NULL) || (tmp->locks == NULL) || (tmp->next == NULL) || (tmp->prev == NULL)) { Rast3d_cache_dispose(tmp); Rast3d_error("Rast3d_cache_new: error in Rast3d_malloc"); return (void *)NULL; } /* Init the cache lock */ for(i = 0; i < tmp->nofElts; i++) tmp->locks[i] = 0; tmp->eltRemoveFun = eltRemoveFun; tmp->eltRemoveFunData = eltRemoveFunData; tmp->eltLoadFun = eltLoadFun; tmp->eltLoadFunData = eltLoadFunData; tmp->hash = Rast3d_cache_hash_new(nofNames); if (tmp->hash == NULL) { Rast3d_cache_dispose(tmp); Rast3d_error("Rast3d_cache_new: error in Rast3d_cache_hash_new"); return (void *)NULL; } Rast3d_cache_reset(tmp); return tmp; }
static int Rast3d_readIndex(RASTER3D_Map * map) { unsigned char *tmp, *tmp2; int dummy1, dummy2, indexLength, tileIndex; long indexLast; indexLast = lseek(map->data_fd, (long)0, SEEK_END); if (indexLast == -1) { Rast3d_error("Rast3d_readIndex: can't position file"); return 0; } indexLength = indexLast - map->indexOffset; if (lseek(map->data_fd, map->indexOffset, SEEK_SET) == -1) { Rast3d_error("Rast3d_readIndex: can't position file"); return 0; } tmp = Rast3d_malloc(map->indexLongNbytes * map->nTiles); if (tmp == NULL) { Rast3d_error("Rast3d_readIndex: error in Rast3d_malloc"); return 0; } if (indexLength < map->indexLongNbytes * map->nTiles) { /* RLE encoded? */ if (indexLength > sizeof(long) * map->nTiles) { /*->index large enough? */ tmp2 = Rast3d_malloc(indexLength); if (tmp2 == NULL) { Rast3d_error("Rast3d_readIndex: error in Rast3d_malloc"); return 0; } } else /* YES */ tmp2 = (unsigned char *)map->index; if (read(map->data_fd, tmp2, indexLength) != indexLength) { Rast3d_error("Rast3d_readIndex: can't read file"); return 0; } Rast3d_rle_decode(tmp2, tmp, map->indexLongNbytes * map->nTiles, 1, &dummy1, &dummy2); if (indexLength > sizeof(long) * map->nTiles) Rast3d_free(tmp2); } else /* NO RLE */ if (read(map->data_fd, tmp, indexLength) != indexLength) { Rast3d_error("Rast3d_readIndex: can't read file"); return 0; } Rast3d_long_decode(tmp, map->index, map->nTiles, map->indexLongNbytes); for (tileIndex = 0; tileIndex < map->nTiles; tileIndex++) if (map->index[tileIndex] == 0) map->index[tileIndex] = -1; Rast3d_free(tmp); return 1; }
int Rast3d_init_index(RASTER3D_Map * map, int hasIndex) { int tile; int i0, i1, i2, i3, i4, i5, offset, nofElts; int *offsetP; map->hasIndex = hasIndex; map->index = Rast3d_malloc(sizeof(long) * map->nTiles); map->tileLength = Rast3d_malloc(sizeof(int) * map->nTiles); if ((map->index == NULL) || (map->tileLength == NULL)) { Rast3d_error("Rast3d_init_index: error in Rast3d_malloc"); return 0; } if (map->operation == RASTER3D_WRITE_DATA) { for (tile = 0; tile < map->nTiles; tile++) map->index[tile] = -1; return 1; } if (!map->hasIndex) { offset = 0; for (tile = 0; tile < map->nTiles; tile++) { map->index[tile] = offset * map->numLengthExtern + map->offset; nofElts = Rast3d_compute_clipped_tile_dimensions (map, tile, &i0, &i1, &i2, &i3, &i4, &i5); map->tileLength[tile] = nofElts * map->numLengthExtern; offset += nofElts; } return 1; } if (!Rast3d_readIndex(map)) { Rast3d_error("Rast3d_init_index: error in Rast3d_readIndex"); return 0; } offsetP = Rast3d_malloc(sizeof(int) * map->nTiles); if (offsetP == NULL) { Rast3d_error("Rast3d_init_index: error in Rast3d_malloc"); return 0; } for (tile = 0; tile < map->nTiles; tile++) offsetP[tile] = tile; cmpIndex = map->index; qsort(offsetP, map->nTiles, sizeof(int), indexSortCompare); for (tile = 0; tile < map->nTiles - 1; tile++) { if (map->index[offsetP[tile]] == -1) { map->tileLength[offsetP[tile]] = 0; continue; } map->tileLength[offsetP[tile]] = map->index[offsetP[tile + 1]] - map->index[offsetP[tile]]; } if (map->index[offsetP[map->nTiles - 1]] == -1) map->tileLength[offsetP[map->nTiles - 1]] = 0; else map->tileLength[offsetP[map->nTiles - 1]] = map->indexOffset - map->index[offsetP[map->nTiles - 1]]; Rast3d_free(offsetP); return 1; }
void *Rast3d_open_cell_old(const char *name, const char *mapset, RASTER3D_Region * window, int typeIntern, int cache) { RASTER3D_Map *map; int proj, zone; int compression, useRle, useLzw, type, tileX, tileY, tileZ; int rows, cols, depths, precision; double ew_res, ns_res, tb_res; int nofHeaderBytes, dataOffset, useXdr, hasIndex; char *ltmp, *unit; double north, south, east, west, top, bottom; map = Rast3d_open_cell_old_no_header(name, mapset); if (map == NULL) { Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_open_cell_old_no_header")); return (void *)NULL; } if (lseek(map->data_fd, (long)0, SEEK_SET) == -1) { Rast3d_error(_("Rast3d_open_cell_old: can't rewind file")); return (void *)NULL; } if (!Rast3d_read_header(map, &proj, &zone, &north, &south, &east, &west, &top, &bottom, &rows, &cols, &depths, &ew_res, &ns_res, &tb_res, &tileX, &tileY, &tileZ, &type, &compression, &useRle, &useLzw, &precision, &dataOffset, &useXdr, &hasIndex, &unit)) { Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_read_header")); return 0; } if (window == RASTER3D_DEFAULT_WINDOW) window = Rast3d_window_ptr(); if (proj != window->proj) { Rast3d_error(_("Rast3d_open_cell_old: projection does not match window projection")); return (void *)NULL; } if (zone != window->zone) { Rast3d_error(_("Rast3d_open_cell_old: zone does not match window zone")); return (void *)NULL; } map->useXdr = useXdr; if (hasIndex) { /* see RASTER3D_openCell_new () for format of header */ if ((!Rast3d_read_ints(map->data_fd, map->useXdr, &(map->indexLongNbytes), 1)) || (!Rast3d_read_ints(map->data_fd, map->useXdr, &(map->indexNbytesUsed), 1))) { Rast3d_error(_("Rast3d_open_cell_old: can't read header")); return (void *)NULL; } /* if our long is to short to store offsets we can't read the file */ if (map->indexNbytesUsed > sizeof(long)) Rast3d_fatal_error(_("Rast3d_open_cell_old: index does not fit into long")); ltmp = Rast3d_malloc(map->indexLongNbytes); if (ltmp == NULL) { Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_malloc")); return (void *)NULL; } /* convert file long to long */ if (read(map->data_fd, ltmp, map->indexLongNbytes) != map->indexLongNbytes) { Rast3d_error(_("Rast3d_open_cell_old: can't read header")); return (void *)NULL; } Rast3d_long_decode(ltmp, &(map->indexOffset), 1, map->indexLongNbytes); Rast3d_free(ltmp); } nofHeaderBytes = dataOffset; if (typeIntern == RASTER3D_TILE_SAME_AS_FILE) typeIntern = type; if (!Rast3d_fill_header(map, RASTER3D_READ_DATA, compression, useRle, useLzw, type, precision, cache, hasIndex, map->useXdr, typeIntern, nofHeaderBytes, tileX, tileY, tileZ, proj, zone, north, south, east, west, top, bottom, rows, cols, depths, ew_res, ns_res, tb_res, unit)) { Rast3d_error(_("Rast3d_open_cell_old: error in Rast3d_fill_header")); return (void *)NULL; } Rast3d_region_copy(&(map->window), window); Rast3d_adjust_region(&(map->window)); Rast3d_get_nearest_neighbor_fun_ptr(&(map->resampleFun)); return map; }
void *Rast3d_open_cell_new(const char *name, int typeIntern, int cache, RASTER3D_Region * region) { RASTER3D_Map *map; int nofHeaderBytes, dummy = 0, compression, precision; long ldummy = 0; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; Rast3d_init_defaults(); if (!Rast3d_mask_open_old()) { Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_mask_open_old")); return (void *)NULL; } compression = g3d_do_compression; precision = g3d_precision; map = Rast3d_malloc(sizeof(RASTER3D_Map)); if (map == NULL) { Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_malloc")); return (void *)NULL; } if (G_unqualified_name(name, G_mapset(), xname, xmapset) < 0) { G_warning(_("map <%s> is not in the current mapset"), name); return (void *)NULL; } map->fileName = G_store(xname); map->mapset = G_store(xmapset); map->tempName = G_tempfile(); map->data_fd = open(map->tempName, O_RDWR | O_CREAT | O_TRUNC, 0666); if (map->data_fd < 0) { Rast3d_error(_("Rast3d_open_cell_new: could not open file")); return (void *)NULL; } Rast3d_make_mapset_map_directory(map->fileName); map->useXdr = RASTER3D_USE_XDR; if (g3d_file_type == FCELL_TYPE) { if (precision > 23) precision = 23; /* 32 - 8 - 1 */ else if (precision < -1) precision = 0; } else if (precision > 52) precision = 52; /* 64 - 11 - 1 */ else if (precision < -1) precision = 0; /* no need to write trailing zeros */ if ((typeIntern == FCELL_TYPE) && (g3d_file_type == DCELL_TYPE)) { if (precision == -1) precision = 23; else precision = RASTER3D_MIN(precision, 23); } if (compression == RASTER3D_NO_COMPRESSION) precision = RASTER3D_MAX_PRECISION; if (compression == RASTER3D_COMPRESSION) map->useXdr = RASTER3D_USE_XDR; if (RASTER3D_HAS_INDEX) { map->indexLongNbytes = sizeof(long); /* at the beginning of the file write */ /* nof bytes of "long" */ /* max nof bytes used for index */ /* position of index in file */ /* the index is appended at the end of the file at closing time. since */ /* we do not know this position yet we write dummy values */ if ((!Rast3d_write_ints(map->data_fd, map->useXdr, &(map->indexLongNbytes), 1)) || (!Rast3d_write_ints(map->data_fd, map->useXdr, &dummy, 1))) { Rast3d_error(_("Rast3d_open_cell_new: can't write header")); return (void *)NULL; } if (write(map->data_fd, &ldummy, map->indexLongNbytes) != map->indexLongNbytes) { Rast3d_error(_("Rast3d_open_cell_new: can't write header")); return (void *)NULL; } } /* can't use a constant since this depends on sizeof (long) */ nofHeaderBytes = lseek(map->data_fd, (long)0, SEEK_CUR); Rast3d_range_init(map); Rast3d_adjust_region(region); if (!Rast3d_fill_header(map, RASTER3D_WRITE_DATA, compression, g3d_do_rle_compression, g3d_do_lzw_compression, g3d_file_type, precision, cache, RASTER3D_HAS_INDEX, map->useXdr, typeIntern, nofHeaderBytes, g3d_tile_dimension[0], g3d_tile_dimension[1], g3d_tile_dimension[2], region->proj, region->zone, region->north, region->south, region->east, region->west, region->top, region->bottom, region->rows, region->cols, region->depths, region->ew_res, region->ns_res, region->tb_res, g3d_unit_default)) { Rast3d_error(_("Rast3d_open_cell_new: error in Rast3d_fill_header")); return (void *)NULL; } /*Set the map window to the map region */ Rast3d_region_copy(&(map->window), region); /*Set the resampling function to nearest neighbor for data access */ Rast3d_get_nearest_neighbor_fun_ptr(&(map->resampleFun)); Rast3d_mask_off(map); return (void *)map; }