C++DICOMファイル窓幅調整

1621 ワード

void Windowing(vtkSmartPointer imageData, int window_width, int window_center)
{

    int width = imageData->GetDimensions()[0];
    int height = imageData->GetDimensions()[1];
    QImage image(width, height, QImage::Format_RGB32);
    short *colorsPtr = reinterpret_cast(imageData->GetScalarPointer());

    // Loop over the vtkImageData contents.
    QRgb *rgbPtr = reinterpret_cast(image.bits());
    int wh = width * height;
    const int num = imageData->GetNumberOfScalarComponents();

    double min = (2 * window_center - window_width) / 2.0 + 0.5;
    double max = (2 * window_center + window_width) / 2.0 + 0.5;

    for (int y = 0; y < height; ++y)
    {
        for (int x = 0; x < width; ++x)
        {
            int ww = (height - 1 - y) * width + x;
            int w = y * width + x;
            //colorsPtr[ww] = colorsPtr[ww] - 32768;
         
            short pixel = colorsPtr[ww];

            int c = (pixel - min)*255.0 / (double)(max - min);
            c = c > 255 ? 255 : c;
            c = c < 0 ? 0 : c;
            QColor color(c, c, c);
            QRgb cr = color.rgb();
            rgbPtr[w] = cr;
        }
    }


    QByteArray ba;
    QBuffer buf(&ba);

    int quality = 100;
    image.save(&buf, "jpg", quality);//png

#ifdef DEBUG
    int g_count = 0;
    {
        std::string jpgfile = "SaveJpg" + std::to_string(long long(g_count++)) + ".jpg";
        bool b = image.save(jpgfile.c_str(), nullptr, quality);
    }
#endif // DEBUG
}