Ejemplo n.º 1
0
        public static string IdentifyImageFormat(IMAGE_FORMATS format)
        {
            switch (format)
            {
            case IMAGE_FORMATS.JPG:
                return(".jpg");

            case IMAGE_FORMATS.PNG:
                return(".png");

            default:
                return(null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parse optional switches.
        /// Returns argv[] index of first file-name argument (== argc if none).
        /// Any file names with indexes <= last_file_arg_seen are ignored;
        /// they have presumably been processed in a previous iteration.
        /// (Pass 0 for last_file_arg_seen on the first or only iteration.)
        /// for_real is false on the first (dummy) pass; we may skip any expensive
        /// processing.
        /// </summary>
        static bool parse_switches(jpeg_decompress_struct cinfo, string[] argv, bool for_real, out int last_file_arg_seen)
        {
            string arg;

            /* Set up default JPEG parameters. */
            requested_fmt         = IMAGE_FORMATS.FMT_BMP; /* set default output file format */
            outfilename           = null;
            last_file_arg_seen    = -1;
            cinfo.Err.Trace_level = 0;

            /* Scan command line options, adjust parameters */
            int argn = 0;

            for ( ; argn < argv.Length; argn++)
            {
                arg = argv[argn];
                if (arg[0] != '-')
                {
                    /* Not a switch, must be a file name argument */
                    last_file_arg_seen = argn;
                    break;
                }

                arg = arg.Substring(1);

                if (cdjpeg_utils.keymatch(arg, "bmp", 1))
                {
                    /* BMP output format. */
                    requested_fmt = IMAGE_FORMATS.FMT_BMP;
                }
                else if (cdjpeg_utils.keymatch(arg, "colors", 1) ||
                         cdjpeg_utils.keymatch(arg, "colours", 1) ||
                         cdjpeg_utils.keymatch(arg, "quantize", 1) ||
                         cdjpeg_utils.keymatch(arg, "quantise", 1))
                {
                    /* Do color quantization. */
                    if (++argn >= argv.Length) /* advance to next argument */
                    {
                        return(false);
                    }

                    try
                    {
                        int val = int.Parse(argv[argn]);
                        cinfo.Desired_number_of_colors = val;
                        cinfo.Quantize_colors          = true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return(false);
                    }
                }
                else if (cdjpeg_utils.keymatch(arg, "dct", 2))
                {
                    /* Select IDCT algorithm. */
                    if (++argn >= argv.Length) /* advance to next argument */
                    {
                        return(false);
                    }

                    if (cdjpeg_utils.keymatch(argv[argn], "int", 1))
                    {
                        cinfo.Dct_method = J_DCT_METHOD.JDCT_ISLOW;
                    }
                    else if (cdjpeg_utils.keymatch(argv[argn], "fast", 2))
                    {
                        cinfo.Dct_method = J_DCT_METHOD.JDCT_IFAST;
                    }
                    else if (cdjpeg_utils.keymatch(argv[argn], "float", 2))
                    {
                        cinfo.Dct_method = J_DCT_METHOD.JDCT_FLOAT;
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (cdjpeg_utils.keymatch(arg, "dither", 2))
                {
                    /* Select dithering algorithm. */
                    if (++argn >= argv.Length) /* advance to next argument */
                    {
                        return(false);
                    }

                    if (cdjpeg_utils.keymatch(argv[argn], "fs", 2))
                    {
                        cinfo.Dither_mode = J_DITHER_MODE.JDITHER_FS;
                    }
                    else if (cdjpeg_utils.keymatch(argv[argn], "none", 2))
                    {
                        cinfo.Dither_mode = J_DITHER_MODE.JDITHER_NONE;
                    }
                    else if (cdjpeg_utils.keymatch(argv[argn], "ordered", 2))
                    {
                        cinfo.Dither_mode = J_DITHER_MODE.JDITHER_ORDERED;
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (cdjpeg_utils.keymatch(arg, "debug", 1) || cdjpeg_utils.keymatch(arg, "verbose", 1))
                {
                    /* Enable debug printouts. */
                    /* On first -d, print version identification */
                    if (!printed_version)
                    {
                        Console.Write(string.Format("Bit Miracle's DJPEG, version {0}\n{1}\n", jpeg_common_struct.Version, jpeg_common_struct.Copyright));
                        printed_version = true;
                    }
                    cinfo.Err.Trace_level++;
                }
                else if (cdjpeg_utils.keymatch(arg, "fast", 1))
                {
                    /* Select recommended processing options for quick-and-dirty output. */
                    cinfo.Two_pass_quantize = false;
                    cinfo.Dither_mode       = J_DITHER_MODE.JDITHER_ORDERED;
                    if (!cinfo.Quantize_colors) /* don't override an earlier -colors */
                    {
                        cinfo.Desired_number_of_colors = 216;
                    }
                    cinfo.Dct_method          = JpegConstants.JDCT_FASTEST;
                    cinfo.Do_fancy_upsampling = false;
                }
                else if (cdjpeg_utils.keymatch(arg, "grayscale", 2) || cdjpeg_utils.keymatch(arg, "greyscale", 2))
                {
                    /* Force monochrome output. */
                    cinfo.Out_color_space = J_COLOR_SPACE.JCS_GRAYSCALE;
                }
                else if (cdjpeg_utils.keymatch(arg, "rgb", 3))
                {
                    /* Force RGB output. */
                    cinfo.Out_color_space = J_COLOR_SPACE.JCS_RGB;
                }
                else if (cdjpeg_utils.keymatch(arg, "nosmooth", 3))
                {
                    /* Suppress fancy upsampling */
                    cinfo.Do_fancy_upsampling = false;
                }
                else if (cdjpeg_utils.keymatch(arg, "onepass", 3))
                {
                    /* Use fast one-pass quantization. */
                    cinfo.Two_pass_quantize = false;
                }
                else if (cdjpeg_utils.keymatch(arg, "os2", 3))
                {
                    /* BMP output format (OS/2 flavor). */
                    requested_fmt = IMAGE_FORMATS.FMT_OS2;
                }
                else if (cdjpeg_utils.keymatch(arg, "outfile", 4))
                {
                    /* Set output file name. */
                    if (++argn >= argv.Length) /* advance to next argument */
                    {
                        return(false);
                    }

                    outfilename = argv[argn];   /* save it away for later use */
                }
                else if (cdjpeg_utils.keymatch(arg, "scale", 1))
                {
                    /* Scale the output image by a fraction M/N. */
                    if (++argn >= argv.Length) /* advance to next argument */
                    {
                        return(false);
                    }

                    int slashPos = argv[argn].IndexOf('/');
                    if (slashPos == -1)
                    {
                        return(false);
                    }

                    try
                    {
                        string num = argv[argn].Substring(0, slashPos);
                        cinfo.Scale_num = int.Parse(num);
                        string denom = argv[argn].Substring(slashPos + 1);
                        cinfo.Scale_denom = int.Parse(denom);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return(false);
                    }
                }
                else
                {
                    /* bogus switch */
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        public static byte[] GetItem(string username, int size, ITEMS_COLLECTION item, IMAGE_FORMATS format)
        {
            string mainUrl     = ApiUtils.IdentifyItem(item);
            string imageFormat = ApiUtils.IdentifyImageFormat(format);

            string[] args = ApiUtils.GetArgs(mainUrl, username, size, imageFormat, item);
            return(FileUtils.GetBytesFromResponse(api.Request(args)));
        }
Ejemplo n.º 4
0
        public static void SaveItem(string username, string path, int size, ITEMS_COLLECTION item, IMAGE_FORMATS format)
        {
            string mainUrl     = ApiUtils.IdentifyItem(item);
            string imageFormat = ApiUtils.IdentifyImageFormat(format);

            string[] args = ApiUtils.GetArgs(mainUrl, username, size, imageFormat, item);
            FileUtils.SaveFileFromResponse(api.Request(args), path);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Parse optional switches.
        /// Returns argv[] index of first file-name argument (== argc if none).
        /// Any file names with indexes <= last_file_arg_seen are ignored;
        /// they have presumably been processed in a previous iteration.
        /// (Pass 0 for last_file_arg_seen on the first or only iteration.)
        /// for_real is false on the first (dummy) pass; we may skip any expensive
        /// processing.
        /// </summary>
        static bool parse_switches(jpeg_decompress_struct cinfo, string[] argv, bool for_real, out int last_file_arg_seen)
        {
            string arg;

            /* Set up default JPEG parameters. */
            requested_fmt = IMAGE_FORMATS.FMT_BMP;    /* set default output file format */
            outfilename = null;
            last_file_arg_seen = -1;
            cinfo.Err.Trace_level = 0;

            /* Scan command line options, adjust parameters */
            int argn = 0;
            for ( ; argn < argv.Length; argn++)
            {
                arg = argv[argn];
                if (arg[0] != '-')
                {
                    /* Not a switch, must be a file name argument */
                    last_file_arg_seen = argn;
                    break;
                }

                arg = arg.Substring(1);

                if (cdjpeg_utils.keymatch(arg, "bmp", 1))
                {
                    /* BMP output format. */
                    requested_fmt = IMAGE_FORMATS.FMT_BMP;
                }
                else if (cdjpeg_utils.keymatch(arg, "colors", 1) ||
                         cdjpeg_utils.keymatch(arg, "colours", 1) ||
                         cdjpeg_utils.keymatch(arg, "quantize", 1) ||
                         cdjpeg_utils.keymatch(arg, "quantise", 1))
                {
                    /* Do color quantization. */
                    if (++argn >= argv.Length) /* advance to next argument */
                        return false;

                    try
                    {
                        int val = int.Parse(argv[argn]);
                        cinfo.Desired_number_of_colors = val;
                        cinfo.Quantize_colors = true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return false;
                    }
                }
                else if (cdjpeg_utils.keymatch(arg, "dct", 2))
                {
                    /* Select IDCT algorithm. */
                    if (++argn >= argv.Length) /* advance to next argument */
                        return false;

                    if (cdjpeg_utils.keymatch(argv[argn], "int", 1))
                        cinfo.Dct_method = J_DCT_METHOD.JDCT_ISLOW;
                    else if (cdjpeg_utils.keymatch(argv[argn], "fast", 2))
                        cinfo.Dct_method = J_DCT_METHOD.JDCT_IFAST;
                    else if (cdjpeg_utils.keymatch(argv[argn], "float", 2))
                        cinfo.Dct_method = J_DCT_METHOD.JDCT_FLOAT;
                    else
                        return false;
                }
                else if (cdjpeg_utils.keymatch(arg, "dither", 2))
                {
                    /* Select dithering algorithm. */
                    if (++argn >= argv.Length) /* advance to next argument */
                        return false;

                    if (cdjpeg_utils.keymatch(argv[argn], "fs", 2))
                        cinfo.Dither_mode = J_DITHER_MODE.JDITHER_FS;
                    else if (cdjpeg_utils.keymatch(argv[argn], "none", 2))
                        cinfo.Dither_mode = J_DITHER_MODE.JDITHER_NONE;
                    else if (cdjpeg_utils.keymatch(argv[argn], "ordered", 2))
                        cinfo.Dither_mode = J_DITHER_MODE.JDITHER_ORDERED;
                    else
                        return false;
                }
                else if (cdjpeg_utils.keymatch(arg, "debug", 1) || cdjpeg_utils.keymatch(arg, "verbose", 1))
                {
                    /* Enable debug printouts. */
                    /* On first -d, print version identification */
                    if (!printed_version)
                    {
                        Console.Write(string.Format("Bit Miracle's DJPEG, version {0}\n{1}\n", jpeg_common_struct.Version, jpeg_common_struct.Copyright));
                        printed_version = true;
                    }
                    cinfo.Err.Trace_level++;
                }
                else if (cdjpeg_utils.keymatch(arg, "fast", 1))
                {
                    /* Select recommended processing options for quick-and-dirty output. */
                    cinfo.Two_pass_quantize = false;
                    cinfo.Dither_mode = J_DITHER_MODE.JDITHER_ORDERED;
                    if (!cinfo.Quantize_colors) /* don't override an earlier -colors */
                        cinfo.Desired_number_of_colors = 216;
                    cinfo.Dct_method = JpegConstants.JDCT_FASTEST;
                    cinfo.Do_fancy_upsampling = false;
                }
                else if (cdjpeg_utils.keymatch(arg, "grayscale", 2) || cdjpeg_utils.keymatch(arg, "greyscale", 2))
                {
                    /* Force monochrome output. */
                    cinfo.Out_color_space = J_COLOR_SPACE.JCS_GRAYSCALE;
                }
                else if (cdjpeg_utils.keymatch(arg, "nosmooth", 3))
                {
                    /* Suppress fancy upsampling */
                    cinfo.Do_fancy_upsampling = false;
                }
                else if (cdjpeg_utils.keymatch(arg, "onepass", 3))
                {
                    /* Use fast one-pass quantization. */
                    cinfo.Two_pass_quantize = false;
                }
                else if (cdjpeg_utils.keymatch(arg, "os2", 3))
                {
                    /* BMP output format (OS/2 flavor). */
                    requested_fmt = IMAGE_FORMATS.FMT_OS2;
                }
                else if (cdjpeg_utils.keymatch(arg, "outfile", 4))
                {
                    /* Set output file name. */
                    if (++argn >= argv.Length) /* advance to next argument */
                        return false;

                    outfilename = argv[argn];   /* save it away for later use */
                }
                else if (cdjpeg_utils.keymatch(arg, "scale", 1))
                {
                    /* Scale the output image by a fraction M/N. */
                    if (++argn >= argv.Length) /* advance to next argument */
                        return false;

                    int slashPos = argv[argn].IndexOf('/');
                    if (slashPos == -1)
                        return false;

                    try
                    {
                        string num = argv[argn].Substring(0, slashPos);
                        cinfo.Scale_num = int.Parse(num);
                        string denom = argv[argn].Substring(slashPos + 1);
                        cinfo.Scale_denom = int.Parse(denom);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return false;
                    }
                }
                else /* bogus switch */
                    return false;
            }

            return true;
        }