📄 xgprintgif.c
字号:
r = hexit[colorname[1]]; g = hexit[colorname[2]]; b = hexit[colorname[3]]; r = rgbnorm( r, lmaxval, 1, colorname ); g = rgbnorm( g, lmaxval, 1, colorname ); b = rgbnorm( b, lmaxval, 1, colorname ); break; case 7: r = ( hexit[colorname[1]] << 4 ) + hexit[colorname[2]]; g = ( hexit[colorname[3]] << 4 ) + hexit[colorname[4]]; b = ( hexit[colorname[5]] << 4 ) + hexit[colorname[6]]; r = rgbnorm( r, lmaxval, 2, colorname ); g = rgbnorm( g, lmaxval, 2, colorname ); b = rgbnorm( b, lmaxval, 2, colorname ); break; case 10: r = ( hexit[colorname[1]] << 8 ) + ( hexit[colorname[2]] << 4 ) + hexit[colorname[3]]; g = ( hexit[colorname[4]] << 8 ) + ( hexit[colorname[5]] << 4 ) + hexit[colorname[6]]; b = ( hexit[colorname[7]] << 8 ) + ( hexit[colorname[8]] << 4 ) + hexit[colorname[9]]; r = rgbnorm( r, lmaxval, 3, colorname ); g = rgbnorm( g, lmaxval, 3, colorname ); b = rgbnorm( b, lmaxval, 3, colorname ); break; case 13: r = ( hexit[colorname[1]] << 12 ) + ( hexit[colorname[2]] << 8 ) + ( hexit[colorname[3]] << 4 ) + hexit[colorname[4]]; g = ( hexit[colorname[5]] << 12 ) + ( hexit[colorname[6]] << 8 ) + ( hexit[colorname[7]] << 4 ) + hexit[colorname[8]]; b = ( hexit[colorname[9]] << 12 ) + ( hexit[colorname[10]] << 8 ) + ( hexit[colorname[11]] << 4 ) + hexit[colorname[12]]; r = rgbnorm( r, lmaxval, 4, colorname ); g = rgbnorm( g, lmaxval, 4, colorname ); b = rgbnorm( b, lmaxval, 4, colorname ); break; default: printf("Unsupported color specification: %s\n",colorname); r = g = b = 0; break; } } else if ( ( colorname[0] >= '0' && colorname[0] <= '9' ) || colorname[0] == '.' ) { /* It's an old-style decimal/float rgb specifier. */ float fr, fg, fb; sscanf((char *)colorname,"%f , %f , %f",&fr,&fg,&fb); r = fr * lmaxval; g = fg * lmaxval; b = fb * lmaxval; } else { printf("XpmToGif: color name lookup not implemented yet.\n"); r = b = g = 0; } PPM_ASSIGN( p, r, g, b ); return p;}char **pm_allocarray(int cols, int rows, int size) { char** its; int i; its = (char**) my_malloc( rows * sizeof(char*) ); its[0] = (char*) my_malloc( rows * cols * size ); for ( i = 1; i < rows; ++i ) its[i] = &(its[0][i * cols * size]); return its;}void ppm_readppminitrest(char *stream, int* colsP, int* rowsP, pixval* maxvalP) { int maxval; /* Read size. */ *colsP = ppm_getint( stream ); *rowsP = ppm_getint( stream ); /* Read maxval. */ maxval = ppm_getint( stream ); *maxvalP = maxval;}int ppm_readmagicnumber(char *stream) { int ich1, ich2; ich1 = stream[read_index++]; ich2 = stream[read_index++]; return ich1 * 256 + ich2;}int ppm_getint(char *stream) { register char ch; register int i; do { ch = stream[read_index++]; } while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' ); i = 0; do { i = i * 10 + ch - '0'; ch = stream[read_index++]; } while ( ch >= '0' && ch <= '9' ); return i;}void ppm_readppmrow( char *stream, pixel* pixelrow, int cols, pixval maxval, int format ) { register int col; register pixel* pP; register pixval r, g, b; for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP ) { r = stream[read_index++]; g = stream[read_index++]; b = stream[read_index++]; PPM_ASSIGN( *pP, r, g, b ); }}pixel **ppm_readppm(char *stream, int* colsP, int* rowsP, pixval* maxvalP) { pixel** pixels; int row; int format = 0; ppm_readppminitrest( stream, colsP, rowsP, maxvalP); pixels = ppm_allocarray( *colsP, *rowsP ); for ( row = 0; row < *rowsP; ++row ) ppm_readppmrow( stream, pixels[row], *colsP, *maxvalP, format ); return pixels;}/*******************************************************************//*******************************************************************/void XpmToGif(char **xpmData, char *outFile) { pixel *pixrow = NULL, *colors = NULL; register pixel *pP; int rows, cols, ncolors, chars_per_pixel, row; register int col; register char *ptr; char *stream=NULL; int length; int size=1; read_index=3; write_index=0; ppm_pbmmaxval = 1; maxbits = BITS; maxmaxcode = (code_int)1 << BITS; hsize = HSIZE; free_ent = 0; clear_flg = 0; in_count = 1; out_count = 0; cur_accum = 0; cur_bits = 0; getline_index = 0; pixels = NULL; cht = NULL; ReadXPMFile(xpmData, &cols, &rows, &ncolors, &chars_per_pixel, &colors); stream = ppm_writeppminit(stream, cols, rows, (pixval) PPM_MAXMAXVAL, 0); length = strlen(stream); pixrow = ppm_allocrow(cols); for (row = 0, ptr = xpmData[row+ncolors+1]; row < rows; ++row, ptr = xpmData[row+ncolors+1]) { for (col = 0, pP = pixrow; col < cols; ++col, ++pP, ++ptr) *pP = colors[(int)*ptr]; stream = (char *)my_realloc(stream, size*cols*3*sizeof(char)+length); ppm_writeppmrow(stream, pixrow, cols, (pixval) PPM_MAXMAXVAL, 0); size++; } ToGif(stream, outFile); free(pixrow); free(stream); free(pixels[0]); free(pixels); free(colors);}void ReadXPMFile(char **stream, int *widthP, int *heightP, int *ncolorsP, int *chars_per_pixelP, pixel **colorsP) { char line[MAX_LINE], str1[MAX_LINE], str2[MAX_LINE]; char *t1; char *t2; int format, v; register int i, j; int flag; unsigned int curkey, key, highkey; /* current color key */ unsigned int lastwaskey; /* key read */ char curbuf[BUFSIZ]; /* current buffer */ *widthP = *heightP = *ncolorsP = *chars_per_pixelP = format = -1; flag = 0; /* to avoid getting twice a line */ /* Read the hints line */ getline(line, sizeof(line), stream); sscanf(line, "%d %d %d %d", widthP, heightP, ncolorsP, chars_per_pixelP); /* Allocate space for color table. */ /* Up to two chars per pixel, we can use an indexed table. */ v = 1; for (i = 0; i < *chars_per_pixelP; ++i) v *= 256; *colorsP = ppm_allocrow(v); /* Read the color table */ for (i = 0; i < *ncolorsP; i++) { getline(line, sizeof(line), stream); t1 = line; strncpy(str1, t1, *chars_per_pixelP); str1[*chars_per_pixelP] = '\0'; t1++; t1++; v = 0; for (j = 0; j < *chars_per_pixelP; ++j) v = (v << 8) + str1[j]; /* * read color keys and values */ curkey = 0; highkey = 1; lastwaskey = 0; t2 = t1; while ( 1 ) { for (t1=t2 ;; t1++) if (*t1 != ' ' && *t1 != ' ') break; for (t2 = t1;; t2++) if (*t2 == ' ' || *t2 == ' ' || *t2 == '\0') break; if (t2 == t1) break; strncpy(str2, t1, t2 - t1); str2[t2 - t1] = '\0'; if (!lastwaskey) { for (key = 1; key < NKEYS + 1; key++) if (!strcmp(colorKeys[key - 1], str2)) break; } else key = NKEYS + 1; if (key > NKEYS) { /* append name */ if (!lastwaskey) strcat(curbuf, " "); /* append space */ strcat(curbuf, str2); /* append buf */ lastwaskey = 0; } if (key <= NKEYS) { /* new key */ if (curkey > highkey) { /* flush string */ /* Index into table. */ (*colorsP)[v] = ppm_parsecolor((unsigned char*)curbuf, (pixval) PPM_MAXMAXVAL); highkey = curkey; } curkey = key; /* set new key */ curbuf[0] = '\0'; /* reset curbuf */ lastwaskey = 1; } if (*t2 == 0) break; } if (curkey > highkey) { /* Index into table. */ (*colorsP)[v] = ppm_parsecolor((unsigned char*)curbuf, (pixval) PPM_MAXMAXVAL); highkey = curkey; } }}void getline( char *line, int size, char **stream) { static int length, i, j, k, l, max; if (getline_index == 0) { sscanf(stream[0], "%d %d %d %d", &i, &j, &k, &l); max = j+k; } if (getline_index <= max) { length = strlen(stream[getline_index]); i = (size > length) ? length : size; strncpy(line, stream[getline_index], i); line[i] = 0; getline_index++; }}void ToGif(char* stream, char *outFile) { int rows, cols, colors, i, BitsPerPixel; pixval maxval; colorhist_vector chv = NULL; int Red[MAXCOLORS], Green[MAXCOLORS], Blue[MAXCOLORS]; FILE *file; if((file = fopen(outFile, "wb"))==NULL) { printf("\nError: could not write GIF file, open failed."); return; } pixels = ppm_readppm( stream, &cols, &rows, &maxval ); /* Figure out the colormap. */ chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colors ); /* Now turn the ppm colormap into the appropriate GIF colormap. */ for ( i = 0; i < colors; ++i ) { Red[i] = PPM_GETR( chv[i].color ); Green[i] = PPM_GETG( chv[i].color ); Blue[i] = PPM_GETB( chv[i].color ); } BitsPerPixel = colorstobpp( colors ); /* And make a hash table for fast lookup. */ cht = ppm_colorhisttocolorhash( chv, colors ); ppm_freecolorhist( chv ); /* All set, let's do it. */ GIFEncode(file, cols, rows, 0, 0, BitsPerPixel, Red, Green, Blue, GetPixel ); ppm_freecolorhash( cht ); fclose(file);}static int colorstobpp( int colors) { int bpp; if ( colors <= 2 ) bpp = 1; else if ( colors <= 4 ) bpp = 2; else if ( colors <= 8 ) bpp = 3; else if ( colors <= 16 ) bpp = 4; else if ( colors <= 32 ) bpp = 5; else if ( colors <= 64 ) bpp = 6; else if ( colors <= 128 ) bpp = 7; else if ( colors <= 256 ) bpp = 8; return bpp;}static int GetPixel( int x, int y) { int color; color = ppm_lookupcolor( cht, &pixels[y][x] ); return color;}/***************************************************************************** * * GIFENCODE.C - GIF Image compression interface * * GIFEncode( FName, GHeight, GWidth, GInterlace, Background, * BitsPerPixel, Red, Green, Blue, GetPixel ) * *****************************************************************************//* * Bump the 'curx' and 'cury' to point to the next pixel */static void BumpPixel() { /* * Bump the current X position */ ++curx; /* * If we are at the end of a scan line, set curx back to the beginning * If we are interlaced, bump the cury to the appropriate spot, * otherwise, just increment it. */ if( curx == Width ) { curx = 0; if( !Interlace ) ++cury; else { switch( Pass ) { case 0: cury += 8; if( cury >= Height ) { ++Pass; cury = 4; } break; case 1: cury += 8; if( cury >= Height ) { ++Pass; cury = 2; } break; case 2: cury += 4; if( cury >= Height ) { ++Pass; cury = 1; } break; case 3: cury += 2; break; } } }}/* * Return the next pixel from the image */static int GIFNextPixel( ifunptr getpixel) { int r=0; if( CountDown == 0 ) return EOF; --CountDown; r = ( * getpixel )( curx, cury ); BumpPixel(); return r;}/* public */static void GIFEncode(FILE* fp, int GWidth, int GHeight, int GInterlace,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -