Пример #1
0
    ByteBufferPtr JpegCompressor::compress(const RawImagePtr& image, size_t x, size_t y, size_t width, size_t height)
    {
        RasterFormat::Format_ format = image->format()->format_;
        if ( RasterFormat::GRAYSCALE == format )
        {
            m_cinfo.input_components = 1;
            m_cinfo.in_color_space = JCS_GRAYSCALE;
        }
        else if ( RasterFormat::RGB == format )
        {
            m_cinfo.input_components = 3;
            m_cinfo.in_color_space = JCS_RGB;
        }
		else if ( RasterFormat::BGR == format )
		{
			m_cinfo.input_components = 3;
			m_cinfo.in_color_space = JCS_EXT_BGR;
		}
        else
            return ByteBufferPtr();
        jpeg_set_defaults(&m_cinfo);

        m_cinfo.image_width = width;
        m_cinfo.image_height = height;

		jpeg_set_quality(&m_cinfo, image->format()->quality_, true);

        jpeg_start_compress(&m_cinfo, TRUE);

        const char *src;
        size_t stride;
        if ( RasterFormat::NORMAL == image->format()->orientation_ )
        {
            src = (const char *)image->data()
                + ( y * image->format()->rowTotalBytes_ )
                + ( x * image->format()->bytesPerPixel_ );
            stride = image->format()->rowTotalBytes_;
        }
        else
        {
            src = (const char *)image->data()
                + ( image->format()->rowTotalBytes_ * ( image->format()->rasterHeight_ - y - 1 ) )
                + ( x * image->format()->bytesPerPixel_ );
            stride = -image->format()->rowTotalBytes_;
        }

        JSAMPLE *rgb = new JSAMPLE[width * m_cinfo.input_components * 8];
        JSAMPROW rowPointer[8];
        for (int i = 0; i < 8; i++)
            rowPointer[i] = &rgb[width * m_cinfo.input_components * i];

        while ( m_cinfo.next_scanline < m_cinfo.image_height )
        {
            int maxRows = m_cinfo.image_height - m_cinfo.next_scanline;
            if (maxRows > 8)
            {
                maxRows = 8;
            }
            for ( int dy = 0; dy < maxRows; dy++ )
            {
                memcpy(rowPointer[dy], src, width * m_cinfo.input_components);
                src += stride;
            }
            jpeg_write_scanlines(&m_cinfo, rowPointer, maxRows);
        }

        delete [] rgb;

        jpeg_finish_compress(&m_cinfo);

        ByteBufferPtr buffer(new ByteBuffer(m_outputBuffer, m_numBytesReady, true));
        m_outputBuffer = NULL;
        return buffer;
    }
Пример #2
0
bool EventConnection::postNetEvent(NetEvent *theEvent)
{
   // Check if the direction this event moves is a valid direction.
   TNLAssertV(   (theEvent->getEventDirection() != NetEvent::DirUnset)
      && (theEvent->getEventDirection() != NetEvent::DirServerToClient || !isConnectionToServer())
      && (theEvent->getEventDirection() != NetEvent::DirClientToServer || !isConnectionToClient()),
      ("Trying to send wrong event direction in %s", theEvent->getClassName()));

   S32 classId = theEvent->getClassId(getNetClassGroup());
   if(U32(classId) >= mEventClassCount && getConnectionState() == Connected)
   {
      theEvent->incRef();
      theEvent->decRef(); // Avoids some type of memory leak by deleting here if nothing reference it.
      return false;
   }

   theEvent->notifyPosted(this);

   EventNote *event = mEventNoteChunker.alloc();
   event->mEvent = theEvent;
   event->mNextEvent = NULL;

   if(event->mEvent->mGuaranteeType == NetEvent::GuaranteedOrdered)
   {
      event->mSeqCount = mNextSendEventSeq++;
      if(!mSendEventQueueHead)
         mSendEventQueueHead = event;
      else
         mSendEventQueueTail->mNextEvent = event;
      mSendEventQueueTail = event;
   }
   else if(event->mEvent->mGuaranteeType == NetEvent::GuaranteedOrderedBigData)
   {
      BitStream bstream;
      const U32 start = 0;
      const U32 partsSize = 512;

      if(mConnectionParameters.mDebugObjectSizes)
         bstream.advanceBitPosition(BitStreamPosBitSize);
      
      S32 classId = event->mEvent->getClassId(getNetClassGroup());
      bstream.writeInt(classId, mEventClassBitSize);

      event->mEvent->pack(this, &bstream);
      logprintf(LogConsumer::LogEventConnection, "EventConnection %s: WroteEvent %s - %d bits", getNetAddressString(), event->mEvent->getDebugName(), bstream.getBitPosition() - start);

      if(mConnectionParameters.mDebugObjectSizes)
         bstream.writeIntAt(bstream.getBitPosition(), BitStreamPosBitSize, start);

      U32 size = bstream.getBytePosition();

      for(U32 i=0; i<size; i+=partsSize)
      {
         if(i+partsSize < size)
         {
            ByteBuffer *bytebuffer = new ByteBuffer(&bstream.getBuffer()[i], partsSize);
            bytebuffer->takeOwnership();  // may have to use this to prevent errors.
            s2rTNLSendDataParts(0, ByteBufferPtr(bytebuffer));
         }
         else
         {
            ByteBuffer *bytebuffer = new ByteBuffer(&bstream.getBuffer()[i], size-i);
            bytebuffer->takeOwnership();
            s2rTNLSendDataParts(1, ByteBufferPtr(bytebuffer));
         }
      }
      mEventNoteChunker.free(event);
   }
   else
   {
      event->mSeqCount = InvalidSendEventSeq;
      if(!mUnorderedSendEventQueueHead)
         mUnorderedSendEventQueueHead = event;
      else
         mUnorderedSendEventQueueTail->mNextEvent = event;
      mUnorderedSendEventQueueTail = event;
   }
   return true;
}