📄 rgbaindexedpng2osd.c
字号:
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <unistd.h> /* isatty() */#include "png.h" /* libpng header file: includes zlib.h and setjmp.h */static void rgbtoyuv(double R,double G,double B,double *y,double *u,double *v){ *y=(0.257*R + 0.504*G + 0.098*B) + 16.0; *u=(-0.148*R - 0.291*G + 0.439*B) + 128.0; *v=(0.439*R - 0.368*G - 0.071*B) + 128.0;}int main(int argc,char **argv){ FILE *infile; FILE *outfile; png_structp png_ptr; png_infop info_ptr; png_uint_32 width, height, rowbytes; int bit_depth, color_type, num_palette,num_trans; png_colorp palette; png_bytep alphapalette; png_bytep image_data = NULL; png_bytepp row_pointers = NULL; int i,x,y, size; unsigned char b; if (argc > 1) { if ((infile = fopen(argv[1], "rb")) == (FILE *)NULL) { fprintf(stderr, "rgbaindexedpng2osd error: cannot open %s for reading\n", argv[1]); return 1; } if ((outfile = fopen(argv[2], "wb")) == (FILE *)NULL) { fprintf(stderr, "rgbaindexedpng2osd error: cannot open %s for writing\n", argv[2]); return 1; } } else { fprintf(stderr,"Usage: %s inputrgbaindexed.png outfile.osd\n",argv[0]); return -1; } png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,NULL, NULL); info_ptr = png_create_info_struct(png_ptr); png_init_io(png_ptr, infile); png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */ png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); printf("Reading %ldx%ld image.\n",width,height); /* writes osd header */ b = 0x3e; fwrite(&b, 1, 1, outfile); size = width * height + 1032; b = size >> 16; fwrite(&b, 1, 1, outfile); b = (size >> 8) & 0xff; fwrite(&b, 1, 1, outfile); b = size & 0xff; fwrite(&b, 1, 1, outfile); b = width >> 8; fwrite(&b, 1, 1, outfile); b = width & 0xff; fwrite(&b, 1, 1, outfile); b = height >> 8; fwrite(&b, 1, 1, outfile); b = height & 0xff; fwrite(&b, 1, 1, outfile); /* end of osd header */ if (color_type != PNG_COLOR_TYPE_PALETTE) { fprintf(stderr, "image has a wrong palette type\n"); return 1; } png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette); // (Manu) what is the last parameter? png_get_tRNS(png_ptr, info_ptr, &alphapalette,&num_trans,NULL); if (num_palette != num_trans) { fprintf(stderr, "incorrect palettes: color entries = %d, trans entries = %d\n", num_palette, num_trans); return 1; } if (num_palette>256) { fprintf(stderr, "palette size is too big\n"); return 1; } /* writes the palette */ for (i=0 ; i<256 ; i++) { double R,G,B,y,u,v; int a; if (i<num_palette) { R=palette[i].red; G=palette[i].green; B=palette[i].blue; rgbtoyuv(R,G,B,&y,&u,&v); a = alphapalette[i]; } else { y = 0; u = 0; v = 0; a = 0; } b = (unsigned char) a; fwrite(&b, 1, 1, outfile); b = (unsigned char) y; fwrite(&b, 1, 1, outfile); b = (unsigned char) u; fwrite(&b, 1, 1, outfile); b = (unsigned char) v; fwrite(&b, 1, 1, outfile); } /* end of osd palette */ /* allocate space for the PNG image data */ rowbytes = png_get_rowbytes(png_ptr, info_ptr); image_data = (png_bytep)malloc(rowbytes*height); row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep)); /* set the individual row_pointers to point at the correct offsets */ for (i = 0; i < (int)height; ++i) row_pointers[i] = image_data + i*rowbytes; png_read_image(png_ptr, row_pointers); /* read whole image... */ png_read_end(png_ptr, NULL); /* ...done! */ /* writes the indexed image */ for (y=0;y<(int)height;y++) { for (x=0;x<(int)width;x++) { b = (unsigned char) ((row_pointers[y])[x]); fwrite(&b, 1, 1, outfile); } } /* end of indexed image */ png_destroy_read_struct(&png_ptr, &info_ptr, NULL); free(image_data); free(row_pointers); fclose(infile); fclose(outfile); printf("Done.\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -