Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* compile with
- *
- * g++ -Wall test.cpp `pkg-config vipsCC --cflags --libs`
- *
- */
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <vips/vips>
- #include <vips/vips.h>
- #define PROGRESSIVE 1
- /* Macro to convert integer to string */
- #define SSTR( x ) dynamic_cast< std::ostringstream & > ( ( std::ostringstream() << std::dec << x ) ).str()
- /**
- *
- * @param inputname : absolute path of the file. [ /home/vinod/test_code/test.cpp ]
- * @param path : the path. [ /home/vinod/test_code ]
- * @param name : the name. [ test ]
- * @param ext : the extension. [ cpp ]
- */
- void split_path_name_ext(std::string &inputname, std::string &path, std::string &name, std::string &ext)
- {
- path = inputname.substr(0, inputname.find_last_of("/"));
- name = inputname.substr(inputname.find_last_of("/") + 1,
- inputname.find_last_of(".") - inputname.find_last_of("/") - 1);
- ext = inputname.substr(inputname.find_last_of(".") + 1);
- return;
- }
- /**
- *
- * @param ifilename : name of the image file to be resized.
- * @return 0 on success.
- */
- int image_resize(std::string& ifilename)
- {
- int height[]={1080,720,480,360,240};
- int n_files = sizeof(height);
- std::string path , name , ext;
- split_path_name_ext(ifilename,path,name,ext);
- /* The vips7 C++ interface doesn't support the open mode we need.
- */
- VipsImage *x;
- if (vips_foreign_load (ifilename.c_str (), &x,
- "access", VIPS_ACCESS_SEQUENTIAL, NULL)){
- std::cout<<"cant load - "<<ifilename<<std::endl;
- return -1;
- }
- vips::VImage im (x);
- im._ref->close_on_delete = 1;
- double aspect_ratio = double(im.Xsize()) / double(im.Ysize());
- vips::VImage * out;
- out = new vips::VImage [n_files + 1];
- out[0] = im;
- for(int i=0; i < n_files ; i++)
- {
- int input_height = out[i].Ysize();
- int target_height = height[i];
- int target_width = (int(target_height * aspect_ratio) /2 )*2;
- /* I need not upsample */
- if (target_height > input_height) {
- out[i + 1] = out[i];
- continue;
- }
- double scale = target_height / (double) input_height;
- /* Shrink to integer factor first - This is faster
- */
- int shrink_factor = floor (1.0 / scale);
- if ((1.0 / scale) > 2.0)
- {
- out[i] = out[i].shrink (double (shrink_factor), double (shrink_factor));
- }
- /* tilecache is not exposed by the vips7 C++ interface, sadly.
- */
- int tile_width;
- int tile_height;
- int nlines;
- VipsImage * temp;
- vips_get_tile_size (out[i].image (), &tile_width, &tile_height, &nlines);
- if (vips_tilecache (out[i].image (), &temp,
- "tile_width", out[i].Xsize (),
- "tile_height", 10,
- "max_tiles", (nlines * 2) / 10,
- "access", VIPS_ACCESS_SEQUENTIAL,
- "threaded", TRUE, NULL))
- vips_error_exit ("can't cache");
- out[i+1] = vips::VImage(temp);
- out[i+1]._ref->close_on_delete = 1;
- out[i+1]._ref->addref (out[i]._ref);
- /* Use affine transform to get to the required size */
- int height_aft_shrink = floor (input_height / shrink_factor);
- scale = target_height / (double) height_aft_shrink;
- out[i+1] = out[i+1].affinei ("bicubic", scale, 0, 0, scale,
- 0, 0, 0, 0, target_width, target_height);
- /* Apply Sharpening Filter */
- if (target_width < 150 || target_height < 150)
- {
- /* Sharpening Filter*/
- vips::VIMask sharpen_filter (3, 3, 8, 0,
- -1, -1, -1, -1, 16, -1, -1, -1, -1);
- out[i+1] = out[i+1].conv (sharpen_filter);
- }
- std::string ofilename = path + "/" + name + "_" + SSTR(target_height) + ".jpg";
- std::cout << "starting save for " << ofilename << " ..." << std::endl;
- if (vips_foreign_save ((VipsImage *) (out[i+1].image ()),
- (char *) ofilename.c_str (),
- "interlace", PROGRESSIVE, NULL))
- {
- std::cout << "Error in saving file: " << ofilename << std::endl;
- std::cout << vips_error_buffer () << std::endl;
- }
- }
- return 0;
- }
- int
- main (int argc, char **argv)
- {
- if (vips_init (argv[0]))
- vips_error_exit ("unable to init");
- GOptionContext *context;
- GError *error = NULL;
- context = g_option_context_new ("- test prog");
- g_option_context_add_group (context, vips_get_option_group ());
- if (!g_option_context_parse (context, &argc, &argv, &error))
- {
- if (error)
- {
- fprintf (stderr, "%s\n", error->message);
- g_error_free (error);
- }
- vips_error_exit ("try \"%s --help\"", g_get_prgname ());
- }
- g_option_context_free (context);
- std::string filelist("/home/vinod/simple_tests/filelist.txt");
- std::ifstream filehandle(filelist.c_str());
- while(!filehandle.eof())
- {
- std::string ifilename;
- getline(filehandle, ifilename);
- if(ifilename.size()!=0)
- {
- image_resize(ifilename);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement