Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

OSXPixelBuffer.cxx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright 2011-2014 Pierre Ossman 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 <ApplicationServices/ApplicationServices.h>
  22. #include <FL/Fl_Window.H>
  23. #include <FL/x.H>
  24. #include <rfb/LogWriter.h>
  25. #include <rfb/Exception.h>
  26. #include "i18n.h"
  27. #include "OSXPixelBuffer.h"
  28. using namespace rfb;
  29. static rfb::LogWriter vlog("OSXPixelBuffer");
  30. OSXPixelBuffer::OSXPixelBuffer(int width, int height) :
  31. PlatformPixelBuffer(rfb::PixelFormat(32, 24, false, true,
  32. 255, 255, 255, 16, 8, 0),
  33. width, height, NULL, width),
  34. bitmap(NULL)
  35. {
  36. CGColorSpaceRef lut;
  37. data = new rdr::U8[width * height * format.bpp/8];
  38. if (data == NULL)
  39. throw rfb::Exception(_("Not enough memory for framebuffer"));
  40. lut = CGColorSpaceCreateDeviceRGB();
  41. if (!lut)
  42. throw rfb::Exception(_("Could not create framebuffer device"));
  43. bitmap = CGBitmapContextCreate(data, width, height, 8, width*4, lut,
  44. kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little);
  45. CGColorSpaceRelease(lut);
  46. if (!bitmap)
  47. throw rfb::Exception(_("Could not create framebuffer bitmap"));
  48. }
  49. OSXPixelBuffer::~OSXPixelBuffer()
  50. {
  51. CFRelease((CGContextRef)bitmap);
  52. delete [] data;
  53. }
  54. void OSXPixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
  55. {
  56. CGRect rect;
  57. CGContextRef gc;
  58. CGAffineTransform at;
  59. CGImageRef image;
  60. gc = (CGContextRef)fl_gc;
  61. CGContextSaveGState(gc);
  62. // We have to use clipping to partially display an image
  63. rect.origin.x = x - 0.5;
  64. rect.origin.y = y - 0.5;
  65. rect.size.width = w;
  66. rect.size.height = h;
  67. CGContextClipToRect(gc, rect);
  68. // Oh the hackiness that is OS X image handling...
  69. // The CGContextDrawImage() routine magically flips the images and offsets
  70. // them by 0.5,0.5 in order to compensate for OS X unfamiliar coordinate
  71. // system. But this breaks horribly when you set up the CTM to get the
  72. // more familiar top-down system (which FLTK does), meaning we have to
  73. // reset the CTM back to the identity matrix and calculate new origin
  74. // coordinates.
  75. at = CGContextGetCTM(gc);
  76. CGContextScaleCTM(gc, 1, -1);
  77. CGContextTranslateCTM(gc, -at.tx, -at.ty);
  78. rect.origin.x = x - src_x;
  79. rect.origin.y = Fl_Window::current()->h() - (y - src_y);
  80. rect.size.width = width();
  81. rect.size.height = -height(); // Negative height does _not_ flip the image
  82. image = CGBitmapContextCreateImage((CGContextRef)bitmap);
  83. CGContextDrawImage(gc, rect, image);
  84. CGImageRelease(image);
  85. CGContextRestoreGState(gc);
  86. }