Get normalized device coordinate of an image pixel
You can modulus by row length to retrieve the x coordinate (think about the x coordinate &quot;wrapping around&quot; after each line), and division by row length to get the y coordinate (inverting the multiply you have by imageWidth). We subtract one from the width and height to ensure the last pixel in the row/column maps to 1.0. Note that this isn't &quot;Normalized Device Coordinates&quot;, since that implies a 3D quantity which also stores depth, and that all coordinates lie in [-1,1].
struct Point2D { float x; float y; };

Point2D GetNormalizedPixelCoords(int pixelIdx, int imageWidth, int imageHeight) {
    Point2D point;
    point.x = (pixelIdx % imageWidth) / (float)(imageWidth-1);
    point.y = (pixelIdx / imageWidth) / (float)(imageHeight-1);
    return point;
}