gerador de BMPs

Publicado por Rafael 17/10/2008

[ Hits: 6.645 ]

Homepage: nenhum

Download bitmap.hpp




Espero que o seu uso seja simples: forneça

   -->   um nome para o arquivo BMP,
   -->   largura e altura da imagem
   -->   um vetor [][][3] de float com as cores para o construtor

  



Esconder código-fonte

/*
 *   Copyright (C) 2008, 2008 Rafael Siqueira Telles Vieira
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful, 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *   See the GNU General Public License for more details.
 *
 *   License: http://www.lia.ufc.br/~rafaelstv/licenca_GPL_pt.txt
 *
 *   You should have received a copy of the GNU General Public License 
 *   along with this program; if not, write to the Free Software Foundation, 
 *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 
 *
 */
/*

Exemplo de uso: Gera um quadrado verde

#include <stdio.h>
#include "bitmap.hpp"

int main(int argc, char**argv)
{
   float ***imagem;

   int i,j;

   imagem = new float**[100];
   for (i=0;i<100;i++)
   {
      imagem[i] = new float*[100];
      for (j=0;j<100;j++)
      {
         imagem[i][j] = new float[3];
         imagem[i][j][0]=0.0f;
         imagem[i][j][1]=1.0f;
         imagem[i][j][2]=0.0f;
      }
   }
   Bitmap *bmp;
   bmp = new Bitmap(argv[1], 100, 100, imagem);
}

*/
#include <iostream>
#include <fstream>

using namespace std;

class Bitmap
{
   private:
      char* nome;
      int largura;
      int altura;
      float ***imagem;
      ofstream *arquivo;
   public:
   Bitmap(char* nome, int largura, int altura, float  ***imagem)
   {
      this->nome = nome;
      this->largura = largura;
      this->altura = altura;
      this->imagem = imagem;
      arquivo = new ofstream(nome, ios::app);
      criaBMP();
   }
   void escreveByte(char c)
   {
      (*arquivo) << c;
   }

   char converteCor(float espectro)
   {   
      return ((char)((int) (255.0*espectro)));
   }
   void escreveCor(float *cor)
   {
      escreveByte(converteCor(cor[2]));
      escreveByte(converteCor(cor[1]));
      escreveByte(converteCor(cor[0]));
   }
   void escreveCabecalho()
   {
      (*arquivo) << 'B';
      (*arquivo) << 'M';
      int tamanho = (largura * altura * 3) + 54; // 54 bytes para o cabecalho do BMP
      escreveInt(tamanho);
      escreveInt(0);   
      escreveInt(54);   
      escreveInt(40);
      escreveInt(largura);   
      escreveInt(altura);
      escreveShort(1);
      escreveShort(24);   
      escreveInt(0);   
      escreveInt(0);   
      escreveInt(0);   
      escreveInt(0);   
      escreveInt(0);   
      escreveInt(0);   
   }
   void escreveShort(short numero)
   {
      char* t = (char*) №
      (*arquivo) << t[0];
      (*arquivo) << t[1];
   }
   void escreveInt(int numero)
   {
      char* t = (char*) №
      (*arquivo) << t[0];
      (*arquivo) << t[1];
      (*arquivo) << t[2];
      (*arquivo) << t[3];
   }
   void criaBMP()
   {
      int i,j;
      escreveCabecalho();
      for (i=altura-1;i>=0;i--)
         for(j=0;j<largura;j++)
            escreveCor(imagem[j][i]);
      (*arquivo).close();
   }
};

Scripts recomendados

Função readConf. Ler arquivos de configuração.

Gerenciamento de alunos com dados armazenados em arquivo

Números de caracteres em arquivo

Cadastro de arquivo usando ncurses como menu

Ler .conf


  

Comentários
[1] Comentário enviado por rafastv em 18/10/2008 - 07:41h

Apenas complementando: o formato de cores usado pelo arquivo são Bitmaps de 24 bits, as intensidades das cores devem variar de 0 à 1 para cada um dos espectros de cor.

[2] Comentário enviado por rafastv em 18/10/2008 - 08:50h

Existe uma correção a ser feita: A altura e a largura do BMP gerado deve ser multiplo de 4. (sugestão: preencha com zeros as cores finais)


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts