Guest User

Untitled

a guest
Mar 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | None | 0 0
  1. CODEC_ERROR ConvertBGRA64ToFrame_4444_16s(uint8_t *data, int pitch, FRAME *frame,
  2.                                           uint8_t *buffer, int precision)
  3. {
  4.     //#pragma unused(buffer);
  5.  
  6.     //TODO: Add code to write the alpha channel into the fourth plane
  7.     int num_channels;
  8.  
  9.     uint8_t *rgb_row_ptr = data;
  10.     int rgb_row_pitch = pitch;
  11.  
  12.     PIXEL *color_plane[FRAME_MAX_CHANNELS];
  13.     int color_pitch[FRAME_MAX_CHANNELS];
  14.     int frame_width;
  15.     int frame_height;
  16.     int display_height;
  17.     int row;
  18.     int i;
  19.  
  20.     uint8_t *r_row_ptr;
  21.     uint8_t *g_row_ptr;
  22.     uint8_t *b_row_ptr;
  23.     uint8_t *a_row_ptr = NULL;
  24.  
  25.     int r_row_pitch;
  26.     int g_row_pitch;
  27.     int b_row_pitch;
  28.     int a_row_pitch = 0;
  29.  
  30.     //int shift = 20;           // Shift down to form a 10-bit pixel
  31.     int shift = 16 - precision;
  32.     int channel_depth;
  33.  
  34.     bool alpha_flag;
  35.  
  36.     //const int max_rgb = USHRT_MAX;
  37.  
  38.     //TODO: Need to return error codes
  39.     assert(frame != NULL);
  40.     if (! (frame != NULL)) {
  41.         return CODEC_ERROR_INVALID_ARGUMENT;
  42.     }
  43.  
  44.     assert(frame->format == FRAME_FORMAT_RGB || frame->format == FRAME_FORMAT_RGBA);
  45.     if (! (frame->format == FRAME_FORMAT_RGB || frame->format == FRAME_FORMAT_RGBA)) {
  46.         return CODEC_ERROR_BAD_FRAME;
  47.     }
  48.  
  49.     alpha_flag = (frame->format == FRAME_FORMAT_RGBA);
  50.     num_channels = (alpha_flag ? 4 : 3);
  51.  
  52.     // Check that the frame was allocated with enough channels
  53.     assert(frame->num_channels >= num_channels);
  54.  
  55.     //TODO: Set the alpha flag and number of channels using the values in the frame data structure
  56.  
  57.     display_height = frame->display_height;
  58.  
  59.     // Get pointers to the image planes and set the pitch for each plane
  60.     for (i = 0; i < num_channels; i++)
  61.     {
  62.         IMAGE *image = frame->channel[i];
  63.  
  64.         // Set the pointer to the individual planes and pitch for each channel
  65.         color_plane[i] = image->band[0];
  66.         color_pitch[i] = image->pitch;
  67.  
  68.         // The first channel establishes the frame dimensions
  69.         if (i == 0) {
  70.             frame_width = image->width;
  71.             frame_height = image->height;
  72.         }
  73.     }
  74.  
  75.     // This routine does not handle the RG30 format
  76.     channel_depth = pitch * 8 / frame_width;
  77.     assert(channel_depth != 32);
  78.     if (! (channel_depth != 32)) {
  79.         return CODEC_ERROR_BADFORMAT;
  80.     }
  81.  
  82.     // Set the row pointers for each channel to the correct plane
  83.     r_row_ptr = (uint8_t *)color_plane[1];      r_row_pitch = color_pitch[1];
  84.     g_row_ptr = (uint8_t *)color_plane[0];      g_row_pitch = color_pitch[0];
  85.     b_row_ptr = (uint8_t *)color_plane[2];      b_row_pitch = color_pitch[2];
  86.     if (alpha_flag)
  87.     {
  88.         a_row_ptr = (uint8_t *)color_plane[3];  a_row_pitch = color_pitch[3];
  89.     }
  90.  
  91.     for (row = 0; row < display_height; row++)
  92.     {
  93.         // Start at the leftmost column
  94.         int column = 0;
  95.  
  96.         //TODO: Process each row by calling an optimized subroutine
  97. #if (1 && XMMOPT)
  98.  
  99. #endif
  100.  
  101.         // Pointer into the RGB input row
  102.         PIXEL16U *rgb_ptr = (PIXEL16U *)rgb_row_ptr;
  103.  
  104.         // Pointers into the output rows for each plane
  105.         PIXEL16U *r_ptr = (PIXEL16U *)r_row_ptr;
  106.         PIXEL16U *g_ptr = (PIXEL16U *)g_row_ptr;
  107.         PIXEL16U *b_ptr = (PIXEL16U *)b_row_ptr;
  108.         PIXEL16U *a_ptr = (PIXEL16U *)a_row_ptr;
  109.  
  110.         // Process the rest of the column
  111.         for (; column < frame_width; column ++)
  112.         {
  113.             int r, g, b, a;
  114.  
  115.             // Load the first set of ARGB values
  116.             a = *(rgb_ptr++);
  117.             r = *(rgb_ptr++);
  118.             g = *(rgb_ptr++);
  119.             b = *(rgb_ptr++);
  120.  
  121.             // Shift the 16-bit pixels to the encoded precision
  122.             *(r_ptr++) = r >> shift;
  123.             *(g_ptr++) = g >> shift;
  124.             *(b_ptr++) = b >> shift;
  125.  
  126.             if (alpha_flag)
  127.             {
  128.                 //*(a_ptr++) = a >> shift;
  129.                 a >>= shift;
  130.                 // This help preserve the encoding of alpha channel extremes 0 and 1.  Alpha encoding curve
  131.                 if(a > 0 && a < (255<<4))
  132.                 {
  133.                     // step function 0 = 0, 0.0001 = 16/255, 0.9999 = 239/256, 1 = 255/256
  134.                     a *= 223;
  135.                     a += 128;
  136.                     a >>= 8;
  137.                     a += 16<<4;
  138.                 }
  139.             //  if (a < 0) a = 0;
  140.             //  if (a > YU10_MAX) a = YU10_MAX;
  141.                 *(a_ptr++) = a;
  142.             }
  143.         }
  144.  
  145.         // Advance the input row pointer
  146.         rgb_row_ptr += rgb_row_pitch;
  147.  
  148.         // Advance the output row pointers
  149.         r_row_ptr += r_row_pitch;
  150.         g_row_ptr += g_row_pitch;
  151.         b_row_ptr += b_row_pitch;
  152.         a_row_ptr += a_row_pitch;
  153.     }
  154.  
  155.     // Successful conversion
  156.     return CODEC_ERROR_OKAY;
  157. }
Add Comment
Please, Sign In to add comment