My unsigned short pointers return unexpected results. What for?

I write in C for OSX, working on a 64-bit machine in 32-bit mode. I am going to use GCC for 386. The project is large; I have not seen any strange behavior from the compiler (perhaps so far.) The application is multi-threaded, and this code should be, but is currently running single-threaded. I compile using the independent clib position and using posix streams in streaming. This code, by the way, acts exactly the same if I enter it.

The following is a simplified procedure that demonstrates the problem in its simplest form. Basically, I move 16-bit image channels here from one set of three RGB channels (mr, mg, mb) to another set of 3 RGB channels (lr, lg, lb) of exactly the same size. The snippet, as I am going to dump it, works fine:

void lrip_me(   unsigned short *mr, // RIP from source image
                unsigned short *mg,
                unsigned short *mb,
                unsigned short *lr, // into RGB layer
                unsigned short *lg,
                unsigned short *lb,
                struct layer *lay,
                long start,long finish)
{
long xw,yw;
long xf,yf;
long x,y;
unsigned long offset;

    xw = lay->parent->x;
    yw = lay->parent->y;
    xf = xw - 1;
    yf = yw - 1;

    for (y=start; y<finish; y++)
    {
        for (x=0; x<xw; x++)
        {
            offset = (y * xw) + x;
            if (x==0 || x==xf || y==0 || y==yf) // then on edge
            {
                lr[offset] = mr[offset];
                lg[offset] = mg[offset];
                lb[offset] = mb[offset];
            }
            else
            {
                lr[offset] = mr[offset];
                lg[offset] = mg[offset];
                lb[offset] = mb[offset];
            }
        }
    }
}

As you can see, the action on the edge of the image and inside the edges is the same; I just move the data. And it works - the output of this image in the channels lr, lg and lb.

BUT. If inside the else clause I change the lines to read ...

            lr[offset] = mr[offset-xw];
            lg[offset] = mg[offset-xw];
            lb[offset] = mb[offset-xw];

... , , , . . , , . ...

            lr[offset] = mr[offset-1];
            lg[offset] = mg[offset-1];
            lb[offset] = mb[offset-1];

... , , . . - .

// * (mr + offset-1)// . objc. , , , . . , . , , , , .

, . , - , .

; RIP ( ), , , , 8 , , . -; 8- lookaround , , 8, :

ooo
oxo
ooo

RIP, . (hah!) Situtation, .

+3
1

, 2D- .

offset = (y * xw) + x;

, .

+1

Source: https://habr.com/ru/post/1789414/


All Articles