Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Pablo Moreno Olalla
- Email address: darthbrevu@yahoo.es
- */
- #include <cstdio>
- #include <string>
- #include <sstream>
- #include <iostream>
- #include <stdint.h>
- #include <stdexcept>
- using namespace std;
- //This code is a little too ad hoc, which means that you shouldn't use it for general BMP files!
- class Bmp24bits {
- private:
- uint32_t width;
- uint32_t height;
- unsigned char **r;
- unsigned char **g;
- unsigned char **b;
- public:
- Bmp24bits(const char *fn) {
- uint32_t ptr;
- FILE *fd=fopen(fn,"rb");
- if (!fd) throw runtime_error("Couldn't open file.");
- fseek(fd,10,SEEK_SET);
- fread(static_cast<void *>(&ptr),4,1,fd);
- fseek(fd,4,SEEK_CUR);
- // Reading both width and height seems like a good trick, but may be unsafe due to alignment issues.
- // It would work on most computers, though.
- fread(static_cast<void *>(&width),4,1,fd);
- fread(static_cast<void *>(&height),4,1,fd);
- r=new unsigned char *[height];
- g=new unsigned char *[height];
- b=new unsigned char *[height];
- fseek(fd,ptr,SEEK_SET);
- unsigned char *tmpbuf=new unsigned char[3*width];
- for (int i=height-1;i>=0;--i) {
- r[i]=new unsigned char[width];
- g[i]=new unsigned char[width];
- b[i]=new unsigned char[width];
- unsigned char *rt=r[i];
- unsigned char *gt=g[i];
- unsigned char *bt=b[i];
- fread(static_cast<void *>(tmpbuf),1,3*width,fd);
- unsigned char *p=tmpbuf;
- for (size_t j=0;j<width;++j) {
- bt[j]=p[0];
- gt[j]=p[1];
- rt[j]=p[2];
- p+=3;
- }
- }
- delete[] tmpbuf;
- fclose(fd);
- }
- ~Bmp24bits() {
- for (size_t i=0;i<height;++i) {
- delete[] r[i];
- delete[] g[i];
- delete[] b[i];
- }
- delete[] r;
- delete[] g;
- delete[] b;
- }
- inline unsigned int computeSum(char component,size_t row) const {
- if (row>height) return 0;
- const unsigned char *p;
- if (component=='r'||component=='R') p=r[row];
- else if (component=='g'||component=='G') p=g[row];
- else if (component=='b'||component=='B') p=b[row];
- else return 0;
- unsigned int res=1; //It seems that I must do this (instead of res=0), but I don't understand why :(.
- for (size_t i=0;i<width;++i) res+=static_cast<unsigned int>(p[i]);
- return res;
- }
- inline uint32_t getWidth() const {
- return width;
- }
- inline uint32_t getHeight() const {
- return height;
- }
- };
- int main(int argc,char **argv) {
- const char *fn;
- fn=((argc<=1)?"trabaja.bmp":argv[1]);
- //The following two lines are not strictly necessary, but make the code a little more handy.
- if (argc>=3) freopen(argv[2],"r",stdin);
- if (argc>=4) freopen(argv[3],"w",stdout);
- Bmp24bits bm(fn);
- string tmp;
- char c;
- unsigned int line;
- do {
- getline(cin,tmp);
- if (sscanf(tmp.c_str(),"%c%u",&c,&line)!=2) break;
- else cout<<bm.computeSum(c,line)<<endl;
- } while (!cin.eof());
- return 0; //bm will be deleted and freed automatically, so no need to do it explicitly :).
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement