Timo Willemsen
Timo Willemsen

Reputation: 8857

Type problems with bytes and shorts

Hey everyone. I have a function that works both with short input values, as with byte input values, here pixelData is an array filled with either bytes or shorts.

       byte oColor;
        if (pixelData[counter/3] < minValue)
            oColor = 0;
        else if (pixelData[counter/3] > maxValue)
            oColor = 255;
        else
            oColor = (byte)(((pixelData[counter/3] - (WindowLevel - (WindowWidth / 2))) * 255) / WindowWidth);

Then I have the following code:

bool isShort = ReadIsShort();
if(isShort){
 shortArray = ReadShortArray();
} else {
 byteArray = ByteArray();
}

To get either the short or the byte array. However, I cannot use them in function above. Since something like this doesn't work.

var pixelData;
if(IsShort)
 pixelData = shortArray;
else
 pixelData = byteArray;

Anyone know how I should program this, since this clearly is a design flaw.

Upvotes: 1

Views: 159

Answers (5)

Jeff Ogata
Jeff Ogata

Reputation: 57813

You could try a generic method using Comparer<T>, but I'm not sure how this would affect performance:

void ProcessPixelData<T>(T[] pixelData, T minValue, T maxValue)
{
    // ...

    byte oColor;
    if (Comparer<T>.Default.Compare(pixelData[counter / 3], minValue) < 0)
        oColor = 0;
    else if (Comparer<T>.Default.Compare(pixelData[counter / 3], maxValue) > 0)
        oColor = 255;
    else
        oColor = (byte)(((pixelData[counter/3] - (WindowLevel - (WindowWidth / 2))) * 255) / WindowWidth);

    // ...
}

Actually, couldn't ByteArray() return a short[]? Then you could just work with pixelData as a short[] for both cases.

Upvotes: 0

MartinStettner
MartinStettner

Reputation: 29174

Since C# is a strongly typed language, every variable must have a well-defined type. Therefore, pixelData can only be either a short or a byte array. So far there's no design flaw here imo.

I assume, that your computations are more or less performance-critical (since there are pixels involved :)), so I won't repeat the suggestion of using boxing (cast to object[]) or creating Lists or similar things.

Even using generics is not an option, since you cannot give arithemtic type constraints for generic functions (see this qestion) The only solution I can think of is to duplicate the code and write two explicit versions, one for each data type.

Upvotes: 1

Richard J. Ross III
Richard J. Ross III

Reputation: 55583

You could change pixelData to an object[] and use

if (isShort)
    Array.Copy(shortData, pixelData, shortData.Length);
else
    Array.Copy(byteData, pixelData, byteData.Length);

Upvotes: 0

davisoa
davisoa

Reputation: 5439

You need to declare a type for pixelData - one that both a short or byte would convert to, or could contain your array.

Can you change the array of bytes to a List of shorts, since that wouldn't lose any data? Then you can change your function to just act on one type - something like List<Short>.

Upvotes: 0

Phil Hunt
Phil Hunt

Reputation: 8541

You could try obtaining object[] arrays and use this as your final else. I'm not sure I'm crazy about the problem to begin with, though. Is there any way you can go back and refactor so that there is a consistent data type?

oColor = (byte)(((Convert.ToInt16(pixelData[counter / 3]) - (WindowLevel - (WindowWidth / 2))) * 255) / WindowWidth);

Upvotes: 1

Related Questions