DucK196

[LAB1-VGA] Image to Hex v3

Dec 19th, 2017
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.35 KB | None | 0 0
  1. function [ IMAGE, MASK ] = im_hex( image, width, height, filter )
  2.     if(nargin < 4) filter = 'FF'; end                                       % default BG filter is white        
  3.    
  4.     x = imread(image);
  5.     x = imresize(x, [height, width]);                                       % resize image if needed
  6.     r = x(:, :, 1); r = floor(double(r)/32);                                % convert to 3-bit RRR
  7.     g = x(:, :, 2); g = floor(double(g)/32);                                % convert to 3-bit GGG
  8.     b = x(:, :, 3); b = floor(double(b)/64);                                % convert to 2-bit BB
  9.  
  10.     out = ''; msk = '';
  11.     for row = 1:height
  12.         out = [out, '('];
  13.         msk = [msk, '("'];
  14.         for col = 1:width
  15.             color = bin2dec([dec2bin(r(row, col), 3), dec2bin(g(row, col), 3), dec2bin(b(row, col), 2)]);
  16.             if(color == hex2dec(filter))
  17.                 msk = [msk, '0'];
  18.             else
  19.                 msk = [msk, '1'];
  20.             end
  21.             out = [out, sprintf('x"%.2x"', color)];
  22.             if(col < width)
  23.                 out = [out, ','];
  24.             end
  25.         end
  26.         out = [out, ')'];
  27.         msk = [msk, '")'];
  28.         if(row < height)
  29.             out = [out, sprintf(',\n')];
  30.             msk = [msk, sprintf(',\n')];
  31.         end    
  32.     end
  33.     IMAGE = out;
  34.     MASK = msk;
  35. end
Advertisement
Add Comment
Please, Sign In to add comment