Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / Onyx2D / O2PDFFunction_Type0.m
blob8e15c22df5f2d79e77957a17470fd90a8da5f5d8
1 /* Copyright (c) 2010 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import "O2PDFFunction_Type0.h"
10 #import <Onyx2D/O2PDFArray.h>
11 #import <Onyx2D/O2PDFDictionary.h>
12 #import <Onyx2D/O2PDFContext.h>
13 #import <Foundation/NSArray.h>
14 #import <stddef.h>
16 @implementation O2PDFFunction_Type0
18 static void evaluate(void *info,const float *input,float *output) {
19    O2PDFFunction_Type0 *self=info;
21    if(self->_sizeCount!=1){
22     O2PDFError(__FILE__,__LINE__,@"Type0 can not handle size count!=1");
23     return;
24    }
25    
26    if(self->_domainCount/2!=1){
27     O2PDFError(__FILE__,__LINE__,@"Type0 can not handle size more than one input");
28     return;
29    }
30    
31    if(self->_bitsPerSample!=8){
32     O2PDFError(__FILE__,__LINE__,@"Type0 can not handle bps!=8");
33     return;
34    }
35    if(self->_order!=1){
36     O2PDFError(__FILE__,__LINE__,@"Type0 can not handle order!=1");
37     return;
38    }
39 // FIXME, scale input to domain, more bounds checking
40    int      i,numberOfOutputs=self->_rangeCount/2;
42    int      inputIndex=MIN(MAX(0,input[0]*(self->_size[0]-1)),self->_size[self->_sizeCount-1]);
43    int      bytesPerSample=self->_bitsPerSample/8;
44    int      sampleOffset=inputIndex*(bytesPerSample*numberOfOutputs);
45       
46    const uint8_t *samples=self->_bytes+sampleOffset;
47    
48     for(i=0;i<numberOfOutputs;i++){
49      output[i]=samples[i];
50      output[i]=output[i]/255.0;
51     }
54 -initWithDomain:(O2PDFArray *)domain range:(O2PDFArray *)range size:(O2PDFArray *)size bitsPerSample:(O2PDFInteger)bps order:(O2PDFInteger)order encode:(O2PDFArray *)encode decode:(O2PDFArray *)decode data:(NSData *)data {
55    if([super initWithDomain:domain range:range]==nil)
56     return nil;
58    _info=self;
59    _callbacks.evaluate=evaluate;
61    _sizeCount=0;
62    [size getIntegers:&_size count:&_sizeCount];
63    _bitsPerSample=bps;
64    _order=order;
65    [encode getNumbers:&_encode count:&_encodeCount];
66    [decode getNumbers:&_decode count:&_decodeCount];
67    _data=[data retain];
68    _dataLength=[data length];
69    _bytes=[data bytes];
70      
71    return self;
74 -(void)dealloc {
75    if(_size!=NULL)
76     NSZoneFree(NULL,_size);
77    if(_encode!=NULL)
78     NSZoneFree(NULL,_encode);
79    if(_decode!=NULL)
80     NSZoneFree(NULL,_decode);
81    [_data release];
82    [super dealloc];
85 -(BOOL)isLinear {
86    return NO;
89 -(O2PDFObject *)encodeReferenceWithContext:(O2PDFContext *)context {
90    O2PDFDictionary *result=[O2PDFDictionary pdfDictionary];
91    int              i;
92    
93    [result setIntegerForKey:"FunctionType" value:4];
94    [result setObjectForKey:"Domain" value:[O2PDFArray pdfArrayWithNumbers:_domain count:_domainCount]];
95    [result setObjectForKey:"Range" value:[O2PDFArray pdfArrayWithNumbers:_range count:_rangeCount]];
96    // FIXME  stream
97    
98    return [context encodeIndirectPDFObject:result];
101 -(NSString *)description {
102    NSMutableString *result=[NSMutableString string];
103    [result appendFormat:@"<%@ %p:",isa,self];
104    [result appendFormat:@">"];
105    
106    return result;
109 @end