Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6022833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:54:56+00:00 2026-05-23T03:54:56+00:00

Hey so i’m trying to make a function that takes in an image refrence

  • 0

Hey so i’m trying to make a function that takes in an image refrence and sf::IntRect

(this has 4 ints representing the top,bottom,left, and right sides of a rectangle. this rectangle represents the part of the image being displayed, but does not actually cut off the pixels outside the rectangle)

and then creates a new condensed image by cutting off the pixels outside the IntRect. I figured the only way i could do this was by making a 2d pixel array of the same dimensions as the IntRect, and then filling it by iterating through the image, but since i do not know the IntRect’s dimensions i can’t make a constant array……

This problem continually comes up, i assume doing a bunch of vector work and convesions to c_style arrays would cost a lot of performance..

Is there some simple sollution to heavily manipulating/ changing the dimensions and colors of images?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:54:57+00:00Added an answer on May 23, 2026 at 3:54 am

    The usual way to store and access 2-dimensional images (or any 2-dimensional matrix with variable size), is to allocate a 1-dimensional array of suitable size (width * height, or (width + padding) * height), and calculate the index into the array “manually” (y * stride + x, where stride is width + padding).

    (I normally use std::vector for that 1-dimensional array, but that’s a different story)

    The usual way to reference 2D image data (reference as in pass-by-reference), is to pass a tuple of:

    • void* imageData — the address of the top-left pixel
    • size_t stride — the number of bytes to add to imageData to get to the next line
    • size_t width
    • size_t height
    • SomeEnum pixelFormat — the pixel format of the image (can be omitted if there is only one)

    Of course, if there is only one Pixel-Format, you can use a typed pointer, and specify the stride in units of that type (instead of bytes).

    Using such a reference, you can access pixels in a loop quite easily and quite efficient:

    size_t const bytesPerPixel = GetBytesPerPixel(ref->pixelFormat);
    
    for (size_t y = 0; y < ref->height; y++)
    {
        unsigned char* currentLine =
            static_cast<unsigned char*>(ref->imageData) + ref->stride * y;
    
        for (size_t x = 0; x < ref->width; x++)
        {
            // any modern compiler will optimize the multiplication below to
            // an incremental addition
            unsigned char* currentPixel = currentLine + bytesPerPixel * y;
    
            // do something to the pixel data at
            // currentPixel[0] ... currentPixel[bytesPerPixel - 1]
        }
    }
    

    The nice thing about passing image references that way is, that you can always “adjust” such a reference to point to a sub-rect of the original image. You just have to adjust the values accordingly: make imageData point to the top-left pixel of the sub-rect and set width and height to the width and height of the sub-rect. stride stays the same.

    That means you don’t have to “materialize” the cropped image, you can just pass a reference to a sub-rect to any function, and it will operate on that sub-rect just as it would on a “complete” image.

    And if you really want to “materialize” a cropped image, you should now have enough information to do that too. At least I hope so 🙂

    EDIT: Since you were very explicit about the sf::IntRect part, but then only wrote “image” instead of sf::Image, I assumed you were talking about something you manage yourself, not an sf::Image. Well…

    If you just want to copy a sub-rect of an sf::Image to another sf::Image, you can just do this:

    sf::Image sourceImage = ...;
    sf::IntRect subRect = ...;
    
    // construct an empty sf::Image with the appropriate dimensions
    sf::Image newImage(subRect.GetWidth(), subRect.GetHeight());
    
    // copy the pixel data into the new image
    newImage.Copy(sourceImage, 0, 0, subRect);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, i am trying to make a little app that will serve multiple
Hey there, I'm at the moment trying to make a product management application. The
Hey, I'm trying to make an Address book for my site and I was
Hey guys, I just started learning C++ and I have this code that I
Hey I'm trying to creare a link that passes the link's text directly to
Hey just a quick question for anyone who has done this. I want to
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey having some trouble trying to maintain transparency on a png when i create
Hey all, I am not quite getting why this doesn't seem to be working.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.