You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OSXPixelBuffer.cxx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <assert.h>
  22. #include <ApplicationServices/ApplicationServices.h>
  23. #include <FL/Fl_Window.H>
  24. #include <FL/x.H>
  25. #include <rfb/LogWriter.h>
  26. #include <rfb/Exception.h>
  27. #include "OSXPixelBuffer.h"
  28. using namespace rfb;
  29. static rfb::LogWriter vlog("PlatformPixelBuffer");
  30. PlatformPixelBuffer::PlatformPixelBuffer(int width, int height) :
  31. ManagedPixelBuffer(rfb::PixelFormat(32, 24, false, true,
  32. 255, 255, 255, 16, 8, 0),
  33. width, height),
  34. bitmap(NULL)
  35. {
  36. CGColorSpaceRef lut;
  37. lut = CGColorSpaceCreateDeviceRGB();
  38. assert(lut);
  39. bitmap = CGBitmapContextCreate(data, width, height, 8, width*4, lut,
  40. kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little);
  41. assert(bitmap);
  42. CGColorSpaceRelease(lut);
  43. }
  44. PlatformPixelBuffer::~PlatformPixelBuffer()
  45. {
  46. CFRelease((CGContextRef)bitmap);
  47. }
  48. void PlatformPixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
  49. {
  50. CGRect rect;
  51. CGContextRef gc;
  52. CGAffineTransform at;
  53. CGImageRef image;
  54. gc = (CGContextRef)fl_gc;
  55. CGContextSaveGState(gc);
  56. // We have to use clipping to partially display an image
  57. rect.origin.x = x - 0.5;
  58. rect.origin.y = y - 0.5;
  59. rect.size.width = w;
  60. rect.size.height = h;
  61. CGContextClipToRect(gc, rect);
  62. // Oh the hackiness that is OS X image handling...
  63. // The CGContextDrawImage() routine magically flips the images and offsets
  64. // them by 0.5,0.5 in order to compensate for OS X unfamiliar coordinate
  65. // system. But this breaks horribly when you set up the CTM to get the
  66. // more familiar top-down system (which FLTK does), meaning we have to
  67. // reset the CTM back to the identity matrix and calculate new origin
  68. // coordinates.
  69. at = CGContextGetCTM(gc);
  70. CGContextScaleCTM(gc, 1, -1);
  71. CGContextTranslateCTM(gc, -at.tx, -at.ty);
  72. rect.origin.x = x - src_x;
  73. rect.origin.y = Fl_Window::current()->h() - (y - src_y);
  74. rect.size.width = width();
  75. rect.size.height = -height(); // Negative height does _not_ flip the image
  76. image = CGBitmapContextCreateImage((CGContextRef)bitmap);
  77. CGContextDrawImage(gc, rect, image);
  78. CGImageRelease(image);
  79. CGContextRestoreGState(gc);
  80. }