Bool32 CTB_GetRecRaster(const char *fname, int32_t id_rast, RecRaster *r) { uchar raster[REC_MAX_RASTER_SIZE], w, h, data[CTB_DATA_SIZE]; int32_t wb, ww, hh; Bool32 ret; CTB_handle hnd; if (!CTB_open(fname, &hnd, "w")) return FALSE; ret = CTB_read(&hnd, id_rast, raster, data); CTB_close(&hnd); if (ret) { w = data[1]; h = data[2]; ww = w; hh = h; r->lnRasterBufSize = REC_MAX_RASTER_SIZE; r->lnPixWidth = ww; r->lnPixHeight = hh; if (hnd.signums == (CTB_GRAY_SCALE | CTB_PLANE)) { memcpy(r->Raster, raster, ww * hh); } else { wb = (w + 7) / 8; memcpy(r->Raster, raster, wb * hh); CTB_align8_lines(r->Raster, r->lnPixWidth, r->lnPixHeight); } } return ret; }
Bool32 CTB_ReadRecRaster(CTB_handle *hnd, int32_t num, uchar *let, RecRaster *r, uchar *data) { uchar raster[REC_MAX_RASTER_SIZE], w, h; int32_t wb, ww, hh; Bool32 ret; ret = CTB_read(hnd, num, raster, data); if (ret) { w = data[1]; h = data[2]; ww = w; hh = h; r->lnRasterBufSize = REC_MAX_RASTER_SIZE; r->lnPixWidth = ww; r->lnPixHeight = hh; *let = data[3]; if (hnd->signums == (CTB_GRAY_SCALE | CTB_PLANE)) { memcpy(r->Raster, raster, ww * hh); } else { wb = (w + 7) / 8; memcpy(r->Raster, raster, wb * hh); CTB_align8_lines(r->Raster, r->lnPixWidth, r->lnPixHeight); } } return ret; }
////////////////// /////////////////////// // return == 0 - error static int GetCTBasWelet(CTB_handle *CTBhandle, int num, welet *wel) { uchar CTBdata[CTB_DATA_SIZE]; uint16_t *pword16; int16_t *pint16; uint32_t *pword32; if (CTB_read(CTBhandle, num, wel->raster, CTBdata) == FALSE) return 0; wel->let = CTBdata[3]; // in ASCII wel->w = CTBdata[4]; wel->h = CTBdata[5]; wel->weight = CTBdata[6]; // how many symbols make wel->porog = CTBdata[7]; // threshold wel->mw = CTBdata[8]; // medium width wel->mh = CTBdata[9]; // height wel->prob = CTBdata[10]; // for CTB - probability wel->attr = CTBdata[11]; // now put words pword16 = (uint16_t *) (CTBdata + 12); wel->fill = pword16[0]; wel->num = pword16[1]; wel->invalid = pword16[2]; wel->valid = CTBdata[18]; // for CTB - validity wel->kegl = CTBdata[19]; // now short int's pint16 = (int16_t *) (CTBdata + 20); wel->sr_col = pint16[0]; wel->sr_row = pint16[1]; // now dword pword32 = (uint32_t *) (CTBdata + 24); wel->summa = pword32[0]; SetFields(wel->fields,(pword32+1)); pint16 = (int16_t *) (CTBdata + 28 + NFIELDDWORD * sizeof(uint32_t)); wel->nInCTB = pint16[0]; pword32 = (uint32_t *) (pint16 + 1); wel->tablColumn = pword32[0]; return 1; }
int32_t CTB_copy(char *new_name, char *old_name) { char *p; CTB_handle hi, ho; int16_t i, n; uchar dst[CTB_DATA_SIZE], buffer[256* 128 + 2 + CTB_DATA_SIZE ]; ctb_err_code = CTB_ERR_NONE; p = ctb_last_punct(old_name); if (p) *p = '\0'; p = ctb_last_punct(new_name); if (p) *p = '\0'; if (!CTB_open(old_name, &hi, "w")) return 0; CTB_read_global_data(&hi, dst); CTB_files_init(new_name, dst, hi.width, hi.height, hi.dpb, hi.signums, hi.attr_size); n = (int16_t) CTB_volume(&hi); if (!CTB_open(new_name, &ho, "w")) return 0; for (i = 0; i < n; i++) { switch (CTB_read(&hi, i, buffer, dst)) { case 1: CTB_write_mark(&ho, -1, buffer, dst, FALSE); break; case 2: CTB_write_mark(&ho, -1, buffer, dst, TRUE); break; default: break; } } CTB_close(&ho); CTB_close(&hi); return n; }
int32_t CTB_compress(const char *filename) { char *p, tmp_file[MAXPATH], file_name[MAXPATH]; CTB_handle hi, ho; int32_t i, n, compress; uchar dst[CTB_DATA_SIZE], buffer[256* 128 + 2 + CTB_DATA_SIZE ]; p = ctb_last_punct(file_name); strcpy(file_name, filename); ctb_err_code = CTB_ERR_NONE; if (p) *p = '\0'; STRCPY(tmp_file, file_name); p = STRRCHR(tmp_file, '\\'); if (p) { *(p + 1) = '\0'; STRCAT(tmp_file, "$$$$$$$$"); } else STRCPY(tmp_file, "$$$$$$$$"); if (!CTB_open(file_name, &hi, "w")) return FALSE; CTB_read_global_data(&hi, dst); CTB_files_init(tmp_file, dst, hi.width, hi.height, hi.dpb, hi.signums, hi.attr_size); n = CTB_volume(&hi); compress = hi.need_compress; if (compress) { if (!CTB_open(tmp_file, &ho, "w")) return 0; for (i = 0; i < n; i++) { switch (CTB_read(&hi, i, buffer, dst)) { case 1: CTB_write_mark(&ho, -1, buffer, dst, FALSE); break; case 2: CTB_write_mark(&ho, -1, buffer, dst, TRUE); break; default: break; } } ho.need_compress = 0; CTB_close(&ho); } hi.need_compress = 0; CTB_close(&hi); if (!compress) { // delete tmp file // STRCAT(tmp_file, ".CTB"); UNLINK(tmp_file); *ctb_last_punct(tmp_file) = 0; STRCAT(tmp_file, ".IND"); UNLINK(tmp_file); return 0; } STRCAT(file_name, ".CTB"); STRCAT(tmp_file, ".CTB"); UNLINK(file_name); RENAME(tmp_file, file_name); *ctb_last_punct(tmp_file) = 0; *ctb_last_punct(file_name) = 0; STRCAT(file_name, ".IND"); STRCAT(tmp_file, ".IND"); UNLINK(file_name); RENAME(tmp_file, file_name); return n; }