开发者

Type problems with bytes and shorts

开发者 https://www.devze.com 2023-01-23 15:59 出处:网络
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.

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 (pix开发者_如何学PythonelData[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.


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.


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);


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>.


You could change pixelData to an object[] and use

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号