FrameDataSp PreprocessScreen(const ALEScreen& raw_screen) { assert(raw_screen.width() == kRawFrameWidth); assert(raw_screen.height() == kRawFrameHeight); const auto raw_pixels = raw_screen.getArray(); auto screen = std::make_shared<FrameData>(); assert(kRawFrameHeight > kRawFrameWidth); const auto x_ratio = kRawFrameWidth / static_cast<double>(kCroppedFrameSize); const auto y_ratio = kRawFrameHeight / static_cast<double>(kCroppedFrameSize); for (auto i = 0; i < kCroppedFrameSize; ++i) { for (auto j = 0; j < kCroppedFrameSize; ++j) { const auto first_x = static_cast<int>(std::floor(j * x_ratio)); const auto last_x = static_cast<int>(std::floor((j + 1) * x_ratio)); const auto first_y = static_cast<int>(std::floor(i * y_ratio)); const auto last_y = static_cast<int>(std::floor((i + 1) * y_ratio)); auto x_sum = 0.0; auto y_sum = 0.0; uint8_t resulting_color = 0.0; for (auto x = first_x; x <= last_x; ++x) { double x_ratio_in_resulting_pixel = 1.0; if (x == first_x) { x_ratio_in_resulting_pixel = x + 1 - j * x_ratio; } else if (x == last_x) { x_ratio_in_resulting_pixel = x_ratio * (j + 1) - x; } assert( x_ratio_in_resulting_pixel >= 0.0 && x_ratio_in_resulting_pixel <= 1.0); for (auto y = first_y; y <= last_y; ++y) { double y_ratio_in_resulting_pixel = 1.0; if (y == first_y) { y_ratio_in_resulting_pixel = y + 1 - i * y_ratio; } else if (y == last_y) { y_ratio_in_resulting_pixel = y_ratio * (i + 1) - y; } assert( y_ratio_in_resulting_pixel >= 0.0 && y_ratio_in_resulting_pixel <= 1.0); const auto grayscale = PixelToGrayscale( raw_pixels[static_cast<int>(y * kRawFrameWidth + x)]); resulting_color += (x_ratio_in_resulting_pixel / x_ratio) * (y_ratio_in_resulting_pixel / y_ratio) * grayscale; } } (*screen)[i * kCroppedFrameSize + j] = resulting_color; } } return screen; }
void Interface::aleGetScreen() { const auto raw_pixels = ale->getScreen().getArray(); auto screen = std::make_shared<FrameData>(); double yRatio = (1.0*(ALE_HEIGHT))/(1.0*cropHV); double xRatio = (1.0*(ALE_WIDTH))/(1.0*cropWV); for(int i = 0; i < cropHV; ++i) { for(int j = 0; j < cropWV; ++j) { int firstX = (int)(std::floor(j*xRatio)); int lastX = (int)(std::floor((j+1)*xRatio)); int firstY = (int)(std::floor(i*yRatio)); int lastY = (int)(std::floor((i+1)*yRatio)); unsigned int resColor = 0.0; for(int x = firstX; x <= lastX; ++x) { double xRatioInResPixel = 1.0; if(x == firstX) xRatioInResPixel = x + 1.0 - j*xRatio; else if(x == lastX) xRatioInResPixel = xRatio*(j+1)-x; for(int y = firstY; y <= lastY; ++y) { double yRatioInResPixel = 1.0; if(y == firstY) yRatioInResPixel = y + 1.0 - i*yRatio; else if(y == lastY) yRatioInResPixel = yRatio*(i+1)-y; int grayscale = PixelToGrayscale(raw_pixels[static_cast<int>(y * ALE_WIDTH + x)]); resColor += (xRatioInResPixel/xRatio)*(yRatioInResPixel/yRatio)*grayscale; } } //myFrmFile << resColor << " "; if(i >= cropT && i < (cropT + cropH) && j >= cropL && j < (cropL + cropW)) (*screen)[j-cropL+(i-cropT)*cropW] = resColor; } //myFrmFile << std::endl; } lastFrmGrayInfo[0] = screen; }