Find the answer to your Linux question:
Results 1 to 6 of 6
Hi Ive been doing some c programs for image editing. So far ive just done simple blurs, greyscale and colour level things... Now i would really like to be able ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Nov 2004
    Posts
    239

    passing in command line stuff and my first script and also..


    Hi

    Ive been doing some c programs for image editing. So far ive just done simple blurs, greyscale and colour level things...

    Now i would really like to be able to apply my effects to a whole group of images in a folder... so i thought i should start passing in the file names ect... in the command line which i can get to work except it seems to crash when i pass in a number, Any ideas what the type casting should look like to get an int out of the command line?

    also, lets say my effect is blur input_filename output_filename
    and i also have a effect mono input_filename output_filename

    could you guys show me how a script could be used to run these commands on a whole folder of images? Ive never done one before!


    And also, as im dealing with pics, i have a 2d array of pixels that Im loading the data into. i can read in the size of the pic from the file, width and height, but how can i resize the array to match the size of the image?

    Thanks for your help!

  2. #2
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368

    Re: passing in command line stuff and my first script and al

    Any ideas what the type casting should look like to get an int out of the command line?
    Type casting wouldn't help here. Look for 'atoi' and 'strtol' standard libarary functions.

    could you guys show me how a script could be used to run these commands on a whole folder of images? Ive never done one before!
    You could use a shell loop to traverse a range of files and invoke you program on each of them, for example:
    Code:
    for filename in *.jpg
    do
        # make a new name for output file based on the current name
        fileout="`basename $filename .jpg`_out.jpg"
        blur $filename $fileout
    done
    Of course it can much more elaborated, look at http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html to lear more about bash.

    i can read in the size of the pic from the file, width and height, but how can i resize the array to match the size of the image?
    Sound like a good application for dynamic memory management: look for 'malloc', 'realloc' and 'free' library functions.

    Thanks for your help!
    You are welcome. I know my answer only scratches the surface of what could be written, but my purpose is to give you general directions rather than complete answers.
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  3. #3
    Linux Newbie
    Join Date
    Nov 2004
    Posts
    239
    A quick question about this bit...

    for filename in *.jpg
    do
    # make a new name for output file based on the current name
    fileout="`basename $filename .jpg`_out.jpg"
    blur $filename $fileout
    done



    is that limited to the directory in which the script is run? can you specify it to look for all *.jpg in say /home/username/pictrures ect?

    I havnt tried the dynamic array stuff yet, im putting it off while i play with this instead!

  4. #4
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Right, it is limited to the directory when the script is being run. This needn't be the same dir the script is located, as you can cd to your image stash and execute, let's say:
    Code:
    /home/username/bin/scritpname.sh
    Anyhow, it's not a chore to change the script to support a command line parameter specifying a dir, for example:
    Code:
    for filename in $1/*.jpg
    do
    # body loop as previously
    done
    Now you should be able to invoke it by saying:
    Code:
    scriptname.sh /home/username/pictures
    Regards
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  5. #5
    Linux Newbie
    Join Date
    Nov 2004
    Posts
    239
    Right....


    Ive got the script file working fine, thanks for your help with that!


    Im still trying to make my prog handle different file sizes tho. I can read in the width and height from the picture (im using bitmaps) but I cant resize the array dynamically, it just dosnt work!

    I declare the array like this....
    Code:
    struct PIXEL {
      //store colour data in r,b,g format
      unsigned char r,b,g;
      } **bitmap;
    If i do typdef struct PIXLE i get loads of errors from all over the place, not sure why! But it makes it complicated!


    when i resize the array i do this:



    Code:
    void setsize() {
        int i;
        	bitmap = malloc((image_hieght * 3));
    	if(bitmap == NULL)
    		{
    		fprintf(stderr, "out of memory\n");
    		
    		}
    	for&#40;i = 0; i < image_hieght; i++&#41;
    		&#123;
    		bitmap&#91;i&#93; = malloc&#40;&#40;image_width * 3&#41;&#41;;
    		if&#40;bitmap&#91;i&#93; == NULL&#41;
    			&#123;
    			fprintf&#40;stderr, "out of memory\n"&#41;;
    			
    			&#125;
    		&#125; 
    &#125;

    It dosnt produce any errors, but when it outputs the file, it is all black.

    and in the code, if i printf a random pixel, its values are way off, 1157642 ect, but should be less than 255.

    Any help would be much appriciated! when i have my awsome picture tools working, I'l give share the code with anyone who wants it!

  6. #6
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    1) How exactly do you use typedef and what problem do you suppose it to fix? Perhaps something like this?
    Code:
    typedef struct &#123;unsigned char r, b,g;&#125; PIXEL;
    PIXEL **bitmap;
    2) When it comes to memory allocation then:
    Code:
    bitmap = malloc&#40;&#40;image_hieght * 3&#41;&#41;;
    I'm not sure why you use 3 in this place. Perhaps it has something to do with bitmap structure. But even then note that malloc expects storage size in bytes, so if you want to allocate array of size image_hieght * 3 PIXEL pointers you should multiply this value by size of each pointer (or, to put it another way by size of each array cell). This single size can be obtained with sizeof(*bitmap) or sizeof(bitmap[0]), whichever you like. Alike, in the second loop it would be a good habit to write malloc(image_width * sizeof(**bitmap)).
    3) If you want to resize, note that malloc only allocates the memory, it doesn't initialize it to any value, so it's no wonder the pixels are random. You should either copy the old values manually in some loop or use "realloc" function that changes size of previously allocated array and does the copying for you automatically.

    Finally, I know the pains that everyone has to get through in order to grasp dynamic memory allocation in C, but it's certainly worth the effort. Good luck.
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •