📄 raw2tiff.c
字号:
{ switch (dtype) { case TIFF_SHORT: case TIFF_SSHORT: TIFFSwabArrayOfShort((uint16*)buf, width); break; case TIFF_LONG: case TIFF_SLONG: TIFFSwabArrayOfLong((uint32*)buf, width); break; /* case TIFF_FLOAT: */ /* FIXME */ case TIFF_DOUBLE: TIFFSwabArrayOfDouble((double*)buf, width); break; default: break; }}static intguessSize(FILE *fp, TIFFDataType dtype, uint32 hdr_size, int nbands, int swab, uint32 *width, uint32 *length){ const float longt = 40.0; /* maximum possible height/width ratio */ char *buf1, *buf2; struct stat filestat; uint32 w, h, scanlinesize, imagesize; int depth = TIFFDataWidth(dtype); float cor_coef = 0, tmp; fstat(fileno(fp), &filestat); if (filestat.st_size < hdr_size) { fprintf(stderr, "Too large header size specified.\n"); return -1; } imagesize = (filestat.st_size - hdr_size) / nbands / depth; if (*width != 0 && *length == 0) { fprintf(stderr, "Image height is not specified.\n"); *length = imagesize / *width; fprintf(stderr, "Height is guessed as %ld.\n", *length); return 1; } else if (*width == 0 && *length != 0) { fprintf(stderr, "Image width is not specified.\n"); *width = imagesize / *length; fprintf(stderr, "Width is guessed as %ld.\n", *width); return 1; } else if (*width == 0 && *length == 0) { fprintf(stderr, "Image width and height are not specified.\n"); for (w = sqrt(imagesize / longt); w < sqrt(imagesize * longt); w++) { if (imagesize % w == 0) { scanlinesize = w * depth; buf1 = _TIFFmalloc(scanlinesize); buf2 = _TIFFmalloc(scanlinesize); h = imagesize / w; fseek(fp, hdr_size + (int)(h/2)*scanlinesize, SEEK_SET); fread(buf1, scanlinesize, 1, fp); fread(buf2, scanlinesize, 1, fp); if (swab) { swapBytesInScanline(buf1, w, dtype); swapBytesInScanline(buf2, w, dtype); } tmp = fabs(correlation(buf1, buf2, w, dtype)); if (tmp > cor_coef) { cor_coef = tmp; *width = w, *length = h; } _TIFFfree(buf1); _TIFFfree(buf2); } } fprintf(stderr, "Width is guessed as %ld, height is guessed as %ld.\n", *width, *length); return 1; } else { if (filestat.st_size<hdr_size+(*width)*(*length)*nbands*depth) { fprintf(stderr, "Input file too small.\n"); return -1; } } return 1;}/* Calculate correlation coefficient between two numeric vectors */static doublecorrelation(void *buf1, void *buf2, uint32 n_elem, TIFFDataType dtype){ float X, Y, M1 = 0.0, M2 = 0.0, D1 = 0.0, D2 = 0.0, K = 0.0; int i; switch (dtype) { case TIFF_BYTE: default: for (i = 0; i < n_elem; i++) { X = ((u_char *)buf1)[i]; Y = ((u_char *)buf2)[i]; M1 += X, M2 += Y; D1 += X * X, D2 += Y * Y; K += X * Y; } break; case TIFF_SBYTE: for (i = 0; i < n_elem; i++) { X = ((signed char *)buf1)[i]; Y = ((signed char *)buf2)[i]; M1 += X, M2 += Y; D1 += X * X, D2 += Y * Y; K += X * Y; } break; case TIFF_SHORT: for (i = 0; i < n_elem; i++) { X = ((uint16 *)buf1)[i]; Y = ((uint16 *)buf2)[i]; M1 += X, M2 += Y; D1 += X * X, D2 += Y * Y; K += X * Y; } break; case TIFF_SSHORT: for (i = 0; i < n_elem; i++) { X = ((int16 *)buf1)[i]; Y = ((int16 *)buf2)[i]; M1 += X, M2 += Y; D1 += X * X, D2 += Y * Y; K += X * Y; } break; case TIFF_LONG: for (i = 0; i < n_elem; i++) { X = ((uint32 *)buf1)[i]; Y = ((uint32 *)buf2)[i]; M1 += X, M2 += Y; D1 += X * X, D2 += Y * Y; K += X * Y; } break; case TIFF_SLONG: for (i = 0; i < n_elem; i++) { X = ((int32 *)buf1)[i]; Y = ((int32 *)buf2)[i]; M1 += X, M2 += Y; D1 += X * X, D2 += Y * Y; K += X * Y; } break; case TIFF_FLOAT: for (i = 0; i < n_elem; i++) { X = ((float *)buf1)[i]; Y = ((float *)buf2)[i]; M1 += X, M2 += Y; D1 += X * X, D2 += Y * Y; K += X * Y; } break; case TIFF_DOUBLE: for (i = 0; i < n_elem; i++) { X = ((double *)buf1)[i]; Y = ((double *)buf2)[i]; M1 += X, M2 += Y; D1 += X * X, D2 += Y * Y; K += X * Y; } break; } M1 /= n_elem; M2 /= n_elem; D1 -= M1 * M1 * n_elem; D2 -= M2 * M2 * n_elem; K = (K - M1 * M2 * n_elem) / sqrt(D1 * D2); return K;}static intprocessCompressOptions(char* opt){ if (strcmp(opt, "none") == 0) compression = COMPRESSION_NONE; else if (strcmp(opt, "packbits") == 0) compression = COMPRESSION_PACKBITS; else if (strncmp(opt, "jpeg", 4) == 0) { char* cp = strchr(opt, ':'); if (cp && isdigit(cp[1])) quality = atoi(cp+1); if (cp && strchr(cp, 'r')) jpegcolormode = JPEGCOLORMODE_RAW; compression = COMPRESSION_JPEG; } else if (strncmp(opt, "lzw", 3) == 0) { char* cp = strchr(opt, ':'); if (cp) predictor = atoi(cp+1); compression = COMPRESSION_LZW; } else if (strncmp(opt, "zip", 3) == 0) { char* cp = strchr(opt, ':'); if (cp) predictor = atoi(cp+1); compression = COMPRESSION_DEFLATE; } else return (0); return (1);}char* stuff[] = {"raw2tiff --- tool to converting raw byte sequences in TIFF images","usage: raw2tiff [options] input.raw output.tif","where options are:"," -L input data has LSB2MSB bit order (default)"," -M input data has MSB2LSB bit order"," -r # make each strip have no more than # rows"," -H # size of input image file header in bytes (0 by default)"," -w # width of input image in pixels"," -l # length of input image in lines"," -b # number of bands in input image (1 by default)",""," -d data_type type of samples in input image","where data_type may be:"," byte 8-bit unsigned integer (default)"," short 16-bit unsigned integer"," long 32-bit unsigned integer"," sbyte 8-bit signed integer"," sshort 16-bit signed integer"," slong 32-bit signed integer"," float 32-bit IEEE floating point"," double 64-bit IEEE floating point",""," -p photo photometric interpretation (color space) of the input image","where photo may be:"," miniswhite white color represented with 0 value"," minisblack black color represented with 0 value (default)"," rgb image has RGB color model"," cmyk image has CMYK (separated) color model"," ycbcr image has YCbCr color model"," cielab image has CIE L*a*b color model"," icclab image has ICC L*a*b color model"," itulab image has ITU L*a*b color model",""," -s swap bytes fetched from input file",""," -i config type of samples interleaving in input image","where config may be:"," pixel pixel interleaved data (default)"," band band interleaved data",""," -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding"," (no longer supported by default due to Unisys patent enforcement)", " -c zip[:opts] compress output with deflate encoding"," -c jpeg[:opts]compress output with JPEG encoding"," -c packbits compress output with packbits encoding"," -c none use no compression algorithm on output","","JPEG options:"," # set compression quality level (0-100, default 75)"," r output color image as RGB rather than YCbCr","For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. quality","","LZW and deflate options:"," # set predictor value","For example, -c lzw:2 to get LZW-encoded data with horizontal differencing"," -o out.tif write output to out.tif"," -h this help message",NULL};static voidusage(void){ char buf[BUFSIZ]; int i; setbuf(stderr, buf); fprintf(stderr, "%s\n\n", TIFFGetVersion()); for (i = 0; stuff[i] != NULL; i++) fprintf(stderr, "%s\n", stuff[i]); exit(-1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -