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.
388 lines
8.0 KiB
388 lines
8.0 KiB
15 years ago
|
/**
|
||
|
A small utility to generate embedded images for Keramik, especially structured for easy recoloring...
|
||
|
|
||
|
Copyright (c) 2002 Maksim Orlovich <mo002j@mail.rochester.edu>
|
||
|
|
||
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||
|
copy of this software and associated documentation files (the "Software"),
|
||
|
to deal in the Software without restriction, including without limitation
|
||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||
|
and/or sell copies of the Software, and to permit persons to whom the
|
||
|
Software is furnished to do so, subject to the following conditions:
|
||
|
|
||
|
The above copyright notice and this permission notice shall be included in
|
||
|
all copies or substantial portions of the Software.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||
|
DEALINGS IN THE SOFTWARE.
|
||
|
|
||
|
*/
|
||
|
|
||
15 years ago
|
#include <tqfileinfo.h>
|
||
|
#include <tqimage.h>
|
||
|
#include <tqmap.h>
|
||
|
#include <tqregexp.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
15 years ago
|
#include <tqvaluevector.h>
|
||
15 years ago
|
|
||
|
#include <kimageeffect.h>
|
||
|
|
||
|
#include <iostream>
|
||
|
using namespace std;
|
||
|
|
||
|
#include <string.h>
|
||
|
#include <math.h>
|
||
|
|
||
|
//NOTE: Use of old-style header is intentional for portability. See revisions 1.6 and 1.7
|
||
|
|
||
|
//Force-touch-embedded-revision: 2
|
||
|
|
||
|
#include "keramikimage.h"
|
||
|
|
||
|
/**
|
||
|
Need to generate something like this:
|
||
|
TargetColorAlpha, GreyAdd, SrcAlpha;
|
||
|
|
||
|
so that one can do (R*T+GreyAdd, G*T+GreyAdd, B*T+GreyAdd, SrcAlpha) as pixel values
|
||
|
*/
|
||
|
|
||
|
|
||
15 years ago
|
int evalSuffix(TQString suffix)
|
||
15 years ago
|
{
|
||
|
if (suffix == "-tl")
|
||
|
return 0;
|
||
|
|
||
|
if (suffix == "-tc")
|
||
|
return 1;
|
||
|
|
||
|
if (suffix == "-tr")
|
||
|
return 2;
|
||
|
|
||
|
if (suffix == "-cl")
|
||
|
return 3;
|
||
|
|
||
|
if (suffix == "-cc")
|
||
|
return 4;
|
||
|
|
||
|
if (suffix == "-cr")
|
||
|
return 5;
|
||
|
|
||
|
if (suffix == "-bl")
|
||
|
return 6;
|
||
|
|
||
|
if (suffix == "-bc")
|
||
|
return 7;
|
||
|
|
||
|
if (suffix == "-br")
|
||
|
return 8;
|
||
|
|
||
|
if (suffix == "-separator")
|
||
|
return KeramikTileSeparator;
|
||
|
|
||
|
if (suffix == "-slider1")
|
||
|
return KeramikSlider1;
|
||
|
|
||
|
if (suffix == "-slider2")
|
||
|
return KeramikSlider2;
|
||
|
|
||
|
if (suffix == "-slider3")
|
||
|
return KeramikSlider3;
|
||
|
|
||
|
if (suffix == "-slider4")
|
||
|
return KeramikSlider4;
|
||
|
|
||
|
if (suffix == "-groove1")
|
||
|
return KeramikGroove1;
|
||
|
|
||
|
if (suffix == "-groove2")
|
||
|
return KeramikGroove2;
|
||
|
|
||
|
if (suffix == "-1")
|
||
|
return 1;
|
||
|
|
||
|
if (suffix == "-2")
|
||
|
return 2;
|
||
|
|
||
|
if (suffix == "-3")
|
||
|
return 3;
|
||
|
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(int argc, char** argv)
|
||
|
{
|
||
|
if (argc < 2)
|
||
|
return 0;
|
||
|
|
||
15 years ago
|
TQValueVector<KeramikEmbedImage> images;
|
||
15 years ago
|
|
||
15 years ago
|
cout<<"#include <tqintdict.h>\n\n";
|
||
15 years ago
|
cout<<"#include \"keramikimage.h\"\n\n";
|
||
|
|
||
15 years ago
|
TQMap<TQString, int> assignID;
|
||
15 years ago
|
int nextID = 0;
|
||
|
|
||
|
for (int c = 1; c<argc; c++)
|
||
|
{
|
||
|
|
||
15 years ago
|
TQImage input(argv[c]);
|
||
15 years ago
|
|
||
|
|
||
15 years ago
|
TQFileInfo fi(argv[c]);
|
||
|
TQString s = fi.baseName();
|
||
15 years ago
|
|
||
|
KeramikEmbedImage image;
|
||
|
|
||
|
int pos;
|
||
|
|
||
15 years ago
|
TQString id = s;
|
||
15 years ago
|
|
||
|
int readJustID = 0;
|
||
|
|
||
|
|
||
13 years ago
|
if ((pos = s.findRev("-")) != -1)
|
||
15 years ago
|
{
|
||
|
int suffix = evalSuffix(s.mid(pos));
|
||
|
if (suffix !=-1 )
|
||
|
{
|
||
|
id = s.mid(0,pos);
|
||
|
readJustID = suffix;
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
if (!assignID.contains(id))
|
||
15 years ago
|
{
|
||
|
assignID[id] = nextID;
|
||
|
nextID += 256;
|
||
|
}
|
||
|
|
||
14 years ago
|
s.replace("-","_");
|
||
15 years ago
|
|
||
|
|
||
14 years ago
|
if (s.contains("button"))
|
||
15 years ago
|
KImageEffect::contrastHSV(input);
|
||
|
|
||
|
int fullID = assignID[id] + readJustID;//Subwidget..
|
||
|
|
||
|
bool highlights = true;
|
||
|
bool shadows = true;
|
||
|
|
||
|
float gamma = 1.0;
|
||
|
int brightAdj = 0;
|
||
|
|
||
|
|
||
|
|
||
14 years ago
|
if (s.contains("toolbar") || s.contains("tab-top-active") || s.contains("menubar") )
|
||
15 years ago
|
{
|
||
|
// highlights = false;
|
||
|
gamma = 1/1.25f;
|
||
|
//brightAdj = 10;
|
||
|
shadows = false;
|
||
|
}
|
||
|
|
||
14 years ago
|
if (s.contains("scrollbar") && s.contains("groove"))
|
||
15 years ago
|
{
|
||
|
//highlights = false;
|
||
|
//gamma = 1.5;
|
||
|
shadows = false;
|
||
|
}
|
||
|
//brightAdj = -10;
|
||
|
|
||
14 years ago
|
if (s.contains("scrollbar") && s.contains("slider"))
|
||
15 years ago
|
{
|
||
|
//highlights = false;
|
||
|
gamma =1/0.7f;
|
||
|
//shadows = false;
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
if (s.contains("menuitem"))
|
||
15 years ago
|
{
|
||
|
//highlights = false;
|
||
|
gamma =1/0.6f;
|
||
|
//shadows = false;
|
||
|
}
|
||
|
|
||
|
image.width = input.width();
|
||
|
image.height = input.height();
|
||
|
image.id = fullID;
|
||
|
image.data = reinterpret_cast<unsigned char*>(strdup(s.latin1()));
|
||
|
|
||
|
|
||
|
bool reallySolid = true;
|
||
|
|
||
|
int pixCount = 0;
|
||
|
int pixSolid = 0;
|
||
|
|
||
|
cout<<"static const unsigned char "<<s.latin1()<<"[]={\n";
|
||
|
|
||
14 years ago
|
TQ_UINT32* read = reinterpret_cast< TQ_UINT32* >(input.bits() );
|
||
15 years ago
|
int size = input.width()*input.height();
|
||
|
|
||
|
for (int pos=0; pos<size; pos++)
|
||
|
{
|
||
|
QRgb basePix = (QRgb)*read;
|
||
|
|
||
14 years ago
|
if (tqAlpha(basePix) != 255)
|
||
15 years ago
|
reallySolid = false;
|
||
|
else
|
||
|
pixSolid++;
|
||
|
|
||
|
pixCount++;
|
||
|
read++;
|
||
|
}
|
||
|
|
||
|
image.haveAlpha = !reallySolid;
|
||
|
|
||
|
images.push_back(image);
|
||
|
|
||
14 years ago
|
read = reinterpret_cast< TQ_UINT32* >(input.bits() );
|
||
15 years ago
|
for (int pos=0; pos<size; pos++)
|
||
|
{
|
||
|
QRgb basePix = (QRgb)*read;
|
||
|
//cout<<(r*destAlpha.alphas[pos])<<"\n";
|
||
|
//cout<<(int)destAlpha.alphas[pos]<<"\n";
|
||
15 years ago
|
TQColor clr(basePix);
|
||
15 years ago
|
int h,s,v;
|
||
|
clr.hsv(&h,&s,&v);
|
||
|
|
||
14 years ago
|
v=tqGray(basePix);
|
||
15 years ago
|
|
||
|
int targetColorAlpha = 0 , greyAdd = 0;
|
||
14 years ago
|
//int srcAlpha = tqAlpha(basePix);
|
||
15 years ago
|
|
||
|
if (s>0 || v > 128)
|
||
|
{ //Non-shadow
|
||
|
float fv = v/255.0;
|
||
|
fv = pow(fv, gamma);
|
||
|
v = int(255.5*fv);
|
||
|
|
||
|
|
||
|
if (s<17 && highlights) //A bit of a highligt..
|
||
|
{
|
||
|
float effectPortion = (16 - s)/15.0;
|
||
|
|
||
|
greyAdd = (int)(v/4.0 * effectPortion*1.2);
|
||
|
targetColorAlpha = v - greyAdd;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
targetColorAlpha = v;//(int)(fv*255);
|
||
|
greyAdd = 0;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (shadows)
|
||
|
{
|
||
|
targetColorAlpha = 0;
|
||
|
greyAdd = v;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
targetColorAlpha = v;//(int)(fv*255);
|
||
|
greyAdd = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
greyAdd+=brightAdj;
|
||
|
|
||
|
if (reallySolid)
|
||
|
cout<<targetColorAlpha<<","<<greyAdd<<",";
|
||
|
else
|
||
14 years ago
|
cout<<targetColorAlpha<<","<<greyAdd<<","<<tqAlpha(basePix)<<",";
|
||
|
//cout<<tqRed(basePix)<<","<<tqGreen(basePix)<<","<<tqBlue(basePix)<<","<<tqAlpha(basePix)<<",";
|
||
15 years ago
|
|
||
|
if (pos%8 == 7)
|
||
|
cout<<"\n";
|
||
|
|
||
|
read++;
|
||
|
}
|
||
|
|
||
|
cerr<<s.latin1()<<":"<<pixSolid<<"/"<<pixCount<<"("<<reallySolid<<")\n";
|
||
|
|
||
|
cout<<!reallySolid<<"\n";
|
||
|
|
||
|
cout<<"};\n\n";
|
||
|
}
|
||
|
|
||
|
cout<<"static const KeramikEmbedImage image_db[] = {\n";
|
||
|
|
||
|
for (unsigned int c=0; c<images.size(); c++)
|
||
|
{
|
||
|
cout<<"\t{ "<<(images[c].haveAlpha?"true":"false")<<","<<images[c].width<<", "<<images[c].height<<", "<<images[c].id<<", "<<(char *)images[c].data<<"},";
|
||
|
cout<<"\n";
|
||
|
}
|
||
|
cout<<"\t{0, 0, 0, 0, 0}\n";
|
||
|
cout<<"};\n\n";
|
||
|
|
||
|
cout<<"class KeramikImageDb\n";
|
||
|
cout<<"{\n";
|
||
|
cout<<"public:\n";
|
||
|
cout<<"\tstatic KeramikImageDb* getInstance()\n";
|
||
|
cout<<"\t{\n";
|
||
|
cout<<"\t\tif (!instance) instance = new KeramikImageDb;\n";
|
||
|
cout<<"\t\treturn instance;\n";
|
||
|
cout<<"\t}\n\n";
|
||
|
cout<<"\tstatic void release()\n";
|
||
|
cout<<"\t{\n";
|
||
|
cout<<"\t\tdelete instance;\n";
|
||
|
cout<<"\t\tinstance=0;\n";
|
||
|
cout<<"\t}\n\n";
|
||
|
cout<<"\tKeramikEmbedImage* getImage(int id)\n";
|
||
|
cout<<"\t{\n";
|
||
|
cout<<"\t\treturn images[id];\n";
|
||
|
cout<<"\t}\n\n";
|
||
|
cout<<"private:\n";
|
||
|
cout<<"\tKeramikImageDb():images(503)\n";
|
||
|
cout<<"\t{\n";
|
||
|
cout<<"\t\tfor (int c=0; image_db[c].width; c++)\n";
|
||
|
cout<<"\t\t\timages.insert(image_db[c].id, &image_db[c]);\n";
|
||
|
cout<<"\t}\n";
|
||
|
cout<<"\tstatic KeramikImageDb* instance;\n";
|
||
14 years ago
|
cout<<"\tTQIntDict<KeramikEmbedImage> images;\n";
|
||
15 years ago
|
cout<<"};\n\n";
|
||
|
cout<<"KeramikImageDb* KeramikImageDb::instance = 0;\n\n";
|
||
|
|
||
|
cout<<"KeramikEmbedImage* KeramikGetDbImage(int id)\n";
|
||
|
cout<<"{\n";
|
||
|
cout<<"\treturn KeramikImageDb::getInstance()->getImage(id);\n";
|
||
|
cout<<"}\n\n";
|
||
|
|
||
|
cout<<"void KeramikDbCleanup()\n";
|
||
|
cout<<"{\n";
|
||
|
cout<<"\t\tKeramikImageDb::release();\n";
|
||
|
cout<<"}\n";
|
||
|
|
||
|
|
||
|
|
||
|
|
||
15 years ago
|
TQFile file("keramikrc.h");
|
||
15 years ago
|
file.open(IO_WriteOnly);
|
||
15 years ago
|
TQTextStream ts( &file);
|
||
15 years ago
|
ts<<"#ifndef KERAMIK_RC_H\n";
|
||
|
ts<<"#define KERAMIK_RC_H\n";
|
||
|
|
||
|
ts<<"enum KeramikWidget {\n";
|
||
15 years ago
|
for (TQMap<TQString, int>::iterator i = assignID.begin(); i != assignID.end(); ++i)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString name = "keramik_"+i.key();
|
||
14 years ago
|
name.replace("-","_");
|
||
15 years ago
|
ts<<"\t"<<name<<" = "<<i.data()<<",\n";
|
||
|
}
|
||
|
ts<<"\tkeramik_last\n";
|
||
|
ts<<"};\n";
|
||
|
|
||
|
ts<<"#endif\n";
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
// vim: ts=4 sw=4 noet
|