简体   繁体   English

在Retina设备上升级图像

[英]Upscaling images on Retina devices

I know images upscale by default on retina devices, but the default scaling makes the images blurry. 我知道默认情况下在视网膜设备上图像是按比例放大的,但是默认比例缩放会使图像模糊。

I was wondering if there was a way to scale it in nearest-neighbor mode, where there are no transparent pixels created, but rather each pixel multiplied by 4, so it looks like it would on a non retina device. 我想知道是否有一种方法可以在最近邻居模式下缩放它,其中没有创建透明像素,而是将每个像素乘以4,所以看起来就像在非视网膜设备上一样。

Example of what I'm talking about can be seen in the image below. 下图显示了我正在谈论的示例。 example http://cclloyd.com/downloads/sdfsdf.png 范例http://cclloyd.com/downloads/sdfsdf.png

CoreGraphics will not do a 2x scale like that, you need to write a bit of explicit pixel mapping logic to do something like this. CoreGraphics不会像这样进行2倍缩放,您需要编写一些显式的像素映射逻辑来执行类似的操作。 The following is some code I used to do this operation, you would of course need to fill in the details as this operates on an input buffer of pixels and writes to an output buffer of pixels that is 2x larger. 以下是我用来执行此操作的一些代码,您当然需要填写详细信息,因为它在像素的输入缓冲区上进行操作并写入2倍大的像素输出缓冲区。

  // Use special case "DOUBLE" logic that will simply duplicate the exact
  // RGB value from the indicated pixel into the 2x sized output buffer.

  int numOutputPixels = resizedFrameBuffer.width * resizedFrameBuffer.height;

  uint32_t *inPixels32 = (uint32_t*)cgFrameBuffer.pixels;
  uint32_t *outPixels32 = (uint32_t*)resizedFrameBuffer.pixels;

  int outRow = 0;
  int outColumn = 0;

  for (int i=0; i < numOutputPixels; i++) {
    if ((i > 0) && ((i % resizedFrameBuffer.width) == 0)) {
      outRow += 1;
      outColumn = 0;
    }

    // Divide by 2 to get the column/row in the input framebuffer
    int inColumn = outColumn / 2;
    int inRow = outRow / 2;

    // Get the pixel for the row and column this output pixel corresponds to   
    int inOffset = (inRow * cgFrameBuffer.width) + inColumn;
    uint32_t pixel = inPixels32[inOffset];

    outPixels32[i] = pixel;

    //fprintf(stdout, "Wrote 0x%.10X for 2x row/col %d %d (%d), read from row/col %d %d (%d)\n", pixel, outRow, outColumn, i, inRow, inColumn, inOffset);

    outColumn += 1;
  }

This code of course depends on you creating a buffer of pixels and then wrapping it back up into CFImageRef. 当然,此代码取决于您创建像素缓冲区,然后将其包装回CFImageRef。 But, you can find all the code to do that kind of thing easily. 但是,您可以找到所有代码来轻松完成此类操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM