File: BitsPlusPackColorImage.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (73 lines) | stat: -rw-r--r-- 2,059 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function outImage = BitsPlusPackColorImage(inImage,SPACESCALE,COLORSCALE)
% [outImage,inImage] = BitsPlusPackColorImage(inImage,[SPACESCALE],[COLORSCALE]))
%
% Convert an image for Bits++, assuming it will
% displayed on an even pixel boundary and that
% it has an even number of columns.
%
% Image is resized if SPACESCALE == 1 (default).
% Otherwise scaling is assumed to have been done in advance.
%
% Assume input is in range 0-1 if COLORSCALE == 1 (default).
% Assume input is in range 0-65535 and be integer if COLORSCALE == 0.
%
% If scaling in space, must scale in color, otherwise interpolation
% can violate assumptions of prescaled color (e.g. integer vals).
%
% 8/9/04	dhb		Wrote it.
% 18/4/05   ejw     Converted it to run with OSX version of Psychtoolbox.
% 26.2.07   mk      Bugfix for LSB conversion: Added modulo operation.
% 3/01/07   mk      Bugfix for MSB conversion: Added floor operation.

if (nargin < 2 || isempty(SPACESCALE))
	SPACESCALE = 1;
end
if (nargin < 3 || isempty(COLORSCALE))
	COLORSCALE = 1;
end

if (SPACESCALE && ~COLORSCALE)
	error('COLORSCALE must be set if SPACESCALE is.');
end

if (SPACESCALE)
	[m,n,p] = size(inImage);
	if (rem(n,2) ~= 0)
		error('Image must have even number of columns');
    end
    % imresize is part of the image processing toolbox
    % I have recoded to avoid the need for this
	%inImage = imresize(inImage,[m n/2],'bilinear');
   
    % halve the horizontal width of the image
    % this compensates for the 2:1 aspect ratio for colour++ pixels
    inImage = inImage(:,[1:2:n],:);
   
	index = find(inImage < 0);
	if (~isempty(index))
		inImage(index) = 0; 
	end
	index = find(inImage > 1);
	if (~isempty(index))
		inImage(index) = 1; 
	end
else
	[m,n,p] = size(inImage);
	n = n*2;
end

if (COLORSCALE)
	inImage = round(65535*inImage);
end

% Make output image and stuff with high and low bytes
outImage = uint8(zeros(m,n,p));

% First byte is high 8 bits
outImage(:,1:2:end-1,:) = uint8(floor(inImage/256));

% Second byte is low 8 bits
outImage(:,2:2:end,:) = uint8(mod(inImage, 256));