swift - Cannot invoke '_' with an argument list of type '_' - Which of the two choices should I use? -
i have error. think have 2 choices. 1 best code? differences mean?
cannot invoke 'rgbtohsv' argument list of type '(float,float,float)'
rgbtohsv(cgfloat(r), cgfloat(g), cgfloat(b))
rgbtohsv(cgfloat(), cgfloat(), cgfloat())
also if take @ screen shot if give me pointers regarding other couple of errors great too. know have match types don't know syntax order. http://i.imgur.com/sackg6h.png
thanks
func rgbtohsv(r : cgfloat, g : cgfloat, b : cgfloat) -> (h : cgfloat, s : cgfloat, v : cgfloat) { var h : cgfloat = 0.0 var s : cgfloat = 0.0 var v : cgfloat = 0.0 let col = uicolor(red: r, green: g, blue: b, alpha: 1.0) col.gethue(&h, saturation: &s, brightness: &v, alpha: nil) return (h, s, v) } // process frame of video func captureoutput(captureoutput:avcaptureoutput, didoutputsamplebuffer samplebuffer:cmsamplebuffer, fromconnection connection:avcaptureconnection) { // if we're paused don't if currentstate == currentstate.statepaused { // reset our frame counter self.validframecounter = 0 return } // image buffer var cvimgref:cvimagebufferref = cmsamplebuffergetimagebuffer(samplebuffer) // lock image buffer cvpixelbufferlockbaseaddress(cvimgref, 0) // access data var width: size_t = cvpixelbuffergetwidth(cvimgref) var height:size_t = cvpixelbuffergetheight(cvimgref) // raw image bytes let buf = unsafemutablepointer<uint8>(cvpixelbuffergetbaseaddress(cvimgref)) var bprow: size_t = cvpixelbuffergetbytesperrow(cvimgref) var r:float = 0.0 var g:float = 0.0 var b:float = 0.0 var y = 0; y < height; y++ { var x:uint8 = 0; x < width * 4; x += 4 { // error: '<' cannot applied operands of type 'uint8' , 'int' b += buf[x] g += buf[x + 1] r += buf[x + 2] } buf += bprow(unsafemutablepointer(uint8)) // error: '+=' cannot applied operands of type 'unsafemutablepointer<uint8>' , 'size_t' } r /= 255 * (width*height) g /= 255 * (width*height) b /= 255 * (width*height) //} // convert rgb hsv colourspace var h:float = 0.0 var s:float = 0.0 var v:float = 0.0 rgbtohsv(r, g, b) // error
you have lot of type mismatch error.
the type of x
should not uint8
because x
increase until value of width.
for var x:uint8 = 0; x < width * 4; x += 4 { // error: '<' cannot applied operands of type 'uint8' , 'int'
so fix below:
for var x = 0; x < width * 4; x += 4 {
to increment pointer address, can use advancedby()
function.
buf += bprow(unsafemutablepointer(uint8)) // error: '+=' cannot applied operands of type 'unsafemutablepointer<uint8>' , 'size_t'
like below:
var pixel = buf.advancedby(y * bprow)
and line,
rgbtohsv(r, g, b) // error
there no implicit casts in swift between cgfloat
, float
unfortunately. should cast explicitly cgfloat
.
rgbtohsv(cgfloat(r), g: cgfloat(g), b: cgfloat(b))
the whole edited code here:
func rgbtohsv(r: cgfloat, g: cgfloat, b: cgfloat) -> (h: cgfloat, s: cgfloat, v: cgfloat) { var h: cgfloat = 0.0 var s: cgfloat = 0.0 var v: cgfloat = 0.0 let col = uicolor(red: r, green: g, blue: b, alpha: 1.0) col.gethue(&h, saturation: &s, brightness: &v, alpha: nil) return (h, s, v) } // process frame of video func captureoutput(captureoutput:avcaptureoutput, didoutputsamplebuffer samplebuffer:cmsamplebuffer, fromconnection connection:avcaptureconnection) { // if we're paused don't if currentstate == currentstate.statepaused { // reset our frame counter self.validframecounter = 0 return } // image buffer var cvimgref = cmsamplebuffergetimagebuffer(samplebuffer) // lock image buffer cvpixelbufferlockbaseaddress(cvimgref, 0) // access data var width = cvpixelbuffergetwidth(cvimgref) var height = cvpixelbuffergetheight(cvimgref) // raw image bytes let buf = unsafemutablepointer<uint8>(cvpixelbuffergetbaseaddress(cvimgref)) var bprow = cvpixelbuffergetbytesperrow(cvimgref) var r: float = 0.0 var g: float = 0.0 var b: float = 0.0 var y = 0; y < height; y++ { var pixel = buf.advancedby(y * bprow) var x = 0; x < width * 4; x += 4 { // error: '<' cannot applied operands of type 'uint8' , 'int' b += float(pixel[x]) g += float(pixel[x + 1]) r += float(pixel[x + 2]) } } r /= 255 * float(width * height) g /= 255 * float(width * height) b /= 255 * float(width * height) //} // convert rgb hsv colourspace var h: float = 0.0 var s: float = 0.0 var v: float = 0.0 rgbtohsv(cgfloat(r), g: cgfloat(g), b: cgfloat(b)) // error }
Comments
Post a Comment