hsv_quantize.c
Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <malloc.h>
00004 #include <math.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007 #include <ppm.h>
00008
00009 #include "hsv_test.proto"
00010
00011 main(int argc, char *argv[]) {
00012
00013 FILE *ppm_file;
00014 PPM *im_hsv, *im_quant, *im_output;
00015 int *colmap;
00016 enum file_types ppm_type;
00017 enum ppm_error the_error;
00018 int numH, numS, numV, numGrey;
00019
00020 switch(argc) {
00021 case 5:
00022 numH = atoi(argv[1]);
00023 numS = atoi(argv[2]);
00024 numV = atoi(argv[3]);
00025 numGrey = atoi(argv[4]);
00026 break;
00027 default:
00028 fprintf(stderr, "Usage: %s numH, numS, numV, numGrey\n\n", argv[0]);
00029 exit(1);
00030 break;
00031 }
00032
00033 ppm_file = stdin;
00034
00035
00036 switch(ppm_type = read_magic_no(ppm_file)) {
00037 case PGM_ASC: case PPM_ASC: case PGM_RAW: case PPM_RAW:
00038 if ((the_error = read_ppm(ppm_file, &im_hsv, ppm_type)) != PPM_OK)
00039 {
00040 ppm_handle_error(the_error);
00041 exit(1);
00042 }
00043 break;
00044 default:
00045 fprintf(stderr, "Unrecognized file type.\n");
00046 exit(1);
00047 break;
00048 }
00049
00050
00051 if ((the_error = hsv_quantize_ppm(im_hsv, &im_quant, &colmap, numH, numS, numV, numGrey)) != PPM_OK) {
00052 ppm_handle_error(the_error);
00053 exit(1);
00054 }
00055
00056
00057 if ((the_error = colmap2rgb_ppm(im_quant, colmap, numH*numS*numV + numGrey, &im_output)) != PPM_OK) {
00058 ppm_handle_error(the_error);
00059 exit(1);
00060 }
00061
00062
00063 if ((the_error = write_ppm(stdout, im_output, PPM_RAW)) != PPM_OK) {
00064 ppm_handle_error(the_error);
00065 exit(1);
00066 }
00067
00068 exit(0);
00069 }