copying the particular pixel in an image

This is a discussion on copying the particular pixel in an image within the C Programming forums, part of the General Programming Boards category; how to write a function in c for copying the particular pixel in one image to another.... picture2(s,j) = picture1(i,j);...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    33

    copying the particular pixel in an image

    how to write a function in c for copying the particular pixel in one image to another....

    picture2(s,j) = picture1(i,j);

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    Are you using a specific image (manipulation) library, a GUI toolkit, or writing your own code from scratch?

    Please show us the code you have.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    writing own code

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    it's a vb code im trying to convert into c

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    What are your inputs and outputs? Image files in some particular format?

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    picture1,picture2 as picture

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by prathiksa View Post
    picture1,picture2 as picture
    You need to provide information on how the image data is represented and accessed, it can be for example accessed directly in a 2d array or linearly in a ordinary array, or with functions provided by a library.

    Since you are asking about copying of pixels, should we assume that you already loaded the images from disk and now have them accessible in memory in some form?
    Last edited by Subsonics; 10-22-2012 at 06:48 AM.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    it is accessed linearly in ordinary array
    array[289];
    have an already loaded image
    after some manipulations going to write the image (from picture 1 to picture 2)
    i just want a function to write the function about copying pixels
    i have declared image as char image

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by prathiksa View Post
    it is accessed linearly in ordinary array
    array[289];
    have an already loaded image
    after some manipulations going to write the image (from picture 1 to picture 2)
    i just want a function to write the function about copying pixels
    i have declared image as char image
    The question then becomes, how do I assign a value to an array.

    But how is a pixel represented in the array? Is it a color image? For an rgba image you may have something like an r,g,b,a sequence appearing in a cycles. Or a pixel may be represented with a struct for example. Once you know this the assignment is very simple, image_a[i] = image_b[i].

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    now image_a copies the entire image_b right?

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by prathiksa View Post
    now image_a copies the entire image_b right?
    No, it copies what ever is at index i, that may be a pixel or a color component depending on how the image is represented. What type is the array declared as?

    If you have simply loaded the entire image file from disk without any image library you will also have header information in there, in that case you need to make sure you do not make any changes to the header. The image data may also contain padding or be compressed, which you also need to take into consideration, if this is what you have done.
    Last edited by Subsonics; 10-23-2012 at 08:45 AM.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    thanks

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    one more question
    how to change the size of an array replacing redim

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    If your code is not too long then you could just paste it (the vb code and the C code you are replacing it with) here. Then we can actually see what you are trying to do and help you out better.

    The closest things to redim is realloc, but we cant tell if that is even the right thing for you to use without seeing the code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    The reason I asked was because of how easy the portable pixmap (ppm, one of the netpbm formats) is.

    The minimal C99 code I need (and use for quick sketches in particular) for image generation is
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    
    typedef struct {
        int  width;
        int  height;
        unsigned char data[];
    } ppm_t;
    
    
    static inline ppm_t *ppm_create(const int width, const int height)
    {
        ppm_t *image;
    
        if (width < 1 || height < 1) {
            errno = EINVAL;
            return NULL;
        }
    
        image = malloc(sizeof (ppm_t) + 3 * width * height);
        if (!image) {
            errno = EINVAL;
            return NULL;
        }
    
        image->width = width;
        image->height = height;
    
        return image;
    }
    
    
    static inline int ppm_setpixel(ppm_t *const image, const int x, const int y, const int color)
    {
        if (image && x >= 0 && y >= 0 && x < image->width && y < image->height) {
            unsigned char *const p = image->data + 3*(y * image->width + x);
            p[0] = color;
            p[1] = color >> 8;
            p[2] = color >> 16;
            return color & 0xFFFFFF;
        } else
            return -1;
    }
    
    
    static inline int ppm_getpixel(const ppm_t *const image, const int x, const int y)
    {
        if (image && x >= 0 && y >= 0 && x < image->width && y < image->height) {
            const unsigned char *const p = image->data + 3*(y * image->width + x);
            return (int)p[0] + ((int)p[1] << 8) + ((int)p[2] << 16);
        } else
            return -1;
    }
    
    
    static int ppm_save(const ppm_t *const image, FILE *const out)
    {
        if (!image || !out)
            return errno = EINVAL;
    
        if (ferror(out))
            return errno = EIO;
    
        fprintf(out, "P6\n%d %d\n255\n", image->width, image->height);
        if (fwrite(image->data, 3*image->width*image->height, 1, out) != 1)
            return errno = EIO;
    
        if (fflush(out) || ferror(out))
            return errno = EIO;
    
        return 0;
    }
    The ppm_setpixel() and ppm_getpixel() don't mind if you go out of bounds (it checks for valid coordinates), and take and return the color in the hexadecimal web-friendly format, 0xFFFFFF (0xFF0000 for red, 0x00FF00 for green, 0xFFFFFF for white, 0x000000 for black, and so on).

    If working with non-RGB color spaces, or colors generated by floating-point algorithms, you can use something like the following function to compute your color values. This one takes the RGB components as floating-point numbers in the range [0, 1]:
    Code:
    static inline int ppm_color(const float red, const float green, const float blue)
    {
        int  color;
    
        if (red <= 0.0f)
            color = 0x000000;
        else
        if (red < 1.0f)
            color = (int)(0.5f + 255.0f * red) << 16;
        else
            color = 0xFF0000;
    
        if (green >= 1.0f)
            color |= 0x00FF00;
        else
        if (green > 0.0f)
            color |= (int)(0.5f + 255.0f * green) << 8;
    
        if (blue >= 1.0f)
            blue |= 0x0000FF;
        else
        if (blue > 0.0f)
            color |= (int)(0.5f + 255.0f * blue);
    
        return color;
    }
    For the cases where I need alpha or blending, I tend to use 32-bit pixels, and convert to 24-bit at save time. Using 32-bit unsigned integers for ARGB (alpha-red-green-blue) makes it very simple to e.g. blend two pixel values easily.

    Reading a PPM image is a bit more complicated, because there are a number of variants you may encounter, and because there might be comment lines between the format identifier and width and height information . If I read PPM inputs, I usually support P6 (ppm, rgb color) and P5 (pgm, grayscale) variants, although there are also P4 (pbm, black-and-white) and ASCII versions of all three (P3, P2, and P1, respectively). The easiest way to do that is to have one function read the header (format, width, height, and if not P4/P1, then the maximum component value), then read the data using a format-specific helper function.

    For those interested in high dynamic range imaging (HDR), note that the PPM format does support 16 bits per component (48 bits per pixel). The byte order then is Gg (for grayscale) or RrGgBb (for RGB), the most significant byte first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get the rgb pixel values from a bmp image
    By anan in forum C Programming
    Replies: 3
    Last Post: 03-16-2011, 05:21 PM
  2. getting pixel data from image using libpng
    By mamacken in forum C Programming
    Replies: 5
    Last Post: 01-11-2010, 10:59 AM
  3. Reducing size of image pixel data to display in window
    By synthetix in forum C Programming
    Replies: 2
    Last Post: 11-16-2009, 04:10 AM
  4. reading gif image pixel Information-help
    By kapil1089thekin in forum C Programming
    Replies: 2
    Last Post: 05-17-2008, 01:13 AM
  5. Calculate the pixel intensities in an image...
    By Sharmontime in forum C Programming
    Replies: 10
    Last Post: 06-08-2006, 01:19 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21